Issue 17486: datetime.timezone returns the wrong tzname() (original) (raw)

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')