]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-83861: Fix datetime.astimezone() method (GH-101545)
authorAlexander Belopolsky <abalkin@users.noreply.github.com>
Wed, 19 Apr 2023 21:02:29 +0000 (17:02 -0400)
committerGitHub <noreply@github.com>
Wed, 19 Apr 2023 21:02:29 +0000 (14:02 -0700)
Lib/datetime.py
Lib/test/datetimetester.py
Misc/NEWS.d/next/Library/2023-02-06-16-45-18.gh-issue-83861.mMbIU3.rst [new file with mode: 0644]
Modules/_datetimemodule.c

index 637144637485bcdd5ba3770e923c05575dc25795..09a2d2d5381c34132cae6761e288cac4d64b6cb5 100644 (file)
@@ -1965,6 +1965,11 @@ class datetime(date):
     def _local_timezone(self):
         if self.tzinfo is None:
             ts = self._mktime()
+            # Detect gap
+            ts2 = self.replace(fold=1-self.fold)._mktime()
+            if ts2 != ts: # This happens in a gap or a fold
+                if (ts2 > ts) == self.fold:
+                    ts = ts2
         else:
             ts = (self - _EPOCH) // timedelta(seconds=1)
         localtm = _time.localtime(ts)
index 570f803918c1ef52e01cadc159d6025f58e4fe6d..477f16f1841f6260eb668d43b13feef9b4285f14 100644 (file)
@@ -6212,6 +6212,10 @@ class ZoneInfoTest(unittest.TestCase):
                     ts1 = dt.replace(fold=1).timestamp()
                     self.assertEqual(ts0, s0 + ss / 2)
                     self.assertEqual(ts1, s0 - ss / 2)
+                    # gh-83861
+                    utc0 = dt.astimezone(timezone.utc)
+                    utc1 = dt.replace(fold=1).astimezone(timezone.utc)
+                    self.assertEqual(utc0, utc1 + timedelta(0, ss))
         finally:
             if TZ is None:
                 del os.environ['TZ']
diff --git a/Misc/NEWS.d/next/Library/2023-02-06-16-45-18.gh-issue-83861.mMbIU3.rst b/Misc/NEWS.d/next/Library/2023-02-06-16-45-18.gh-issue-83861.mMbIU3.rst
new file mode 100644 (file)
index 0000000..e85e7a4
--- /dev/null
@@ -0,0 +1,4 @@
+Fix datetime.astimezone method return value when invoked on a naive datetime
+instance that represents local time falling in a timezone transition gap.
+PEP 495 requires that instances with fold=1 produce earlier times than those
+with fold=0 in this case.
index eda8c5610ba659e608d1f4a9988835a169e71fa5..f317dc14e15bf1ee09d69a0d93cabaf97752922a 100644 (file)
@@ -6153,17 +6153,31 @@ local_to_seconds(int year, int month, int day,
 static PyObject *
 local_timezone_from_local(PyDateTime_DateTime *local_dt)
 {
-    long long seconds;
+    long long seconds, seconds2;
     time_t timestamp;
+    int fold = DATE_GET_FOLD(local_dt);
     seconds = local_to_seconds(GET_YEAR(local_dt),
                                GET_MONTH(local_dt),
                                GET_DAY(local_dt),
                                DATE_GET_HOUR(local_dt),
                                DATE_GET_MINUTE(local_dt),
                                DATE_GET_SECOND(local_dt),
-                               DATE_GET_FOLD(local_dt));
+                               fold);
     if (seconds == -1)
         return NULL;
+    seconds2 = local_to_seconds(GET_YEAR(local_dt),
+                                GET_MONTH(local_dt),
+                                GET_DAY(local_dt),
+                                DATE_GET_HOUR(local_dt),
+                                DATE_GET_MINUTE(local_dt),
+                                DATE_GET_SECOND(local_dt),
+                                !fold);
+    if (seconds2 == -1)
+        return NULL;
+    /* Detect gap */
+    if (seconds2 != seconds && (seconds2 > seconds) == fold)
+        seconds = seconds2;
+
     /* XXX: add bounds check */
     timestamp = seconds - epoch;
     return local_timezone_from_timestamp(timestamp);