-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
gh-99772: Do not ignore utc offset microseconds #99774
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Fix compare operations for datetime.time values with microseconds resolution in utcoffset. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4473,22 +4473,24 @@ time_richcompare(PyObject *self, PyObject *other, int op) | |
| } | ||
| /* The hard case: both aware with different UTC offsets */ | ||
| else if (offset1 != Py_None && offset2 != Py_None) { | ||
| int offsecs1, offsecs2; | ||
| int64_t offusecs1, offusecs2, diff64; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might be a minor discussion point, but we could avoid using
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually implemented it like you suggest earlier, because I share your gut feeling. Using the existing code for checks up to seconds and adjusting only the microseconds check. Indeed with the advantage that it all fits in 32 bits. What I encountered using this approach was the following: 13:59:59.9+02 is actually later than 14:00:00+02:00:00.9. The combined microseconds values, in the time values and the offsets, can contribute up to a maximum of 1.999998 seconds to the diff in either way. That means that the simple intermediary check that is used in other multi step comparisons
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @blenq Does one of the tests already handle the use case of |
||
| assert(offset1 != offset2); /* else last "if" handled it */ | ||
| offsecs1 = TIME_GET_HOUR(self) * 3600 + | ||
| TIME_GET_MINUTE(self) * 60 + | ||
| TIME_GET_SECOND(self) - | ||
| GET_TD_DAYS(offset1) * 86400 - | ||
| GET_TD_SECONDS(offset1); | ||
| offsecs2 = TIME_GET_HOUR(other) * 3600 + | ||
| TIME_GET_MINUTE(other) * 60 + | ||
| TIME_GET_SECOND(other) - | ||
| GET_TD_DAYS(offset2) * 86400 - | ||
| GET_TD_SECONDS(offset2); | ||
| diff = offsecs1 - offsecs2; | ||
| if (diff == 0) | ||
| diff = TIME_GET_MICROSECOND(self) - | ||
| TIME_GET_MICROSECOND(other); | ||
| offusecs1 = TIME_GET_HOUR(self) * INT64_C(3600000000) + | ||
| TIME_GET_MINUTE(self) * INT64_C(60000000) + | ||
| TIME_GET_SECOND(self) * 1000000 + | ||
| TIME_GET_MICROSECOND(self) - | ||
| GET_TD_DAYS(offset1) * 86400000000 - | ||
| GET_TD_SECONDS(offset1) * INT64_C(1000000) - | ||
| GET_TD_MICROSECONDS(offset1); | ||
| offusecs2 = TIME_GET_HOUR(other) * INT64_C(3600000000) + | ||
| TIME_GET_MINUTE(other) * INT64_C(60000000) + | ||
| TIME_GET_SECOND(other) * 1000000 + | ||
| TIME_GET_MICROSECOND(other) - | ||
| GET_TD_DAYS(offset2) * 86400000000 - | ||
| GET_TD_SECONDS(offset2) * INT64_C(1000000) - | ||
| GET_TD_MICROSECONDS(offset2); | ||
| diff64 = offusecs1 - offusecs2; | ||
| diff = diff64 ? diff64 < 0 ? -1 : 1 : 0; /* diff_to_bool needs int */ | ||
| result = diff_to_bool(diff, op); | ||
| } | ||
| else if (op == Py_EQ) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do we need the minus sign here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because the utc offset contributes to the time in a negative way, in other words, the greater the utc offset, the earlier the actual time.