From 65fbe0d2f7c60fc3911fc3d45b1e9a2119dbc6ff Mon Sep 17 00:00:00 2001 From: Kabir Kwatra Date: Tue, 26 Apr 2022 17:50:09 -0700 Subject: [PATCH 1/8] feat(datetime): Add `datetime.UTC` alias for `datetime.timezone.utc` fixes #91928 --- Doc/library/datetime.rst | 5 +++++ Lib/datetime.py | 4 ++-- Lib/test/datetimetester.py | 4 +++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/Doc/library/datetime.rst b/Doc/library/datetime.rst index f447b7bc9491e4..df9047a184f8f9 100644 --- a/Doc/library/datetime.rst +++ b/Doc/library/datetime.rst @@ -84,6 +84,11 @@ 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, ``datetime.utc``. + + Available Types --------------- diff --git a/Lib/datetime.py b/Lib/datetime.py index 260b1de38877a6..224d94cda7b0e2 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,7 @@ 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 335cded3b5fac6..9fe2b9b3b4930d 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 @@ -81,7 +82,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 +311,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)) From ce049854a6c71ea22f5ff68de0ffc333934efe3e Mon Sep 17 00:00:00 2001 From: Kabir Kwatra Date: Tue, 26 Apr 2022 18:03:47 -0700 Subject: [PATCH 2/8] docs: Add NEWS entry for #91973 --- Doc/library/datetime.rst | 1 - .../next/Library/2022-04-26-18-02-44.gh-issue-91928.V0YveU.rst | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2022-04-26-18-02-44.gh-issue-91928.V0YveU.rst diff --git a/Doc/library/datetime.rst b/Doc/library/datetime.rst index df9047a184f8f9..9b8309479ffd6b 100644 --- a/Doc/library/datetime.rst +++ b/Doc/library/datetime.rst @@ -88,7 +88,6 @@ The :mod:`datetime` module exports the following constants: alias for the UTC timezone, ``datetime.utc``. - Available Types --------------- 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 00000000000000..9e185bcf88d326 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-04-26-18-02-44.gh-issue-91928.V0YveU.rst @@ -0,0 +1 @@ +Add `datetime.UTC` alias for `datetime.timezone.utc`. From 28f228b5302f03aa5560206f82433d6d86faa141 Mon Sep 17 00:00:00 2001 From: Kabir Kwatra Date: Wed, 27 Apr 2022 12:00:35 -0700 Subject: [PATCH 3/8] docs: add contribution acknowledgement --- Misc/ACKS | 1 + .../next/Library/2022-04-26-18-02-44.gh-issue-91928.V0YveU.rst | 2 ++ 2 files changed, 3 insertions(+) diff --git a/Misc/ACKS b/Misc/ACKS index 1efc6a07c6cada..ec4de61ff9273e 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 index 9e185bcf88d326..35838c7e31649b 100644 --- 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 @@ -1 +1,3 @@ Add `datetime.UTC` alias for `datetime.timezone.utc`. + +Patch by Kabir Kwatra. From cefda335b5564382feba320ea6c0937979cf9e97 Mon Sep 17 00:00:00 2001 From: Kabir Kwatra Date: Wed, 27 Apr 2022 12:01:48 -0700 Subject: [PATCH 4/8] docs(datetime): clarify `UTC` timezone singleton --- Doc/library/datetime.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/datetime.rst b/Doc/library/datetime.rst index 9b8309479ffd6b..efcef320c30ad2 100644 --- a/Doc/library/datetime.rst +++ b/Doc/library/datetime.rst @@ -86,7 +86,7 @@ The :mod:`datetime` module exports the following constants: .. attribute:: UTC - alias for the UTC timezone, ``datetime.utc``. + Alias for the UTC timezone singleton :attr:`datetime.timezone.utc`. Available Types --------------- From 1e8257d4015d5199223a1f6590e90e527a8656ff Mon Sep 17 00:00:00 2001 From: Kabir Kwatra Date: Wed, 27 Apr 2022 15:13:14 -0700 Subject: [PATCH 5/8] docs(datetime): add versionadded to `datetime.UTC` --- Doc/library/datetime.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/Doc/library/datetime.rst b/Doc/library/datetime.rst index efcef320c30ad2..9b9aa71d139d3a 100644 --- a/Doc/library/datetime.rst +++ b/Doc/library/datetime.rst @@ -84,6 +84,7 @@ 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``. +.. versionadded:: 3.11 .. attribute:: UTC Alias for the UTC timezone singleton :attr:`datetime.timezone.utc`. From 7335f0af832fdaa9af84a5e43c691b6c029506d1 Mon Sep 17 00:00:00 2001 From: Kabir Kwatra Date: Wed, 27 Apr 2022 15:13:52 -0700 Subject: [PATCH 6/8] test(datetime): add test for `datetime.UTC` alias --- Lib/test/datetimetester.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Lib/test/datetimetester.py b/Lib/test/datetimetester.py index 9fe2b9b3b4930d..d85b5466f7fc28 100644 --- a/Lib/test/datetimetester.py +++ b/Lib/test/datetimetester.py @@ -67,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) From 185c7ac2321b78784faa62ded6b94ae5cf446183 Mon Sep 17 00:00:00 2001 From: Kabir Kwatra Date: Wed, 27 Apr 2022 15:14:39 -0700 Subject: [PATCH 7/8] fix(datetime): move `datetime.UTC` definition to end to ensure that it is an alias --- Lib/datetime.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Lib/datetime.py b/Lib/datetime.py index 224d94cda7b0e2..756e9ceb56dde2 100644 --- a/Lib/datetime.py +++ b/Lib/datetime.py @@ -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}' -UTC = timezone.utc = timezone._create(timedelta(0)) +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. @@ -2514,3 +2515,5 @@ def _name_from_offset(delta): # appropriate to maintain a single module level docstring and # remove the following line. from _datetime import __doc__ + +UTC = timezone.utc From 9542f545e08f4f0d208af9d4583988c56c543827 Mon Sep 17 00:00:00 2001 From: Paul Ganssle Date: Tue, 3 May 2022 14:00:31 -0600 Subject: [PATCH 8/8] Clean up nits 1. Add UTC definition into C implementation as well (just for consistency, there's no particular reason not to simply assign the alias in the Python code later). 2. Move `..versionadded` tag into the right place --- Doc/library/datetime.rst | 3 ++- Lib/datetime.py | 4 +--- Modules/_datetimemodule.c | 4 ++++ 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Doc/library/datetime.rst b/Doc/library/datetime.rst index 9b9aa71d139d3a..ca17dc880cfb34 100644 --- a/Doc/library/datetime.rst +++ b/Doc/library/datetime.rst @@ -84,11 +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``. -.. versionadded:: 3.11 .. 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 756e9ceb56dde2..7f79aa436eb5ea 100644 --- a/Lib/datetime.py +++ b/Lib/datetime.py @@ -2290,7 +2290,7 @@ 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 @@ -2515,5 +2515,3 @@ def _name_from_offset(delta): # appropriate to maintain a single module level docstring and # remove the following line. from _datetime import __doc__ - -UTC = timezone.utc diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c index 24c2198893a379..20cdb1822ab964 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. */