Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
time.strptime() for different languages
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  9 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Adam Monsen  
View profile  
 More options Aug 31 2005, 4:11 pm
Newsgroups: comp.lang.python
From: "Adam Monsen" <hair...@gmail.com>
Date: 31 Aug 2005 13:11:01 -0700
Local: Wed, Aug 31 2005 4:11 pm
Subject: 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)


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Benjamin Niemann  
View profile  
 More options Aug 31 2005, 4:44 pm
Newsgroups: comp.lang.python
From: Benjamin Niemann <p...@odahoda.de>
Date: Wed, 31 Aug 2005 22:44:23 +0200
Local: Wed, Aug 31 2005 4:44 pm
Subject: Re: time.strptime() for different languages

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/


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Adam Monsen  
View profile  
 More options Aug 31 2005, 5:02 pm
Newsgroups: comp.lang.python
From: "Adam Monsen" <hair...@gmail.com>
Date: 31 Aug 2005 14:02:24 -0700
Local: Wed, Aug 31 2005 5:02 pm
Subject: Re: time.strptime() for different languages
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/


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Fredrik Lundh  
View profile  
 More options Aug 31 2005, 5:54 pm
Newsgroups: comp.lang.python
From: "Fredrik Lundh" <fred...@pythonware.com>
Date: Wed, 31 Aug 2005 23:54:56 +0200
Local: Wed, Aug 31 2005 5:54 pm
Subject: Re: time.strptime() for different languages

('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>


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Adam Monsen  
View profile  
 More options Aug 31 2005, 8:00 pm
Newsgroups: comp.lang.python
From: "Adam Monsen" <hair...@gmail.com>
Date: 31 Aug 2005 17:00:37 -0700
Local: Wed, Aug 31 2005 8:00 pm
Subject: Re: time.strptime() for different languages
Excellent! Thank you, Fredrik!

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


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Adam Monsen  
View profile  
 More options Aug 31 2005, 9:08 pm
Newsgroups: comp.lang.python
From: "Adam Monsen" <hair...@gmail.com>
Date: 31 Aug 2005 18:08:04 -0700
Local: Wed, Aug 31 2005 9:08 pm
Subject: Re: time.strptime() for different languages
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/


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Adam Monsen  
View profile  
 More options Aug 31 2005, 11:39 pm
Newsgroups: comp.lang.python
From: "Adam Monsen" <hair...@gmail.com>
Date: 31 Aug 2005 20:39:44 -0700
Local: Wed, Aug 31 2005 11:39 pm
Subject: Re: time.strptime() for different languages
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/


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Adam Monsen  
View profile  
 More options Sep 13 2005, 7:02 pm
Newsgroups: comp.lang.python
From: "Adam Monsen" <hair...@gmail.com>
Date: 13 Sep 2005 16:02:30 -0700
Local: Tues, Sep 13 2005 7:02 pm
Subject: Re: time.strptime() for different languages
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/


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Adam Monsen  
View profile  
 More options Sep 19 2005, 11:58 am
Newsgroups: comp.lang.python
From: "Adam Monsen" <hair...@gmail.com>
Date: 19 Sep 2005 08:58:33 -0700
Local: Mon, Sep 19 2005 11:58 am
Subject: Re: time.strptime() for different languages
Brett Cannon fixed this bug last week. Thanks, Brett!

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


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2010 Google