Index: Doc/library/imaplib.rst =================================================================== --- Doc/library/imaplib.rst (revision 88087) +++ Doc/library/imaplib.rst (working copy) @@ -85,8 +85,8 @@ .. function:: Internaldate2tuple(datestr) - Converts an IMAP4 INTERNALDATE string to Coordinated Universal Time. Returns a - :mod:`time` module tuple. + Converts an IMAP4 INTERNALDATE string to local time. + Returns a :mod:`time` module tuple. .. function:: Int2AP(num) @@ -102,7 +102,8 @@ .. function:: Time2Internaldate(date_time) - Converts a :mod:`time` module tuple to an IMAP4 ``INTERNALDATE`` representation. + Converts a :mod:`time` module tuple (local time) to an + IMAP4 ``INTERNALDATE`` representation. Returns a string in the form: ``"DD-Mmm-YYYY HH:MM:SS +HHMM"`` (including double-quotes). Index: Lib/imaplib.py =================================================================== --- Lib/imaplib.py (revision 88087) +++ Lib/imaplib.py (working copy) @@ -22,7 +22,7 @@ __version__ = "2.58" -import binascii, errno, random, re, socket, subprocess, sys, time +import binascii, errno, random, re, socket, subprocess, sys, time, calendar try: import ssl @@ -1308,15 +1308,18 @@ -Mon2num = {'Jan': 1, 'Feb': 2, 'Mar': 3, 'Apr': 4, 'May': 5, 'Jun': 6, - 'Jul': 7, 'Aug': 8, 'Sep': 9, 'Oct': 10, 'Nov': 11, 'Dec': 12} +Mon2num = {b'Jan': 1, b'Feb': 2, b'Mar': 3, b'Apr': 4, b'May': 5, b'Jun': 6, + b'Jul': 7, b'Aug': 8, b'Sep': 9, b'Oct': 10, b'Nov': 11, b'Dec': 12} def Internaldate2tuple(resp): - """Convert IMAP4 INTERNALDATE to UT. + """Convert IMAP4 INTERNALDATE to local time. Returns Python time module tuple. """ + if isinstance(resp, str): + resp = bytes(resp, 'ASCII') + mo = InternalDate.match(resp) if not mo: return None @@ -1335,26 +1338,16 @@ # INTERNALDATE timezone must be subtracted to get UT zone = (zoneh*60 + zonem)*60 - if zonen == '-': + if zonen == b'-': zone = -zone tt = (year, mon, day, hour, min, sec, -1, -1, -1) + utc = calendar.timegm(tt) - zone - utc = time.mktime(tt) + return time.localtime(utc) - # Following is necessary because the time module has no 'mkgmtime'. - # 'mktime' assumes arg in local timezone, so adds timezone/altzone. - lt = time.localtime(utc) - if time.daylight and lt[-1]: - zone = zone + time.altzone - else: - zone = zone + time.timezone - return time.localtime(utc - zone) - - - def Int2AP(num): """Convert integer to A-P string representation.""" @@ -1372,16 +1365,19 @@ """Convert IMAP4 flags response to python tuple.""" + if isinstance(resp, str): + resp = bytes(resp, 'ASCII') + mo = Flags.match(resp) if not mo: return () - return tuple(mo.group('flags').split()) + return tuple(str(x, 'ASCII') for x in mo.group('flags').split()) def Time2Internaldate(date_time): - """Convert 'date_time' to IMAP4 INTERNALDATE representation. + """Convert 'date_time' (local time) to IMAP4 INTERNALDATE representation. Return string in form: '"DD-Mmm-YYYY HH:MM:SS +HHMM"' """ Index: Lib/test/test_imaplib.py =================================================================== --- Lib/test/test_imaplib.py (revision 88087) +++ Lib/test/test_imaplib.py (working copy) @@ -23,6 +23,18 @@ class TestImaplib(unittest.TestCase): + def test_ParseFlags(self): + self.assertEqual(imaplib.ParseFlags('1 (FLAGS (\Answered \Seen))'), + ('\\Answered', '\\Seen')) + + def test_Internaldate2tuple(self): + # The following two should pruduce the same result. + # These dates were chosen because the latter appeared on the other + # side of a DST change in a previous implementation, causing it to + # be off by one hour. + self.assertEqual(imaplib.Internaldate2tuple('25 (INTERNALDATE "01-Apr-2000 19:02:23 -0700")')[0:6], (2000, 4, 1, 19, 2, 23)) + self.assertEqual(imaplib.Internaldate2tuple('101 (INTERNALDATE "02-Apr-2000 02:02:23 +0000")')[0:6], (2000, 4, 1, 19, 2, 23)) + def test_that_Time2Internaldate_returns_a_result(self): # We can check only that it successfully produces a result, # not the correctness of the result itself, since the result