Closes Issue#22241: timezone.utc name is now plain 'UTC', not 'UTC-00:00'.

This commit is contained in:
Alexander Belopolsky 2015-09-06 13:07:21 -04:00
parent 40dc328cc2
commit 7827a5b7c2
5 changed files with 23 additions and 8 deletions

View File

@ -1734,10 +1734,7 @@ made to civil time.
otherwise :exc:`ValueError` is raised.
The *name* argument is optional. If specified it must be a string that
is used as the value returned by the ``tzname(dt)`` method. Otherwise,
``tzname(dt)`` returns a string 'UTCsHH:MM', where s is the sign of
*offset*, HH and MM are two digits of ``offset.hours`` and
``offset.minutes`` respectively.
will be used as the value returned by the :meth:`datetime.tzname` method.
.. versionadded:: 3.2
@ -1750,11 +1747,19 @@ made to civil time.
.. method:: timezone.tzname(dt)
Return the fixed value specified when the :class:`timezone` instance is
constructed or a string 'UTCsHH:MM', where s is the sign of
*offset*, HH and MM are two digits of ``offset.hours`` and
Return the fixed value specified when the :class:`timezone` instance
is constructed. If *name* is not provided in the constructor, the
name returned by ``tzname(dt)`` is generated from the value of the
``offset`` as follows. If *offset* is ``timedelta(0)``, the name
is "UTC", otherwise it is a string 'UTC±HH:MM', where ± is the sign
of ``offset``, HH and MM are two digits of ``offset.hours`` and
``offset.minutes`` respectively.
.. versionchanged:: 3.6
Name generated from ``offset=timedelta(0)`` is now plain 'UTC', not
'UTC+00:00'.
.. method:: timezone.dst(dt)
Always returns ``None``.

View File

@ -1920,6 +1920,8 @@ class timezone(tzinfo):
@staticmethod
def _name_from_offset(delta):
if not delta:
return 'UTC'
if delta < timedelta(0):
sign = '-'
delta = -delta

View File

@ -258,7 +258,8 @@ class TestTimeZone(unittest.TestCase):
with self.assertRaises(TypeError): self.EST.dst(5)
def test_tzname(self):
self.assertEqual('UTC+00:00', timezone(ZERO).tzname(None))
self.assertEqual('UTC', timezone.utc.tzname(None))
self.assertEqual('UTC', timezone(ZERO).tzname(None))
self.assertEqual('UTC-05:00', timezone(-5 * HOUR).tzname(None))
self.assertEqual('UTC+09:30', timezone(9.5 * HOUR).tzname(None))
self.assertEqual('UTC-00:01', timezone(timedelta(minutes=-1)).tzname(None))

View File

@ -17,6 +17,8 @@ Core and Builtins
Library
-------
- Issue #22241: timezone.utc name is now plain 'UTC', not 'UTC-00:00'.
- Issue #23517: fromtimestamp() and utcfromtimestamp() methods of
datetime.datetime now round microseconds to nearest with ties going away from
zero (ROUND_HALF_UP), as Python 2 and Python older than 3.3, instead of

View File

@ -3267,6 +3267,11 @@ timezone_str(PyDateTime_TimeZone *self)
Py_INCREF(self->name);
return self->name;
}
if (self == PyDateTime_TimeZone_UTC ||
(GET_TD_DAYS(self->offset) == 0 &&
GET_TD_SECONDS(self->offset) == 0 &&
GET_TD_MICROSECONDS(self->offset) == 0))
return PyUnicode_FromString("UTC");
/* Offset is normalized, so it is negative if days < 0 */
if (GET_TD_DAYS(self->offset) < 0) {
sign = '-';