From 64a3fd6c39a7c0096d9e5984156e0153ed09a7ee Mon Sep 17 00:00:00 2001 From: Xiao Yuan Date: Thu, 2 Jul 2026 18:00:20 +0300 Subject: [PATCH] gh-86165: Fix Time2Internaldate with datetime timetuple (GH-151844) (cherry picked from commit 460fe13601d1a71f4700f6685e0f4d01d1ef0d6b) Co-authored-by: Xiao Yuan --- Lib/imaplib.py | 5 ++--- Lib/test/test_imaplib.py | 9 +++++++++ .../2026-06-21-15-50-24.gh-issue-86165.dyac1G.rst | 3 +++ 3 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-06-21-15-50-24.gh-issue-86165.dyac1G.rst diff --git a/Lib/imaplib.py b/Lib/imaplib.py index 2041a083f0bc0e..31d017041e4b39 100644 --- a/Lib/imaplib.py +++ b/Lib/imaplib.py @@ -1854,9 +1854,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: diff --git a/Lib/test/test_imaplib.py b/Lib/test/test_imaplib.py index f0875d39397b60..3f4870db9b83bc 100644 --- a/Lib/test/test_imaplib.py +++ b/Lib/test/test_imaplib.py @@ -143,6 +143,15 @@ def test_Time2Internaldate(self): 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 index 00000000000000..0b630e9ae34683 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-06-21-15-50-24.gh-issue-86165.dyac1G.rst @@ -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.