]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-86165: Fix Time2Internaldate with datetime timetuple (GH-151844)
authorXiao Yuan <yuanx749@gmail.com>
Thu, 2 Jul 2026 15:00:20 +0000 (18:00 +0300)
committerGitHub <noreply@github.com>
Thu, 2 Jul 2026 15:00:20 +0000 (15:00 +0000)
Lib/imaplib.py
Lib/test/test_imaplib.py
Misc/NEWS.d/next/Library/2026-06-21-15-50-24.gh-issue-86165.dyac1G.rst [new file with mode: 0644]

index 799c9dd529c7d9abf21d5f300ebb1d250d60f978..70abfa741444c30b88681646eac6f62b0400017c 100644 (file)
@@ -1869,9 +1869,8 @@ def Time2Internaldate(date_time):
         dt = datetime.fromtimestamp(date_time,
                                     timezone.utc).astimezone()
     elif isinstance(date_time, tuple):
-        try:
-            gmtoff = date_time.tm_gmtoff
-        except AttributeError:
+        gmtoff = getattr(date_time, "tm_gmtoff", None)
+        if gmtoff is None:
             if time.daylight:
                 dst = date_time[8]
                 if dst == -1:
index c8dcf95be33b6330a2764b423c7453c5a29b9555..097056c91a7f89e7ce6050022f26c44bb95f36fb 100644 (file)
@@ -143,6 +143,15 @@ class TestImaplib(unittest.TestCase):
             internal = imaplib.Time2Internaldate(t)
             self.assertEqual(internal, expected)
 
+    @run_with_tz('STD-1DST,M3.2.0,M11.1.0')
+    def test_Time2Internaldate_datetime_timetuple(self):
+        date_time = datetime.fromtimestamp(2000000000).timetuple()
+        self.assertIsNone(date_time.tm_gmtoff)
+        self.assertEqual(
+            imaplib.Time2Internaldate(date_time),
+            '"18-May-2033 05:33:20 +0200"',
+        )
+
     def test_that_Time2Internaldate_returns_a_result(self):
         # Without tzset, we can check only that it successfully
         # produces a result, not the correctness of the result itself,
diff --git a/Misc/NEWS.d/next/Library/2026-06-21-15-50-24.gh-issue-86165.dyac1G.rst b/Misc/NEWS.d/next/Library/2026-06-21-15-50-24.gh-issue-86165.dyac1G.rst
new file mode 100644 (file)
index 0000000..0b630e9
--- /dev/null
@@ -0,0 +1,3 @@
+Fix :func:`imaplib.Time2Internaldate` to use the local timezone offset for
+``time.struct_time`` values with ``tm_gmtoff`` set to ``None``, as returned by
+``datetime.datetime.timetuple()``. Contributed by Xiao Yuan.