Skip to content

Commit 9eb701f

Browse files
[3.13] gh-86165: Fix Time2Internaldate with datetime timetuple (GH-151844) (GH-152897)
(cherry picked from commit 460fe13) Co-authored-by: Xiao Yuan <yuanx749@gmail.com>
1 parent 822d63c commit 9eb701f

3 files changed

Lines changed: 14 additions & 3 deletions

File tree

Lib/imaplib.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,9 +1527,8 @@ def Time2Internaldate(date_time):
15271527
dt = datetime.fromtimestamp(date_time,
15281528
timezone.utc).astimezone()
15291529
elif isinstance(date_time, tuple):
1530-
try:
1531-
gmtoff = date_time.tm_gmtoff
1532-
except AttributeError:
1530+
gmtoff = getattr(date_time, "tm_gmtoff", None)
1531+
if gmtoff is None:
15331532
if time.daylight:
15341533
dst = date_time[8]
15351534
if dst == -1:

Lib/test/test_imaplib.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,15 @@ def test_Time2Internaldate(self):
143143
internal = imaplib.Time2Internaldate(t)
144144
self.assertEqual(internal, expected)
145145

146+
@run_with_tz('STD-1DST,M3.2.0,M11.1.0')
147+
def test_Time2Internaldate_datetime_timetuple(self):
148+
date_time = datetime.fromtimestamp(2000000000).timetuple()
149+
self.assertIsNone(date_time.tm_gmtoff)
150+
self.assertEqual(
151+
imaplib.Time2Internaldate(date_time),
152+
'"18-May-2033 05:33:20 +0200"',
153+
)
154+
146155
def test_that_Time2Internaldate_returns_a_result(self):
147156
# Without tzset, we can check only that it successfully
148157
# produces a result, not the correctness of the result itself,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix :func:`imaplib.Time2Internaldate` to use the local timezone offset for
2+
``time.struct_time`` values with ``tm_gmtoff`` set to ``None``, as returned by
3+
``datetime.datetime.timetuple()``. Contributed by Xiao Yuan.

0 commit comments

Comments
 (0)