Comment 1 for bug 1855021

Revision history for this message
Brian Park (xparks) wrote :

My understanding is that construction of a datetime using the constructor that takes a pytz timezone object is not supported (http://pytz.sourceforge.net/#localized-times-and-date-arithmetic
) due to a deficiency in the datetime API (https://www.python.org/dev/peps/pep-0431/).

Except from the first link above:

"This library only supports two ways of building a localized time. The first is to use the localize() method provided by the pytz library. This is used to localize a naive datetime (datetime with no timezone information):
[...]
The second way of building a localized time is by converting an existing localized time using the standard astimezone() method:
[...]
Unfortunately using the tzinfo argument of the standard datetime constructors ‘’does not work’’ with pytz for many timezones.
[...]
"

This means that datetime for Toronto or Montreal must be constructed this way:

```
>>> toronto = pytz.timezone('America/Toronto')
>>> montreal = pytz.timezone('America/Montreal')

>>> dtor = toronto.localize(datetime(2019, 6, 6, 6, 6, 6, 6))
>>> dmon = montreal.localize(datetime(2019, 6, 6, 6, 6, 6, 6))

>>> print(dtor)
2019-06-06 06:06:06.000006-04:00

>>> print(dmon)
2019-06-06 06:06:06.000006-04:00
```