[Datetime-SIG] PEP 495: What's left to resolve
alexander.belopolsky at gmail.com
Tue Sep 8 09:59:15 CEST 2015
On Mon, Sep 7, 2015 at 9:57 PM, Alexander Belopolsky <
alexander.belopolsky at gmail.com> wrote:
> 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
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
Before anyone complaints that this makes comparison 4x slower, I note that
we can add obvious optimizations for the common tzinfo is
datetime.timezone.utc and isinstance(tzinfo, datetime.timezone) cases.
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 that.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Datetime-SIG