time.strptime() for different languages

Adam Monsen haircut at gmail.com
Thu Sep 1 05:39:44 CEST 2005

Figured this out. I thought I'd post my results in case it is helpful
to someone else.

import locale, time
# save old locale
old_loc = locale.getlocale(locale.LC_TIME)
locale.setlocale(locale.LC_TIME, 'nl_NL')
# seems to be the only way to avoid a ValueError from _strptime...
# now that's a badly behaved module!
import _strptime; reload(_strptime)
# parse local date
date = '10 augustus 2005 om 17:26'
format = '%d %B %Y om %H:%M'
dateTuple = time.strptime(date, format)
# switch back to previous locale
locale.setlocale(locale.LC_TIME, old_loc)

If I try to do further date parsing in the same scope (with a different
locale), it fails. Let me know if you have any ideas about why.

Adam Monsen

More information about the Python-list mailing list