Problem with differences between time points
See original GitHub issueHi,
the shift()
method is nice and seems to work with localized time with some kind of “natural” semantic. However it leads to some results (related to differences between time points).
>>> t0 = arrow.now().shift(weeks=3) # The day before the next DST fall transition
>>> t0
<Arrow [2019-10-26T16:33:24.883042+02:00]>
>>> t1 = t0.shift(days=1)
>>> t1
<Arrow [2019-10-27T16:33:24.883042+01:00]>
>>> t1 - t0
datetime.timedelta(1)
So far, so good. A DST transition occurs between t0
and t1
(I’m living in Paris) and shifting by one day keeps the same time (16:33) with UTC offset adjusted (+02:00 to +01:00). The difference is one day, expressed as a timedelta
.
Now, convert the dates into UTC:
>>> t2 = t0.to('UTC')
>>> t2
<Arrow [2019-10-26T14:33:24.883042+00:00]>
>>> t3 = t1.to('UTC')
>>> t3
<Arrow [2019-10-27T15:33:24.883042+00:00]>
>>> t2 == t0 and t3 == t1
True
>>> t1 - t2
datetime.timedelta(1, 3600)
>>> (t3 - t2) == (t1 - t0)
False
>>> t0 + (t3 - t2) == t1
False
The problem is that, while the time points are the same in local time or UTC (which is expected), their difference is no longer the same. And a few expected invariants are no longer true.
As a conclusion,
- Differences between time points shall always imply a conversion to UTC, even if they are in the same time zone. At least, we would have
t1 - t0 == t3 - t2 == t3 - t0 == t1 - t2
. This should be clearly explained in the documentation. - The documentation should explain that a difference between 2 time points lead to a “naive” delta, even if
arrow
enforces time zone aware time points. - Time zone aware time delta could be introduced with a clean semantic (to be defined?)…
Best regards,
Antoine
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Comparing means between two groups over three time points
I have two groups of ten observations each, where the weight are measured at three different points in time. Let´s say baseline, ...
Read more >Compare changes between two groups over time?
1) Repeated measures ANOVA, can show me differences between time-points of each group: The WT group has increased significantly from M4 to ...
Read more >Are differences between groups different at ...
More specifically, the core research question is usually whether the difference between groups of interest changes from one occasion or time ...
Read more >Multiple groups or comparisons
The groups can be compared with a simple chi-squared (or Fisher's exact) test. Page 2. 2. Comparing multiple groups. ANOVA – Analysis of...
Read more >Detecting time-specific differences between temporal ... - NCBI
In multiple fields of study, time series measured at high frequencies are used to estimate population curves that describe the temporal ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Well, I just meant the arithmetic part of it. The proposal here is not entirely clear IMO, but anything using arrow-specific methods won’t affect compatibility with datetime.
There might be room for some sort of “absolute time difference” method, or custom timedelta types that encode whether or represents an absolute or calendar difference.
Also, pendulum did abandon this particular but of compatibility, so it’s not entirely without precedent. I am not sure if it has caused them any problems.
I mainly wanted to clarify that arithmetic in datetime is defined in a specific way and users may be relying on that.
Hi,
The very basic requirement in my example is that since 1)
t0
andt2
represent the very same time point and 2)t1
andt3
represent the very same time point, then 3) we should at least havet1 - t0
represent the same time delta ast3 - t2
. It shall not depend on the timezone in which the time points have their representation. But unfortunately it does…In the example, the difference shall be 25 hours, which means, expressed as a
timedelta
: one day + one hour with one day being defined as exactly 24 hours (and not one day having a varying natural semantic thetimedelta
can not express).Points 1 & 2 in my proposal shall be enough to deal with that in a consistent way. Enforcing both time points to be in the same time zone when we take the difference is not enough.
Next, let me try to clarify point 3. The design of
arrow
takes into account the fact time zones are an intrinsic component of a time point (in other words, naive time points are ambiguous since they to not identify a unique point on a universal time axis).The point is this design rule shall be extended to time differences, in order to give consistency to calculus over time points and their differences. I would say this is a problem with timedelta that shall also carry time zone information. I can see two solutions.
First one (my points 1 & 2 in original post): use existing
timedelta
, but explicitly state that those naïve time deltas live in the UTC time zone and enforce a few constraints such as:Second one; use a custom time delta with a custom semantic, where one day would means one natural day. Then add and substract those time delta with
arrow
’sshift
semantic. Note this could yield to curious results when the delta is obtained from 2 time points with a DST transition in between (especially if one of the time points is such a day) and is added back to another time point.Or, perhaps accept a naive
timedelta
as an argument toshift
(with natural semantic)?Hope this helps.
Best regards,
Antoine