Open ArneBachmannDLR opened 2 months ago
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:
if you care about calendar days, do your arithmetic on Date
objects:
>>> dt.date() - Date(1970, 1, 1)
DateDelta(P54Y3M8D)
# note that this may be different, depending on `dt` UTC offset!
>>> dt.as_utc().date() - Date(1970, 1, 1)
DateDelta(P54Y3M9D)
If you care about days as exact 24-hour time units, make this explicit:
>>> (dt - UTCDateTime(1970, 1, 1)).in_hours() / 24
19822.5
the reason there is no in_days()
is because days are sometimes more or less than 24 hours long, due to daylight saving time.
.py_datetime()
instead of the private _py_dt
. The private attribute may be removed in later releases.The complicating factor here is that if UTCDateTime - x
simply returns a DateTimeDelta
, that you can't do .in_hours()
or similar since calendar units aren't exact.
The solution is perhaps some kind of Difference
object which can be interpreted on the calendar, as well as the timeline. I will create an issue about this in the future, and link it here.
Just noticed that
(UTCDateTime(2022, 2, 11) - UTCDateTime(1970, 1, 1)).days
gives 10 instead of 19358(dt - UTCDateTime(1970, 1, 1)).in_hours() // 24
returns a float instead of an intIndeed:
.days
refers to the days component of the DateDelta
: Date(2024, 4, 11) - Date(1970, 1, 1)
is DateDelta(P54Y3M10D)
, i.e. .years=54
, .months=3
, .days=10
. The .days
attribute is perhaps too ambiguous, and can lead to misunderstandings—especially coming from datetime.timedelta
. Perhaps renaming it to days_component
is less ambiguous 🤔 float
is actually correct right? There probably isn't an exact 24h-divisible difference between two moments. You need to explicitly decide whether to round up or downedit: mistaken first conclusion
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