[Datetime-SIG] Calendar vs timespan calculations...

Tim Peters tim.peters at gmail.com
Sat Aug 1 07:39:19 CEST 2015

[Łukasz Rekucki]
>> What happens then when you substract a datetime with *strict* tzinfo
>> and a *naive* one? Would A - B == - (B - A) still be true ?

> [re-adding the list]
> That's for the authors of the new PEP to decide, really, but I think it
> could be made to follow the strict rules in both cases, since clearly the
> code isn't an old program requiring backward compatibility (how would such a
> program end up with a strict tzinfo?).

This one solves itself:  it's _already_ the case that subtraction of
aware datetime objects uses timeline arithmetic _unless_ both
datetimes share a .tzinfo member.  If one uses a tzstrict instance and
the other does not, it's impossible that they both use the same
instance.  So timeline arithmetic will be used in any such case.

Maybe some sketchy pseudocode will make it more obvious:

class datetime:
   def __sub__(x, y):
       # assume x and y are both aware datetimes
       if x.tzinfo is y.tzinfo:
           # compute the difference using classic arithmetic
           # compute the difference using timeline arithmetic

It's been like that forever.  Note that the order of the operands is
irrelevant to which kind of arithmetic is used.  The same applies,
mutatis mutandis, to datetime comparison operations..

What does need to change is that "x.tzinfo is y.tzinfo" needs more
qualification, so that two datetimes sharing the same tzstrict
instance don't end up using classic subtraction.  I'm sure the docs
will become even more pleasant to read ;-)

More information about the Datetime-SIG mailing list