[New-bugs-announce] [issue30443] datetime.datetime.__add__ does not respect fold.

Michael R. Shannon report at bugs.python.org
Tue May 23 17:21:46 EDT 2017

New submission from Michael R. Shannon:

datetime.datetime.__add__ is currently implemented by converting the date into a datetime.timedelta object (using toordinal) before adding it to the other (timedelta) object and reconstructing with datetime.combine.  With this method, fold will always be set to 0.

There are two cases that will produce an incorrect result (assuming positive timedelta for examples).

1. fold=0 and the timedelta is large enough to push the time into or past a fold.  Either fold will be 0 and not 1 as it should or the time will be too far ahead by the size of the fold.

2. fold=1 and the timedelta is small enough to leave the time in the fold but fold will be set to 0.  This results in the time being too far behind by the size of the fold.

A possible fix is to convert to UTC before adding the timedelta and call self._tzinfo.fromutc() on the result.

components: Library (Lib)
messages: 294285
nosy: belopolsky, mrshannon
priority: normal
severity: normal
status: open
title: datetime.datetime.__add__ does not respect fold.
type: behavior
versions: Python 3.6, Python 3.7

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list