Skip to content

Conversation

@dyleeeeeeee
Copy link

Description

Enhances Duration.parse() to support compound duration strings like "1h30m", "2d3h15m", etc.

Motivation

Currently, Duration.parse() only supports single-unit durations ("2h", "30m").
This limitation requires users to convert compound durations manually. For example,
"1 hour and 30 minutes" cannot be expressed as "1h30m" and must be converted to "90m".

Changes

  • Modified Duration.parse() to use regex pattern matching for flexible parsing
  • Supports multiple units in a single string: "1h30m", "2d3h15m30s", etc.
  • Maintains full backward compatibility with existing single-unit durations
  • Added comprehensive test coverage for new functionality

Testing

  • All existing tests pass
  • Added new tests covering compound duration parsing
  • Verified backward compatibility with single-unit durations

Examples

# New functionality
Duration.parse("1h30m")      # 90 minutes
Duration.parse("2d3h15m")    # 2 days, 3 hours, 15 minutes
Duration.parse("1h30m45s")   # 1 hour, 30 minutes, 45 seconds

# Existing functionality (still works)
Duration.parse("2h")         # 2 hours
Duration.parse("45m")        # 45 minutes

Enhance Duration.parse() to handle compound duration strings like
\"1h30m\", \"2d3h15m\", etc., in addition to single-unit durations.

- Modified parse() to use regex pattern matching
- Added support for multiple units in a single duration string
- Maintained backward compatibility with single-unit durations
- Added comprehensive test coverage

Resolves common use cases where users need to express durations
with multiple units (e.g., \"1 hour and 30 minutes\" as \"1h30m\").
The regex pattern in Duration.parse() includes 'µs' (microsecond with µ symbol) and 'y' (year) but they were missing from the UNITS dictionary, causing ValueError when parsing durations with these units.

Added:
- 'µs': microsecond (same as 'us') = 1e3 nanoseconds
- 'y': year (365 days) = 31,536,000,000,000,000 nanoseconds

Also added years property and updated to_string() to include years in the unit hierarchy.
- Replace simple compound test with comprehensive test using all units (1y2w3d4h5m6s7ms8us9ns)
- Add individual tests for years (y) and microseconds with µ symbol (µs)
- Update properties test to include years property
- Add years to to_string test
- Ensures complete coverage of newly added duration units
Merge separate tests into one that verifies both 'us' and 'µs' variants produce identical results, ensuring the µ symbol variant works correctly.
@martinschaer martinschaer requested a review from ssttuu November 13, 2025 12:33
@tobiemh tobiemh linked an issue Nov 19, 2025 that may be closed by this pull request
2 tasks
@martinschaer
Copy link
Collaborator

hi @dyleeeeeeee ! Can you run uv run ruff format src/ and uv run ruff check --fix src/ to fix the "CI / Check format" error?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: IndexError when parsing Duration fields from CBOR response

4 participants