Skip to content

Commit 48c6165

Browse files
authored
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 ```
1 parent ee2205b commit 48c6165

File tree

6 files changed

+23
-3
lines changed

6 files changed

+23
-3
lines changed

Doc/library/datetime.rst

+6
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ The :mod:`datetime` module exports the following constants:
8484
The largest year number allowed in a :class:`date` or :class:`.datetime` object.
8585
:const:`MAXYEAR` is ``9999``.
8686

87+
.. attribute:: UTC
88+
89+
Alias for the UTC timezone singleton :attr:`datetime.timezone.utc`.
90+
91+
.. versionadded:: 3.11
92+
8793
Available Types
8894
---------------
8995

Lib/datetime.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"""
66

77
__all__ = ("date", "datetime", "time", "timedelta", "timezone", "tzinfo",
8-
"MINYEAR", "MAXYEAR")
8+
"MINYEAR", "MAXYEAR", "UTC")
99

1010

1111
import time as _time
@@ -2290,7 +2290,8 @@ def _name_from_offset(delta):
22902290
return f'UTC{sign}{hours:02d}:{minutes:02d}:{seconds:02d}'
22912291
return f'UTC{sign}{hours:02d}:{minutes:02d}'
22922292

2293-
timezone.utc = timezone._create(timedelta(0))
2293+
UTC = timezone.utc = timezone._create(timedelta(0))
2294+
22942295
# bpo-37642: These attributes are rounded to the nearest minute for backwards
22952296
# compatibility, even though the constructor will accept a wider range of
22962297
# values. This may change in the future.

Lib/test/datetimetester.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from datetime import tzinfo
2929
from datetime import time
3030
from datetime import timezone
31+
from datetime import UTC
3132
from datetime import date, datetime
3233
import time as _time
3334

@@ -66,6 +67,9 @@ def test_constants(self):
6667
self.assertEqual(datetime.MINYEAR, 1)
6768
self.assertEqual(datetime.MAXYEAR, 9999)
6869

70+
def test_utc_alias(self):
71+
self.assertIs(UTC, timezone.utc)
72+
6973
def test_all(self):
7074
"""Test that __all__ only points to valid attributes."""
7175
all_attrs = dir(datetime_module)
@@ -81,7 +85,7 @@ def test_name_cleanup(self):
8185
if not name.startswith('__') and not name.endswith('__'))
8286
allowed = set(['MAXYEAR', 'MINYEAR', 'date', 'datetime',
8387
'datetime_CAPI', 'time', 'timedelta', 'timezone',
84-
'tzinfo', 'sys'])
88+
'tzinfo', 'UTC', 'sys'])
8589
self.assertEqual(names - allowed, set([]))
8690

8791
def test_divide_and_round(self):
@@ -310,6 +314,7 @@ def test_dst(self):
310314

311315
def test_tzname(self):
312316
self.assertEqual('UTC', timezone.utc.tzname(None))
317+
self.assertEqual('UTC', UTC.tzname(None))
313318
self.assertEqual('UTC', timezone(ZERO).tzname(None))
314319
self.assertEqual('UTC-05:00', timezone(-5 * HOUR).tzname(None))
315320
self.assertEqual('UTC+09:30', timezone(9.5 * HOUR).tzname(None))

Misc/ACKS

+1
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,7 @@ Toshio Kuratomi
987987
Ilia Kurenkov
988988
Vladimir Kushnir
989989
Erno Kuusela
990+
Kabir Kwatra
990991
Ross Lagerwall
991992
Cameron Laird
992993
Loïc Lajeanne
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Add `datetime.UTC` alias for `datetime.timezone.utc`.
2+
3+
Patch by Kabir Kwatra.

Modules/_datetimemodule.c

+4
Original file line numberDiff line numberDiff line change
@@ -6634,6 +6634,10 @@ _datetime_exec(PyObject *module)
66346634
return -1;
66356635
}
66366636

6637+
if (PyModule_AddObjectRef(module, "UTC", PyDateTime_TimeZone_UTC) < 0) {
6638+
return -1;
6639+
}
6640+
66376641
/* A 4-year cycle has an extra leap day over what we'd get from
66386642
* pasting together 4 single years.
66396643
*/

0 commit comments

Comments
 (0)