You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Expose name attribute of datetime.timezone objects #115810
datetime.timezone has two parameters: offset and name, but name cannot be accessed.
Yes, there is the tzname() method, but this method will autogenerate a name when none is given. There is no way to reliably distinguish a 'named' timezone from an unnamed one.
Problem: we can't strictly distinguish named from unnamed
>>>named_tz.tzname(None)
"CET"# as expected>>>unnamed_tz.tzname(None)
"UTC+01:00"# some autogenerated name. Who's to say this wasn't explicitly given?
Proposal: expose the name attribute on timezone objects
>>>named_tz.name"CET">>>unnamed_tz.name""
edit: changed from None to empty string
Potential issues
Users may be confused by seeing both .name and .tzname(). However, this shouldn't be much of an issue since .name won't be part of the tzinfo interface. If dealing explicitly with timezone, I'd argue .name is pretty unambiguous about what it does. tzname() being a method indicates that it 'does more'—i.e. autogenerates something.
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
My use case is that I'd like to serialize fixed offset datetimes to a string, while keeping any timezone name if present.
dt=datetime(...)
as_text=f"{dt.isoformat()}{dt.tzname()}"deffrom_text(s) ->datetime:
"load a fixed-offset datetime from a string, including any timezone name"
In case of named timezones, you get something like 2020-03-31T00:00:00+01:00 CET,
but for unnamed timezones you get 2020-03-31T00:00:00+01:00 UTC+01:00 which is unnecessarily duplicated and doesn't represent an actual timezone name.
seems like a good idea, because otherwise we can't explicitly let the user know the abbreviation for time zone. tzname method is too non-obvious and can return different types of strings ("CET" or something like "UTC+1:00")
I would prefer that tzname could return only the abbreviation if there is a name, but in order not to "break" backward compatibility we can do this in a separate method
I don't think that one small method will overcomplicate the interface, and I'm ready to send PR (I already wrote it for both implementation)
@ariebovenberg Why empty string? Looks to me like the empty string is a valid value for name, which means you can't distinguish between "Unnamed" and "Deliberately given an empty name"
Activity
sobolevn commentedon Feb 22, 2024
CC @pganssle
pganssle commentedon Feb 22, 2024
I am not entirely clear on why you need this information? Did it come up in some real world context, or is it a theoretical concern?
ariebovenberg commentedon Feb 22, 2024
My use case is that I'd like to serialize fixed offset datetimes to a string, while keeping any timezone name if present.
In case of named timezones, you get something like
2020-03-31T00:00:00+01:00 CET,but for unnamed timezones you get
2020-03-31T00:00:00+01:00 UTC+01:00which is unnecessarily duplicated and doesn't represent an actual timezone name.donbarbos commentedon Feb 27, 2025
seems like a good idea, because otherwise we can't explicitly let the user know the abbreviation for time zone.
tznamemethod is too non-obvious and can return different types of strings ("CET"or something like"UTC+1:00")I would prefer that
tznamecould return only the abbreviation if there is a name, but in order not to "break" backward compatibility we can do this in a separate methodI don't think that one small method will overcomplicate the interface, and I'm ready to send PR (I already wrote it for both implementation)
cc @pganssle
donbarbos commentedon Feb 27, 2025
I also suggest adding the
offsetortzoffsetmethod which will always return the time zone asUTC+1orUTC+1:00(not sure which is better)pganssle commentedon Oct 23, 2026
OK, sure, this seems fine.
pganssle commentedon Oct 23, 2026
@ariebovenberg Why empty string? Looks to me like the empty string is a valid value for
name, which means you can't distinguish between "Unnamed" and "Deliberately given an empty name"gh-115810: Expose the `name` attribute of `datetime.timezone` (#131210)