summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorKabir Kwatra <kabir@kwatra.me>2022-05-03 22:14:25 (GMT)
committerGitHub <noreply@github.com>2022-05-03 22:14:25 (GMT)
commit48c6165c28dfb40eafd2fa6de9bebd14fbc7c95c (patch)
tree877ad0238cb76142bbbc27318b7ed71fd72c56cc /Lib
parentee2205b208389611e8a278ac1bc74b34f4994fd2 (diff)
downloadcpython-48c6165c28dfb40eafd2fa6de9bebd14fbc7c95c.zip
cpython-48c6165c28dfb40eafd2fa6de9bebd14fbc7c95c.tar.gz
cpython-48c6165c28dfb40eafd2fa6de9bebd14fbc7c95c.tar.bz2
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 ```
Diffstat (limited to 'Lib')
-rw-r--r--Lib/datetime.py5
-rw-r--r--Lib/test/datetimetester.py7
2 files changed, 9 insertions, 3 deletions
diff --git a/Lib/datetime.py b/Lib/datetime.py
index 260b1de..7f79aa4 100644
--- a/Lib/datetime.py
+++ b/Lib/datetime.py
@@ -5,7 +5,7 @@ time zone and DST data sources.
"""
__all__ = ("date", "datetime", "time", "timedelta", "timezone", "tzinfo",
- "MINYEAR", "MAXYEAR")
+ "MINYEAR", "MAXYEAR", "UTC")
import time as _time
@@ -2290,7 +2290,8 @@ class timezone(tzinfo):
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 335cded..d85b546 100644
--- a/Lib/test/datetimetester.py
+++ b/Lib/test/datetimetester.py
@@ -28,6 +28,7 @@ from datetime import timedelta
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 @@ class TestModule(unittest.TestCase):
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 @@ class TestModule(unittest.TestCase):
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 @@ class TestTimeZone(unittest.TestCase):
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))