This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: datetime.timezone returns the wrong tzname()
Type: Stage:
Components: Library (Lib) Versions: Python 3.3, Python 3.4
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: belopolsky, lregebro
Priority: normal Keywords:

Created on 2013-03-19 20:36 by lregebro, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (2)
msg184673 - (view) Author: Lennart Regebro (lregebro) Date: 2013-03-19 20:36
When calling tzname() on a timezone object it will return "UTC" + the offset.

>>> from datetime import timezone, timedelta, datetime
>>> tz = timezone(timedelta(hours=3))
>>> dt = datetime(2013, 3, 14, 12, 30, tzinfo=tz)
>>> dt.tzname()
'UTC+03:00'

But this breaks strftime:

>>> dt.strftime("%Z%z")
'UTC+03:00+0300'

I think that tzname() should never return an offset, and that for the static offset "timezone" class should always return 'GMT' for any offset, unless a name was explicitly set when creating the timezone instance.

The timezone.utc timezone instance should have the name set to 'UTC'.

This is consistent with how time.tzname works, and hence provides Least Surprise:

>>> import time
>>> time.timezone
86400
>>> time.tzname
('PST', 'PDT')

>>> import os
>>> os.environ['TZ'] = 'GMT-3'
>>> time.tzset()
>>> time.timezone
-10800
>>> time.tzname
('GMT', 'GMT')

>>> os.environ['TZ'] = 'UTC'
>>> time.tzset()
>>> time.timezone
0
>>> time.tzname
('UTC', 'UTC')
msg190732 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2013-06-06 22:33
This is not a bug in datetime.timezone.  The value returned by timezone.tzname() is documented and the code works correctly.  %Z should not be used to produce machine-readable timestamps and for a human reader 'UTC+03:00+0300' should not be confusing.

Note that calling the astimezone() method (without arguments) will return local time with tzname set:

>>> os.putenv('TZ', 'XYZ-3')
>>> time.tzset()
>>> utctime = datetime.datetime.now(datetime.timezone.utc)
>>> localtime = utctime.astimezone()
>>> localtime.strftime('%Z%z')
'XYZ+0300'
History
Date User Action Args
2022-04-11 14:57:43adminsetgithub: 61688
2013-06-06 22:33:48belopolskysetstatus: open -> closed

nosy: + belopolsky
messages: + msg190732

resolution: not a bug
2013-03-19 20:36:00lregebrocreate