Skip to content

gh-115810: Expose name attribute of datetime.timezone #131210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Doc/library/datetime.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2421,6 +2421,10 @@ Class attributes:

The UTC time zone, ``timezone(timedelta(0))``.

.. attribute:: timezone.name

The name of the timezone if specified during creation. If no name is provided,
an empty string is returned.

.. index::
single: % (percent); datetime format
Expand Down
7 changes: 7 additions & 0 deletions Lib/_pydatetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -2486,6 +2486,13 @@ def tzname(self, dt):
raise TypeError("tzname() argument must be a datetime instance"
" or None")

@property
def name(self):
"""Return provided name otherwise empty string."""
if not self._name:
return ''
return self._name

def dst(self, dt):
if isinstance(dt, datetime) or dt is None:
return None
Expand Down
7 changes: 7 additions & 0 deletions Lib/test/datetimetester.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,13 @@ def test_tzname(self):
with self.assertRaises(TypeError): self.EST.tzname('')
with self.assertRaises(TypeError): self.EST.tzname(5)

def test_name_property(self):
tz_named = timezone(timedelta(hours=5), 'IST')
self.assertEqual(tz_named.name, 'IST')

tz_unnamed = timezone(timedelta(hours=0))
self.assertEqual(tz_unnamed.name, '')

def test_fromutc(self):
with self.assertRaises(ValueError):
timezone.utc.fromutc(self.DT)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add ``name`` attribute to :class:`datetime.timezone` that returns the ``name``
provided during creation else an empty string.
19 changes: 18 additions & 1 deletion Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -4373,6 +4373,16 @@ timezone_tzname(PyObject *op, PyObject *dt)
return timezone_str(op);
}

static PyObject *
timezone_name(PyDateTime_TimeZone *self, void *closure)
{
if (self->name == NULL) {
return PyUnicode_FromString("");
}
Py_INCREF(self->name);
return self->name;
}

static PyObject *
timezone_utcoffset(PyObject *op, PyObject *dt)
{
Expand Down Expand Up @@ -4440,6 +4450,13 @@ static PyMethodDef timezone_methods[] = {
{NULL, NULL}
};

static PyGetSetDef timezone_getset[] = {
{"name", (getter)timezone_name, NULL,
PyDoc_STR("If name is specified when timezone is created, returns the name."), NULL},
{NULL}
};


static const char timezone_doc[] =
PyDoc_STR("Fixed offset from UTC implementation of tzinfo.");

Expand Down Expand Up @@ -4473,7 +4490,7 @@ static PyTypeObject PyDateTime_TimeZoneType = {
0, /* tp_iternext */
timezone_methods, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
timezone_getset, /* tp_getset */
0, /* tp_base; filled in PyInit__datetime */
0, /* tp_dict */
0, /* tp_descr_get */
Expand Down
Loading