Laszlo Nagy wrote:
Sorry, I found it:

date            = date-text / DQUOTE date-text DQUOTE
date-day        = 1*2DIGIT ; Day of month
date-day-fixed  = (SP DIGIT) / 2DIGIT ; Fixed-format version of date-day
date-month      = "Jan" / "Feb" / "Mar" / "Apr" / "May" / "Jun" /
                  "Jul" / "Aug" / "Sep" / "Oct" / "Nov" / "Dec"
date-text       = date-day "-" date-month "-" date-year



Is there a standard way to convert a datetime to this special, format or should I write my own function?
If anyone is interested.

Keywords: IMAP, RFC3501, date format, search

"""Custom date class that 'knows' the date format as specified in RFC3501 
IMAP4rev1"""
import datetime

class IMAPDate(datetime.date):
        MONTHNAMES = 
['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
        def to_imapdate(self):
                """Format a date as described in RFC3501."""
                return "%d-%s-%d" % (
                        self.day,
                        self.MONTHNAMES[self.month-1],
                        self.year
                )
        @classmethod
        def from_imapdate(cls,s):
                """Convert an IMAP style date format to an IMAPDate object."""
                if isinstance(s,basestring):
                        parts = s.split('-')
                        ok = False
                        if len(parts) == 3:
                                try:
                                        day = int(parts[0])
                                        month = cls.MONTHNAMES.index(parts[1])+1
                                        year = int(parts[2])                    
                
                                except:
                                        pass
                                return cls(year,month,day)
                        if not ok:
                                raise ValueError("Invalid IMAP date format. 
Please check RFC3501.")
                else:
                        raise TypeError
                


if __name__ == '__main__':
        date = IMAPDate.today()
        fmt = date.to_imapdate()
        print fmt
        print IMAPDate.from_imapdate(fmt)
        print IMAPDate.from_imapdate('32-Jan-2006')
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to