473,395 Members | 2,423 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

time.strptime() for different languages

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
http://adammonsen.com/

(crossposted to Seattle Python Users List)

Aug 31 '05 #1
8 13498
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 ;)

--
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
Aug 31 '05 #2
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
http://adammonsen.com/

Aug 31 '05 #3
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")

(2005, 8, 10, 17, 26, 0, 2, 222, -1)

(see http://docs.python.org/lib/module-locale.html for more on this)

</F>

Aug 31 '05 #4
Excellent! Thank you, Fredrik!

--
Adam Monsen
http://adammonsen.com/

Sep 1 '05 #5
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).

Ugh! Is this stuff broken or is it just me?

--
Adam Monsen
http://adammonsen.com/

Sep 1 '05 #6
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.

--
Adam Monsen
http://adammonsen.com/

Sep 1 '05 #7
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.

def bust_strptime_cache():
import _strptime
_strptime._cache_lock.acquire()
_strptime._TimeRE_cache = _strptime.TimeRE()
_strptime._regex_cache = {}
_strptime._cache_lock.release()

This has been filed as Python bug #1290505. (
http://sf.net/support/tracker.php?aid=1290505 ) A full test case is
attached to that bug.

--
Adam Monsen
http://adammonsen.com/

Sep 13 '05 #8
Brett Cannon fixed this bug last week. Thanks, Brett!

--
Adam Monsen
http://adammonsen.com/

Sep 19 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Gerrit Holl | last post by:
Posted with permission from the author. I have some comments on this PEP, see the (coming) followup to this message. PEP: 321 Title: Date/Time Parsing and Formatting Version: $Revision: 1.3 $...
1
by: Michele Simionato | last post by:
I have strings representing UTC dates and I want to convert them in time tuples using time.strptime. I have an issue with daylight savings. For instance Tue Sep 14 06:06:15 2004 is converted...
4
by: Bryan | last post by:
can someone explain to me why i get different values for the tm_yday and tm_isdst values? >>> import rfc822 >>> rfc822.parsedate('Mar 1 01:03:59 2004') (2004, 3, 1, 1, 3, 59, 0, 0, 0) >>>...
10
by: Maksim Kasimov | last post by:
hi all, sorry if i'm reposting why time.strptime and time.localtime returns tuple with different DST (9 item of the tuple)? is there some of setting to fix the problem? Python 2.2.3 (#1, May...
2
by: Robert | last post by:
A certain DAV server reports a time stamp as "Tues, 30 Aug 2005 20:48:31" ( but not "Tue, ..." as usual) This produces the error below. >>> time.strptime("Tue, 30 Aug 2005 20:48:31","%a, %d...
3
by: Ilja Booij | last post by:
Hi all, I have some trouble with the following: I'm getting a time string, in YYYY-MM-DD HH:mm:ss format, which I need to translate into a string with DD-Mon-YYYY HH:mm:ss +HHMM, where the...
1
by: davelist | last post by:
I'm guessing there is an easy way to do this but I keep going around in circles in the documentation. I have a time stamp that looks like this (corresponding to UTC time): start_time =...
2
by: Joshua J. Kugler | last post by:
I am getting results like these with the time module: %S'))) 1173610800 %S'))) 1173610800 '2007-03-11 03:00:00' I know it probably has something to do with daylight savings, but how can I...
2
by: barronmo | last post by:
I'm trying to get the difference in dates using the time module rather than datetime because I need to use strptime() to convert a date and then find out how many weeks and days until that date. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.