imaplib function bug?

C

Colin Brown

The Python 2.3 documentation in imaplib says:
Internaldate2tuple( datestr)
Converts an IMAP4 INTERNALDATE string to Coordinated Universal Time.
Returns a time module tuple.

Time2Internaldate( date_time)
Converts a time module tuple to an IMAP4 "INTERNALDATE" representation.
Returns a string in the form: "DD-Mmm-YYYY HH:MM:SS +HHMM" (including
double-quotes).
Yet running the following code produces inconsistent results (returning
local not UTC time for Internaldate2tuple):

import imaplib, time
tmtup = time.localtime()
imaptm = imaplib.Time2Internaldate(tmtup)
print 'local_time:',tmtup
print 'local_time:',imaptm
utc = imaplib.Internaldate2tuple('INTERNALDATE '+imaptm)
print 'utc_time :',utc

C:\>python imapbug.py
local_time: (2004, 9, 24, 10, 52, 59, 4, 268, 0)
local_time: "24-Sep-2004 10:52:59 +1200"
utc_time : (2004, 9, 24, 10, 52, 59, 4, 268, 0)

Is this a bug?

Colin Brown
PyNZ
 
C

Colin Brown

Importing calendar and then making the modification as below to imaplib.py
fixes the problem ;-)

=======================================

def Internaldate2tuple(resp):
"""Convert IMAP4 INTERNALDATE to UT.

Returns Python time module tuple.
"""

mo = InternalDate.match(resp)
if not mo:
return None

mon = Mon2num[mo.group('mon')]
zonen = mo.group('zonen')

day = int(mo.group('day'))
year = int(mo.group('year'))
hour = int(mo.group('hour'))
min = int(mo.group('min'))
sec = int(mo.group('sec'))
zoneh = int(mo.group('zoneh'))
zonem = int(mo.group('zonem'))

# INTERNALDATE timezone must be subtracted to get UT

zone = (zoneh*60 + zonem)*60
if zonen == '-':
zone = -zone

tt = (year, mon, day, hour, min, sec, -1, -1, -1)
return time.gmtime(calendar.timegm(tt) - zone)

==============================================

Colin Brown
PyNZ
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top