Anyone know of something that works like time.strptime(), but for other languages? Specifically, Dutch (ex: "31 augustus 2005, 17:26") and German?
Thinking out loud... since "31 augustus 2005, 17:26" is only different by month name, I suppose I could just substitute the month name using a translation table for English to Dutch month names.
Adam Monsen wrote: > Anyone know of something that works like time.strptime(), but for > other languages? Specifically, Dutch (ex: "31 augustus 2005, 17:26") > and German?
> Thinking out loud... since "31 augustus 2005, 17:26" is only different > by month name, I suppose I could just substitute the month name using > a translation table for English to Dutch month names.
Have you tested it with the proper locale setting and strptime(dateString, "%c")? I have not ;)
No, this doesn't seem to work, and I can't find anything in the documentation indicating that it should.
>>> import os >>> os.getenv('LANG') 'nl_NL' >>> import time >>> time.strptime("10 augustus 2005 om 17:26", "%d %B %Y om %H:%M")
Traceback (most recent call last): File "<stdin>", line 1, in ? File "/usr/lib/python2.4/_strptime.py", line 292, in strptime raise ValueError("time data did not match format: data=%s fmt=%s" % ValueError: time data did not match format: data=10 augustus 2005 om 17:26 fmt=%d %B %Y om %H:%M
Adam Monsen wrote: > No, this doesn't seem to work, and I can't find anything in the > documentation indicating that it should.
> >>> import os > >>> os.getenv('LANG') > 'nl_NL' > >>> import time > >>> time.strptime("10 augustus 2005 om 17:26", "%d %B %Y om %H:%M") > Traceback (most recent call last): > File "<stdin>", line 1, in ? > File "/usr/lib/python2.4/_strptime.py", line 292, in strptime > raise ValueError("time data did not match format: data=%s fmt=%s" > % > ValueError: time data did not match format: data=10 augustus 2005 om > 17:26 fmt=%d %B %Y om %H:%M >>> import locale, time >>> time.strftime("%B") 'August' >>> locale.getlocale() (None, None) >>> locale.setlocale(locale.LC_ALL, "") 'nl_NL' >>> locale.getlocale()
('nl_NL', 'ISO8859-1')
>>> time.strftime("%B") 'augustus' >>> time.strptime("10 augustus 2005 om 17:26", "%d %B %Y om %H:%M")
Strange, but I can't figure out how to switch back to the default locale.
>>> import locale, datetime, time >>> locale.setlocale(locale.LC_ALL, 'nl_NL') 'nl_NL' >>> date = '10 augustus 2005 om 17:26' >>> time.strptime(date, "%d %B %Y om %H:%M")
(2005, 8, 10, 17, 26, 0, 2, 222, -1)
>>> locale.setlocale(locale.LC_ALL, '') 'en_US.UTF-8' >>> date = '10 August 2005 at 17:26' >>> time.strptime(date, "%d %B %Y at %H:%M")
Traceback (most recent call last): File "<stdin>", line 1, in ? File "/usr/lib/python2.4/_strptime.py", line 292, in strptime raise ValueError("time data did not match format: data=%s fmt=%s" % ValueError: time data did not match format: data=10 August 2005 at 17:26 fmt=%d %B %Y at %H:%M
Also, locale.resetlocale() throws locale.Error (I saw some open bugs on this).
Figured this out. I thought I'd post my results in case it is helpful to someone else.
----------------------------------8<---------------------------------- 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) ---------------------------------->8----------------------------------
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.
One way I'm able to do further date parsing in other locales is to switch the locale for LC_TIME, bust the _strptime regular expressions manually, then call strptime() again. Here's a function to bust the cache. This works for me, but your mileage may vary.