-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve API for calculating difference between two datetimes #108
Comments
Hi @ArneBachmannDLR, thanks for posting this issue—this is indeed a part of the API I'd like to see improved myself as well. Before I go into detail about potential solutions, here are two workarounds, with different semantics:
the reason there is no A note on your workaround
A long-term solutionThe complicating factor here is that if The solution is perhaps some kind of |
Just noticed that
|
Indeed:
edit: mistaken first conclusion |
The new release 0.6.0 has changed the API somewhat to at least prevent confusing situations: >>> (Date(2022, 2, 11) - Date(1970, 1, 1)).in_years_months_days()
(52, 1, 10)
>>> (Instant.now() - Instant.from_utc(1970, 1, 1)).in_days_of_24h()
19908.618500809072 Of course, this still leaves the situation where you'd like to get a |
Another gap in the current API: calculating the number of days between two dates. The API I'm considering: >>> b - a
DateDiff(...) # no longer a Delta, since we CANNOT assume whether we want it in months/days, or only days
>>> (b - a).in_months_days()
DateDelta(P3M5D)
>>> (b - a).in_months_days().tuple() # to allow unpacking
(3, 5)
>>> (b - a).in_days()
96 Still needs some thought... |
I was expecting it to return a DateTimeDelta, but it is:
Hint: I want to compute
(dt - UTCDateTime(1970, 1, 1)).days
.Update: this seems to work:
(dt._py_dt - datetime.datetime(1970, 1, 1, tzinfo=datetime.UTC)).days
The text was updated successfully, but these errors were encountered: