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) 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/
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/
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>
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/
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/
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/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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 $...
|
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...
|
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)
>>>...
|
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...
|
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...
|
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...
|
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 =...
|
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...
|
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. ...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
| |