[Python-checkins] cpython: Closes Issue#22241: timezone.utc name is now plain 'UTC', not 'UTC-00:00'.
alexander.belopolsky
python-checkins at python.org
Sun Sep 6 19:07:33 CEST 2015
https://hg.python.org/cpython/rev/f904b7eb3981
changeset: 97709:f904b7eb3981
user: Alexander Belopolsky <alexander.belopolsky at gmail.com>
date: Sun Sep 06 13:07:21 2015 -0400
summary:
Closes Issue#22241: timezone.utc name is now plain 'UTC', not 'UTC-00:00'.
files:
Doc/library/datetime.rst | 19 ++++++++++++-------
Lib/datetime.py | 2 ++
Lib/test/datetimetester.py | 3 ++-
Misc/NEWS | 2 ++
Modules/_datetimemodule.c | 5 +++++
5 files changed, 23 insertions(+), 8 deletions(-)
diff --git a/Doc/library/datetime.rst b/Doc/library/datetime.rst
--- a/Doc/library/datetime.rst
+++ b/Doc/library/datetime.rst
@@ -1734,10 +1734,7 @@
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 @@
.. 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``.
diff --git a/Lib/datetime.py b/Lib/datetime.py
--- a/Lib/datetime.py
+++ b/Lib/datetime.py
@@ -1920,6 +1920,8 @@
@staticmethod
def _name_from_offset(delta):
+ if not delta:
+ return 'UTC'
if delta < timedelta(0):
sign = '-'
delta = -delta
diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py
--- a/Lib/test/datetimetester.py
+++ b/Lib/test/datetimetester.py
@@ -258,7 +258,8 @@
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))
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -17,6 +17,8 @@
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
diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c
--- a/Modules/_datetimemodule.c
+++ b/Modules/_datetimemodule.c
@@ -3267,6 +3267,11 @@
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 = '-';
--
Repository URL: https://hg.python.org/cpython
More information about the Python-checkins
mailing list