Rules misapplied when using pytz as parameter in a datetime instantiation
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
pytz |
Won't Fix
|
Undecided
|
Unassigned |
Bug Description
I know America/Montreal should be a link to America/Toronto.
Tested in Python 3.6.8
$ pipenv run pip list
Package Version Location
----------------- -------
...
pytz 2019.3
...
$ pipenv run python
Python 3.6.8 (v3.6.8:3c6b436a57, Dec 24 2018, 02:04:31)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pytz
>>> from datetime import datetime
>>> dd = datetime(2019, 6, 6, 6, 6, 6, 6, pytz.timezone(
>>> dd.astimezone(
datetime.
Why the time is now 11:24? The minutes should be 06.
>>> dd2 = datetime(2019, 6, 6, 6, 6, 6, 6, pytz.timezone(
>>> dd2.astimezone(
datetime.
Consistent for Toronto
>>> dd3 = datetime(2019, 6, 6, 6, 6, 6, 6, pytz.timezone(
>>> dd3
datetime.
>>> dd3.astimezone(
datetime.
Works when converted afterwards
My understanding is that construction of a datetime using the constructor that takes a pytz timezone object is not supported (http:// pytz.sourceforg e.net/# localized- times-and- date-arithmetic /www.python. org/dev/ peps/pep- 0431/).
) due to a deficiency in the datetime API (https:/
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:
``` 'America/ Toronto' ) 'America/ Montreal' )
>>> toronto = pytz.timezone(
>>> montreal = pytz.timezone(
>>> dtor = toronto. localize( datetime( 2019, 6, 6, 6, 6, 6, 6)) localize( datetime( 2019, 6, 6, 6, 6, 6, 6))
>>> dmon = montreal.
>>> print(dtor) 000006- 04:00
2019-06-06 06:06:06.
>>> print(dmon) 000006- 04:00
2019-06-06 06:06:06.
```