Rules misapplied when using pytz as parameter in a datetime instantiation

Bug #1855021 reported by Russoz
6
This bug affects 1 person
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("America/Montreal"))
>>> dd.astimezone(pytz.timezone("UTC"))
datetime.datetime(2019, 6, 6, 11, 24, 6, 6, tzinfo=<UTC>)

Why the time is now 11:24? The minutes should be 06.

>>> dd2 = datetime(2019, 6, 6, 6, 6, 6, 6, pytz.timezone("America/Toronto"))
>>> dd2.astimezone(pytz.timezone("UTC"))
datetime.datetime(2019, 6, 6, 11, 24, 6, 6, tzinfo=<UTC>)

Consistent for Toronto

>>> dd3 = datetime(2019, 6, 6, 6, 6, 6, 6, pytz.timezone("UTC"))
>>> dd3
datetime.datetime(2019, 6, 6, 6, 6, 6, 6, tzinfo=<UTC>)
>>> dd3.astimezone(pytz.timezone("America/Toronto"))
datetime.datetime(2019, 6, 6, 2, 6, 6, 6, tzinfo=<DstTzInfo 'America/Toronto' EDT-1 day, 20:00:00 DST>)

Works when converted afterwards

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
```

Revision history for this message
Russoz (russoz) wrote :

Hi Brian, thanks for the comprehensive explanation. I still think there's an improvement opportunity on datetime side though. Feel free to close this ticket at your convenience.

Cheers
Alex

Revision history for this message
Stuart Bishop (stub) wrote :

Improvement work needs to happen in upstream Python, which can now support pytz functionality with a better API. Changes can't happen here without breaking backwards compatibility.

Changed in pytz:
status: New → Won't Fix
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.