chrisimcevoy / pyoda-time

A better date and time API for Python
https://pyodatime.org/
Apache License 2.0
1 stars 0 forks source link

feat: port `DurationPattern` #155

Closed chrisimcevoy closed 4 months ago

chrisimcevoy commented 4 months ago

Brief bit of exposition needed here first.

In .NET, spans of time tend to be represented by TimeSpan. The Noda Time equivalent to that is a Duration. Noda Time's Duration is capable of conversion to and from all the values in the range of TimeSpan, which covers 292 years or so. In fact, Noda Time's Duration has a much larger range than that, something like 20,000+ years.

When Duration was implemented back in #47, I decided that for Pyoda Time, Duration should have a similar relationship with datetime.timedelta, i.e. it should be possible to convert all possible timedelta values to and from the Pyoda type. The thing is that timedelta's range is so vast that it makes the .NET range look measly by comparison.

At the time, I decided to drastically increase the range of Duration from the range offered by Noda Time, so that the Pyoda Duration would have a range which is slightly larger than timedelta. That decision has had knock-on effects for the DurationPattern port, particularly with regard to parsing validation and so forth.

The end result is that this DurationPattern implementation is very reminiscent of the one you'll find in the mother project, but it allows for much, much greater values to be parsed and formatted.

One other small note. I'm currently having a discussion with myself over in #154 about string formatting stuff. Included in this PR is the use of the new pattern in Duration.__str__ and Duration.__format__. There is no test coverage for that currently, but I had a bit of a play in a REPL and...

>>> from pyoda_time import Duration
>>> 
>>> 
>>> Duration.max_value
1073741823:23:59:59.999999999
>>> Duration.min_value
-1073741824:00:00:00
>>> Duration.epsilon
0:00:00:00.000000001
>>> f"{Duration.max_value:o}"
'1073741823:23:59:59.999999999'
>>> f"{Duration.max_value:j}"
'25769803775:59:59.999999999'
>>> f"{Duration.max_value:D hh mm ss 'hello world'}"
'1073741823 23 59 59 hello world'
codecov[bot] commented 4 months ago

Codecov Report

Attention: Patch coverage is 95.12195% with 12 lines in your changes missing coverage. Please review.

Project coverage is 95.54%. Comparing base (d989c2b) to head (cf5bf40). Report is 27 commits behind head on main.

Files with missing lines Patch % Lines
pyoda_time/_duration.py 40.00% 6 Missing :warning:
pyoda_time/text/_duration_pattern.py 92.20% 6 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #155 +/- ## ========================================== - Coverage 95.54% 95.54% -0.01% ========================================== Files 238 241 +3 Lines 18710 18953 +243 ========================================== + Hits 17876 18108 +232 - Misses 834 845 +11 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.