Search code examples
pythondatetimelocalestrptime

How to transform an English Gmail imap date to a French date format


I have to read Gmail's imap mail with a Python script with Django, and it works well, except for the date.

I grab the date to this format: '2 Apr 2012 13:21:43' and use strptime:

date = strptime(date, "%d %b %Y %H:%M:%S")

But I've got an error because my locale is fr_FR and in France Apr is not a valid month abbreviation, and I don't know the other month name used by Google. If I knew, it, I could make a mapping:

{'Apr':'avr.',...}

Does anyone have an idea how I could use the correct locale? Can I use the timezone on my strptime to indicate the date is in an English format?


Solution

  • Try

    import locale
    locale.setlocale(locale.LC_ALL,'en_US')
    

    Update

    Have a look at dateutil package as well http://pypi.python.org/pypi/python-dateutil/1.5 if you wouldn't change your location. It works with your sample when I set fr_FR locale.

    from dateutil import parser
    date = '2 Apr 2012 13:21:43'
    date = parser.parse(date)