Open
Conversation
Implements parsing and formatting of ISO 8601 duration strings
(P1Y2M3DT4H5M6S format), with arithmetic support for Arrow objects.
The Duration class supports:
- Parsing: Duration.parse("P1Y2M3DT4H5M6S")
- Formatting: str(duration) / duration.isoformat()
- Arrow integration: arrow.get("2020-01-01") + Duration(years=1)
- Conversion to/from timedelta and relativedelta
- Duration arithmetic (add, subtract, negate)
- Fractional values (PT1.5S)
- Weeks (P2W)
Closes arrow-py#757
Contributor
Author
Test ResultsAll 39 new tests pass, and all 1940 existing tests continue to pass: Manual test: >>> from arrow import Duration
>>> d = Duration.parse("P1Y2M3DT4H5M6S")
>>> d
Duration(years=1, months=2, days=3, hours=4, minutes=5, seconds=6)
>>> str(d)
'P1Y2M3DT4H5M6S'
>>> import arrow
>>> arrow.get("2020-01-01") + Duration(years=1, months=2)
<Arrow [2021-03-01T00:00:00+00:00]> |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1251 +/- ##
==========================================
Coverage 100.00% 100.00%
==========================================
Files 10 11 +1
Lines 2315 2420 +105
Branches 358 379 +21
==========================================
+ Hits 2315 2420 +105 ☔ View full report in Codecov by Sentry. |
- Fix import ordering in test_duration.py (stdlib before third-party) - Apply black formatting to both duration.py and test_duration.py - Fix mypy type error in from_timedelta by using typed local variable
Contributor
Author
|
any chance this could get reviewed? happy to make changes |
Cover NotImplemented returns, microsecond handling in from_timedelta, years/months in to_timedelta, and fractional number formatting.
Contributor
Author
|
Pushed additional tests to cover the missing lines flagged by codecov - NotImplemented returns, microsecond handling, years/months in to_timedelta, and fractional formatting. Should bring patch coverage up to 100%. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds a
Durationclass that handles ISO 8601 duration strings (theP1Y2M3DT4H5M6Sformat). Came up in #757.Supports parsing, formatting, and doing math with Arrow objects:
Handles weeks (
P2W), fractional values (PT1.5S), and the full date+time combination. Also supports duration arithmetic (add/subtract/negate) and equality/hashing.39 new tests covering parsing, formatting, roundtrips, arithmetic, conversions, edge cases, and error handling.
Closes #757