[Datetime-SIG] Another round on error-checking

Alexander Belopolsky alexander.belopolsky at gmail.com
Fri Sep 4 01:55:05 CEST 2015

On Thu, Sep 3, 2015 at 6:51 PM, Chris Barker <chris.barker at noaa.gov> wrote:
> I know I only got it when someone explained the implementation:
> "remove the tzinfo object, do the math, tack the tzinfo back on"
> Simple elegant, and now I get it. And get why things go wonky with
datetimes with two different tzinfo objects.
> By the way, something like that should be in the docs.

Doc patches from good writers are always welcome, but in this case, I don't
see what needs to be added to what the reference manual already says:

Subtraction of a datetime from a datetime is defined only if both operands
are naive, or if both are aware. If one is aware and the other is naive,
TypeError is raised.

If both are naive, or both are aware and have the same tzinfo attribute,
the tzinfo attributes are ignored, and the result is a timedelta object t
such that datetime2 + t == datetime1. No time zone adjustments are done in
this case.

If both are aware and have different tzinfo attributes, a-b acts as if a
and b were first converted to naive UTC datetimes first. The result is
(a.replace(tzinfo=None) - a.utcoffset()) -(b.replace(tzinfo=None) -
b.utcoffset()) except that the implementation never overflows.


The only improvement that comes to mind is to make "Supported operations:"
a linkable section.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/datetime-sig/attachments/20150903/f7776b75/attachment-0001.html>

More information about the Datetime-SIG mailing list