Skip to content

Expose name attribute of datetime.timezone objects #115810

Closed

Description

@ariebovenberg

Feature or enhancement

Summary

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.

Example

>>> offset = timedelta(hours=1)
>>> named_tz = timezone(offset, name="CET")
>>> unnamed_tz = timezone(offset)

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

Links to previous discussion of this feature:

No response

Linked PRs

Activity

  1. sobolevn commented on Feb 22, 2024

    @sobolevn
    Member
  2. pganssle commented on Feb 22, 2024

    @pganssle
    Member

    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?

  3. ariebovenberg commented on Feb 22, 2024

    @ariebovenberg
    ContributorAuthor

    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()}"
    
    def from_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.

  4. donbarbos commented on Feb 27, 2025

    @donbarbos
    Contributor

    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)

    cc @pganssle

  5. donbarbos commented on Feb 27, 2025

    @donbarbos
    Contributor

    I also suggest adding the offset or tzoffset method which will always return the time zone as UTC+1 or UTC+1:00 (not sure which is better)

  6. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Mar 1, 2025
  7. pganssle commented on Oct 23, 2026

    @pganssle
    Member

    OK, sure, this seems fine.

  8. pganssle commented on Oct 23, 2026

    @pganssle
    Member

    edit: changed from None to empty string

    @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"

    >>> dt.timezone(dt.timedelta(), "")
    datetime.timezone(datetime.timedelta(0), '')
  9. added a commit that references this issue on Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirstdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions