Skip to content

Expose name attribute of datetime.timezone objects #115810

@ariebovenberg

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

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

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions