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

Tim Peters tim.peters at gmail.com
Tue Sep 8 19:06:00 CEST 2015

>> Solution 1: Make t1 > t0.
>> Solution 2: Leave t1 == t0, but make t1 != u1.
> Solution 3:  Leave t1 == t0, but make *both* t0 != u0 and t1 != u1 if
> t0.utcoffset() != t1.utcoffset().
> In other words,
> def __eq__(self, other):
>     n_self = self.replace(tzinfo=None)
>     n_other = other.replace(tzinfo=None)
>     if self.tzinfo is other.tzinfo:
>         return n_self == n_other

Well, that's infinite recursion - but I know what you mean ;-)

>     u_self = n_self - self.utcoffset()
>     v_self = n_self - self.replace(fold=(1-self.fold)).utcoffset()
>     u_other = n_other - other.utcoffset()
>     v_other = n_other - other.replace(fold=(1-self.fold)).utcoffset()
>     return u_self == u_other == v_self == v_other

More infinite recursion.

> Before anyone complaints that this makes comparison 4x slower,

I don't care about the speed of by-magic interzone comparison, but if
someone does I'd say it's only about 2x slower.  .utcoffset() is the
major expense, and this only doubles the number of those.

> I note that we can add obvious optimizations for the common tzinfo is
> datetime.timezone.utc and  isinstance(tzinfo, datetime.timezone) cases.

Please no.  Comparison is almost certainly almost always intrazone,
and .utcoffset() isn't called at all for intrazone comparisons.

> Users that truly want to compare aware datetime instances between two
> variable offset timezones, should realize that fold/gap detection in *both*
> r.h.s. and l.h.s. zones is part of the operation that they request.
> This solution has some nice properties compared to the solution 2: (1) it
> restores the transitivity - we no longer have u0 == t0 == t1 and t1 != u1;
> (2) it restores the symmetry between fold=0 and fold=1 while preserving a
> full backward compatibility.
> I also think this solution makes an intuitive sense: since we cannot decide
> which of the two UTC times u0 and u1 should belong in the equivalency class
> of t0 == t1 - neither should.  "In the face of ambiguity" and all

I do like that this "breaks" interzone comparison only in cases where
`fold` actually makes a difference.  Certainly more principled and
focused than special-casing the snot out of all and only fold=1.

But I can never decide whether something really "fixes the hash
problem" without a lot more thought.

So far, so good :-)

More information about the Datetime-SIG mailing list