Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2420d83

Browse files
committed
Issue #10941: Fix imaplib.Internaldate2tuple to produce correct result near
the DST transition. Patch by Joe Peterson.
1 parent ea7e9f9 commit 2420d83

5 files changed

Lines changed: 45 additions & 15 deletions

File tree

Lib/imaplib.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
__version__ = "2.58"
2424

25-
import binascii, errno, random, re, socket, subprocess, sys, time
25+
import binascii, errno, random, re, socket, subprocess, sys, time, calendar
2626

2727
try:
2828
import ssl
@@ -1340,19 +1340,9 @@ def Internaldate2tuple(resp):
13401340
zone = -zone
13411341

13421342
tt = (year, mon, day, hour, min, sec, -1, -1, -1)
1343+
utc = calendar.timegm(tt) - zone
13431344

1344-
utc = time.mktime(tt)
1345-
1346-
# Following is necessary because the time module has no 'mkgmtime'.
1347-
# 'mktime' assumes arg in local timezone, so adds timezone/altzone.
1348-
1349-
lt = time.localtime(utc)
1350-
if time.daylight and lt[-1]:
1351-
zone = zone + time.altzone
1352-
else:
1353-
zone = zone + time.timezone
1354-
1355-
return time.localtime(utc - zone)
1345+
return time.localtime(utc)
13561346

13571347

13581348

Lib/test/support.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"reap_children", "cpython_only", "check_impl_detail", "get_attribute",
5454
"swap_item", "swap_attr", "requires_IEEE_754",
5555
"TestHandler", "Matcher", "can_symlink", "skip_unless_symlink",
56-
"import_fresh_module", "failfast",
56+
"import_fresh_module", "failfast", "run_with_tz"
5757
]
5858

5959
class Error(Exception):
@@ -1020,6 +1020,35 @@ def inner(*args, **kwds):
10201020
return inner
10211021
return decorator
10221022

1023+
#=======================================================================
1024+
# Decorator for running a function in a specific timezone, correctly
1025+
# resetting it afterwards.
1026+
1027+
def run_with_tz(tz):
1028+
def decorator(func):
1029+
def inner(*args, **kwds):
1030+
if 'TZ' in os.environ:
1031+
orig_tz = os.environ['TZ']
1032+
else:
1033+
orig_tz = None
1034+
os.environ['TZ'] = tz
1035+
time.tzset()
1036+
1037+
# now run the function, resetting the tz on exceptions
1038+
try:
1039+
return func(*args, **kwds)
1040+
finally:
1041+
if orig_tz == None:
1042+
del os.environ['TZ']
1043+
else:
1044+
os.environ['TZ'] = orig_tz
1045+
time.tzset()
1046+
1047+
inner.__name__ = func.__name__
1048+
inner.__doc__ = func.__doc__
1049+
return inner
1050+
return decorator
1051+
10231052
#=======================================================================
10241053
# Big-memory-test support. Separate from 'resources' because memory use
10251054
# should be configurable.

Lib/test/test_imaplib.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import time
1212
import calendar
1313

14-
from test.support import reap_threads, verbose, transient_internet
14+
from test.support import reap_threads, verbose, transient_internet, run_with_tz
1515
import unittest
1616

1717
try:
@@ -36,6 +36,13 @@ def test_Internaldate2tuple(self):
3636
b'25 (INTERNALDATE "31-Dec-1999 12:30:00 -1130")')
3737
self.assertEqual(time.mktime(tt), t0)
3838

39+
@run_with_tz('MST+07MDT,M4.1.0,M10.5.0')
40+
def test_Internaldate2tuple_issue10941(self):
41+
self.assertNotEqual(imaplib.Internaldate2tuple(
42+
b'25 (INTERNALDATE "02-Apr-2000 02:30:00 +0000")'),
43+
imaplib.Internaldate2tuple(
44+
b'25 (INTERNALDATE "02-Apr-2000 03:30:00 +0000")'))
45+
3946
def test_that_Time2Internaldate_returns_a_result(self):
4047
# We can check only that it successfully produces a result,
4148
# not the correctness of the result itself, since the result

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,7 @@ Peter Parente
702702
Alexandre Parenteau
703703
Dan Parisien
704704
Harri Pasanen
705+
Joe Peterson
705706
Randy Pausch
706707
Samuele Pedroni
707708
Marcel van der Peijl

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ Core and Builtins
5656
Library
5757
-------
5858

59+
- Issue #10941: Fix imaplib.Internaldate2tuple to produce correct result near
60+
the DST transition. Patch by Joe Peterson.
61+
5962
- Issue #9154: Fix parser module to understand function annotations.
6063

6164
- Issue #14664: It is now possible to use @unittest.skip{If,Unless} on a

0 commit comments

Comments
 (0)