]> granicus.if.org Git - python/commitdiff
Closes Issue#22241: timezone.utc name is now plain 'UTC', not 'UTC-00:00'.
authorAlexander Belopolsky <alexander.belopolsky@gmail.com>
Sun, 6 Sep 2015 17:07:21 +0000 (13:07 -0400)
committerAlexander Belopolsky <alexander.belopolsky@gmail.com>
Sun, 6 Sep 2015 17:07:21 +0000 (13:07 -0400)
Doc/library/datetime.rst
Lib/datetime.py
Lib/test/datetimetester.py
Misc/NEWS
Modules/_datetimemodule.c

index 976cd4900031f5847ef32f85665828762cc4768e..9a0f93656d92eaaab1a068b3326881d07f6d9d46 100644 (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``.
index 3bf9edc5adaeb50f115f73b5cf88abe8f358dbca..6b2ac33a32782cf7c6eb2747c2fe266d77ba7b25 100644 (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
index b8c9b0db94d9af01c037fa47d21b3e6f205b5f8f..f23add35db2588c7d87641cc3af0a70078600a19 100644 (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))
index e88106ae30f6f690b5b7ae4fb33f9072ef44a452..096ab67aa526a0f8c3a8b0a962f6cfdccd5cea66 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -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
index ae459df8a98376205d590f745a8d6243f2ccc5aa..008b733bd3cba07f4de0aec3ed2af839993194f3 100644 (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 = '-';