Skip to content

Commit 7827a5b

Browse files
committed
Closes Issue#22241: timezone.utc name is now plain 'UTC', not 'UTC-00:00'.
1 parent 40dc328 commit 7827a5b

5 files changed

Lines changed: 23 additions & 8 deletions

File tree

‎Doc/library/datetime.rst‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1734,10 +1734,7 @@ made to civil time.
17341734
otherwise :exc:`ValueError` is raised.
17351735

17361736
The *name* argument is optional. If specified it must be a string that
1737-
is used as the value returned by the ``tzname(dt)`` method. Otherwise,
1738-
``tzname(dt)`` returns a string 'UTCsHH:MM', where s is the sign of
1739-
*offset*, HH and MM are two digits of ``offset.hours`` and
1740-
``offset.minutes`` respectively.
1737+
will be used as the value returned by the :meth:`datetime.tzname` method.
17411738

17421739
.. versionadded:: 3.2
17431740

@@ -1750,11 +1747,19 @@ made to civil time.
17501747

17511748
.. method:: timezone.tzname(dt)
17521749

1753-
Return the fixed value specified when the :class:`timezone` instance is
1754-
constructed or a string 'UTCsHH:MM', where s is the sign of
1755-
*offset*, HH and MM are two digits of ``offset.hours`` and
1750+
Return the fixed value specified when the :class:`timezone` instance
1751+
is constructed. If *name* is not provided in the constructor, the
1752+
name returned by ``tzname(dt)`` is generated from the value of the
1753+
``offset`` as follows. If *offset* is ``timedelta(0)``, the name
1754+
is "UTC", otherwise it is a string 'UTC±HH:MM', where ± is the sign
1755+
of ``offset``, HH and MM are two digits of ``offset.hours`` and
17561756
``offset.minutes`` respectively.
17571757

1758+
.. versionchanged:: 3.6
1759+
Name generated from ``offset=timedelta(0)`` is now plain 'UTC', not
1760+
'UTC+00:00'.
1761+
1762+
17581763
.. method:: timezone.dst(dt)
17591764

17601765
Always returns ``None``.

‎Lib/datetime.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1920,6 +1920,8 @@ def fromutc(self, dt):
19201920

19211921
@staticmethod
19221922
def _name_from_offset(delta):
1923+
if not delta:
1924+
return 'UTC'
19231925
if delta < timedelta(0):
19241926
sign = '-'
19251927
delta = -delta

‎Lib/test/datetimetester.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,8 @@ def test_dst(self):
258258
with self.assertRaises(TypeError): self.EST.dst(5)
259259

260260
def test_tzname(self):
261-
self.assertEqual('UTC+00:00', timezone(ZERO).tzname(None))
261+
self.assertEqual('UTC', timezone.utc.tzname(None))
262+
self.assertEqual('UTC', timezone(ZERO).tzname(None))
262263
self.assertEqual('UTC-05:00', timezone(-5 * HOUR).tzname(None))
263264
self.assertEqual('UTC+09:30', timezone(9.5 * HOUR).tzname(None))
264265
self.assertEqual('UTC-00:01', timezone(timedelta(minutes=-1)).tzname(None))

‎Misc/NEWS‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Core and Builtins
1717
Library
1818
-------
1919

20+
- Issue #22241: timezone.utc name is now plain 'UTC', not 'UTC-00:00'.
21+
2022
- Issue #23517: fromtimestamp() and utcfromtimestamp() methods of
2123
datetime.datetime now round microseconds to nearest with ties going away from
2224
zero (ROUND_HALF_UP), as Python 2 and Python older than 3.3, instead of

‎Modules/_datetimemodule.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3267,6 +3267,11 @@ timezone_str(PyDateTime_TimeZone *self)
32673267
Py_INCREF(self->name);
32683268
return self->name;
32693269
}
3270+
if (self == PyDateTime_TimeZone_UTC ||
3271+
(GET_TD_DAYS(self->offset) == 0 &&
3272+
GET_TD_SECONDS(self->offset) == 0 &&
3273+
GET_TD_MICROSECONDS(self->offset) == 0))
3274+
return PyUnicode_FromString("UTC");
32703275
/* Offset is normalized, so it is negative if days < 0 */
32713276
if (GET_TD_DAYS(self->offset) < 0) {
32723277
sign = '-';

0 commit comments

Comments
 (0)