[Python-checkins] gh-91928: Add `datetime.UTC` alias for `datetime.timezone.utc` (GH-91973)

miss-islington webhook-mailer at python.org
Tue May 3 18:14:30 EDT 2022


https://github.com/python/cpython/commit/48c6165c28dfb40eafd2fa6de9bebd14fbc7c95c
commit: 48c6165c28dfb40eafd2fa6de9bebd14fbc7c95c
branch: main
author: Kabir Kwatra <kabir at kwatra.me>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2022-05-03T15:14:25-07:00
summary:

gh-91928: Add `datetime.UTC` alias for `datetime.timezone.utc` (GH-91973)



### fixes #91928

`UTC` is now module attribute aliased to `datetime.timezone.utc`.
You can now do the following:
```python
from datetime import UTC
```

files:
A Misc/NEWS.d/next/Library/2022-04-26-18-02-44.gh-issue-91928.V0YveU.rst
M Doc/library/datetime.rst
M Lib/datetime.py
M Lib/test/datetimetester.py
M Misc/ACKS
M Modules/_datetimemodule.c

diff --git a/Doc/library/datetime.rst b/Doc/library/datetime.rst
index f447b7bc9491e..ca17dc880cfb3 100644
--- a/Doc/library/datetime.rst
+++ b/Doc/library/datetime.rst
@@ -84,6 +84,12 @@ The :mod:`datetime` module exports the following constants:
    The largest year number allowed in a :class:`date` or :class:`.datetime` object.
    :const:`MAXYEAR` is ``9999``.
 
+.. attribute:: UTC
+
+   Alias for the UTC timezone singleton :attr:`datetime.timezone.utc`.
+
+   .. versionadded:: 3.11
+
 Available Types
 ---------------
 
diff --git a/Lib/datetime.py b/Lib/datetime.py
index 260b1de38877a..7f79aa436eb5e 100644
--- a/Lib/datetime.py
+++ b/Lib/datetime.py
@@ -5,7 +5,7 @@
 """
 
 __all__ = ("date", "datetime", "time", "timedelta", "timezone", "tzinfo",
-           "MINYEAR", "MAXYEAR")
+           "MINYEAR", "MAXYEAR", "UTC")
 
 
 import time as _time
@@ -2290,7 +2290,8 @@ def _name_from_offset(delta):
             return f'UTC{sign}{hours:02d}:{minutes:02d}:{seconds:02d}'
         return f'UTC{sign}{hours:02d}:{minutes:02d}'
 
-timezone.utc = timezone._create(timedelta(0))
+UTC = timezone.utc = timezone._create(timedelta(0))
+
 # bpo-37642: These attributes are rounded to the nearest minute for backwards
 # compatibility, even though the constructor will accept a wider range of
 # values. This may change in the future.
diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py
index 335cded3b5fac..d85b5466f7fc2 100644
--- a/Lib/test/datetimetester.py
+++ b/Lib/test/datetimetester.py
@@ -28,6 +28,7 @@
 from datetime import tzinfo
 from datetime import time
 from datetime import timezone
+from datetime import UTC
 from datetime import date, datetime
 import time as _time
 
@@ -66,6 +67,9 @@ def test_constants(self):
         self.assertEqual(datetime.MINYEAR, 1)
         self.assertEqual(datetime.MAXYEAR, 9999)
 
+    def test_utc_alias(self):
+        self.assertIs(UTC, timezone.utc)
+
     def test_all(self):
         """Test that __all__ only points to valid attributes."""
         all_attrs = dir(datetime_module)
@@ -81,7 +85,7 @@ def test_name_cleanup(self):
                     if not name.startswith('__') and not name.endswith('__'))
         allowed = set(['MAXYEAR', 'MINYEAR', 'date', 'datetime',
                        'datetime_CAPI', 'time', 'timedelta', 'timezone',
-                       'tzinfo', 'sys'])
+                       'tzinfo', 'UTC', 'sys'])
         self.assertEqual(names - allowed, set([]))
 
     def test_divide_and_round(self):
@@ -310,6 +314,7 @@ def test_dst(self):
 
     def test_tzname(self):
         self.assertEqual('UTC', timezone.utc.tzname(None))
+        self.assertEqual('UTC', 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))
diff --git a/Misc/ACKS b/Misc/ACKS
index 1efc6a07c6cad..ec4de61ff9273 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -987,6 +987,7 @@ Toshio Kuratomi
 Ilia Kurenkov
 Vladimir Kushnir
 Erno Kuusela
+Kabir Kwatra
 Ross Lagerwall
 Cameron Laird
 Loïc Lajeanne
diff --git a/Misc/NEWS.d/next/Library/2022-04-26-18-02-44.gh-issue-91928.V0YveU.rst b/Misc/NEWS.d/next/Library/2022-04-26-18-02-44.gh-issue-91928.V0YveU.rst
new file mode 100644
index 0000000000000..35838c7e31649
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-04-26-18-02-44.gh-issue-91928.V0YveU.rst
@@ -0,0 +1,3 @@
+Add `datetime.UTC` alias for `datetime.timezone.utc`.
+
+Patch by Kabir Kwatra.
diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c
index 24c2198893a37..20cdb1822ab96 100644
--- a/Modules/_datetimemodule.c
+++ b/Modules/_datetimemodule.c
@@ -6634,6 +6634,10 @@ _datetime_exec(PyObject *module)
         return -1;
     }
 
+    if (PyModule_AddObjectRef(module, "UTC", PyDateTime_TimeZone_UTC) < 0) {
+        return -1;
+    }
+
     /* A 4-year cycle has an extra leap day over what we'd get from
      * pasting together 4 single years.
      */



More information about the Python-checkins mailing list