Skip to content
Merged
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
5 changes: 5 additions & 0 deletions Doc/library/datetime.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2476,6 +2476,11 @@ where historical changes have been made to civil time.
Return ``dt + offset``. The *dt* argument must be an aware
:class:`.datetime` instance, with ``tzinfo`` set to ``self``.

.. attribute:: timezone.name

The *name* passed to the constructor, or ``None`` if no name was passed.

.. versionadded:: next

Class attributes:

Expand Down
4 changes: 4 additions & 0 deletions Doc/whatsnew/3.16.rst
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,10 @@ datetime
:meth:`datetime.time.strptime`.
(Contributed by Stan Ulbrych and Karoline Pauls in :gh:`157290`.)

* Add :attr:`datetime.timezone.name`, the *name* passed to the constructor,
or ``None`` if no name was passed.
(Contributed by Stan Ulbrych in :gh:`131210`.)


dbm.gnu
-------
Expand Down
5 changes: 5 additions & 0 deletions Lib/_pydatetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -2539,6 +2539,11 @@ def tzname(self, dt):
raise TypeError("tzname() argument must be a datetime instance"
" or None")

@property
def name(self):
"""Name given when the timezone was created."""
return self._name

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

def test_name(self):
for offset, name in ((5 * HOUR, 'IST'), (ZERO, '\ud800')):
with self.subTest(offset=offset, name=name):
self.assertEqual(name, timezone(offset, name).name)

for tz in (UTC, timezone(ZERO), timezone(-5 * HOUR)):
with self.subTest(tz=tz):
self.assertIsNone(tz.name)

with self.assertRaises(AttributeError): self.EST.name = 'EST'

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 :attr:`datetime.timezone.name`, the *name* passed to the constructor,
or ``None`` if no name was passed.
18 changes: 17 additions & 1 deletion Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -4558,6 +4558,22 @@ static PyMethodDef timezone_methods[] = {
{NULL, NULL}
};

static PyObject *
timezone_name(PyObject *op, void *Py_UNUSED(closure))
{
PyDateTime_TimeZone *self = PyTimeZone_CAST(op);
if (self->name == NULL) {
Py_RETURN_NONE;
}
return Py_NewRef(self->name);
}

static PyGetSetDef timezone_getset[] = {
{"name", timezone_name, NULL,
PyDoc_STR("Name given when the timezone was created.")},
{NULL}
};

static PyTypeObject PyDateTime_TimeZoneType = {
PyVarObject_HEAD_INIT(NULL, 0)
"datetime.timezone", /* tp_name */
Expand Down Expand Up @@ -4588,7 +4604,7 @@ static PyTypeObject PyDateTime_TimeZoneType = {
0, /* tp_iternext */
timezone_methods, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
timezone_getset, /* tp_getset */
&PyDateTime_TZInfoType, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
Expand Down
Loading