Skip to content

Commit 460fe13

Browse files
authored
gh-86165: Fix Time2Internaldate with datetime timetuple (GH-151844)
1 parent 31a0e09 commit 460fe13

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
@@ -1869,9 +1869,8 @@ def Time2Internaldate(date_time):
18691869
dt = datetime.fromtimestamp(date_time,
18701870
timezone.utc).astimezone()
18711871
elif isinstance(date_time, tuple):
1872-
try:
1873-
gmtoff = date_time.tm_gmtoff
1874-
except AttributeError:
1872+
gmtoff = getattr(date_time, "tm_gmtoff", None)
1873+
if gmtoff is None:
18751874
if time.daylight:
18761875
dst = date_time[8]
18771876
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)