[Datetime-SIG] PEP 495: What's left to resolve

Tim Peters tim.peters at gmail.com
Wed Sep 9 06:25:08 CEST 2015


[Alex]
> ...
> The question is what is easier to understand: (a) t1 and t2 are equal if and
> only if t1 - t1.replace(fold=f1).utcoffset() ==  t2 -
> t2.replace(fold=f2).utcoffset() for all four possible pairs (f1, f2);

Infinite recursion again ;-) , but this time because interzone
equality is being defined in terms of 4 more interzone equalities.

> or (b)
> t1 and t2 are equal if and only if they are unambiguous and valid in their
> respective zones and convert to the same UTC instant.

The docs generally do both, when feasible:  an English description,
followed by a Python expression to resolve the inherent imprecision of
English.  The intent is for the English to give the high-order bits,
and for the Python expression to leave no possible misunderstanding.

For this case, the clearest Python I can think of is:

    def toutc(t, fold):
        return (t - t.replace(fold=fold).utcoffset()).replace(tzinfo=None)

Then t1 == t2, when t1 and t2 are aware datetimes in different zones,
if and only if:

    toutc(t1, 0) == toutc(t1, 1) == toutc(t2, 0) == toutc(t2, 1)

Then there's no English remaining to be misread.  As a side benefit,
the correctness of short-circuiting if t1 is a problem case becomes
dead obvious on the face of it ;-)


More information about the Datetime-SIG mailing list