[ python-Bugs-1280061 ] time.strptime() fails with unicode date string, de_DE locale

SourceForge.net noreply at sourceforge.net
Tue Sep 6 20:04:25 CEST 2005


Bugs item #1280061, was opened at 2005-09-01 13:06
Message generated for change (Comment added) made by meonkeys
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1280061&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Library
Group: Python 2.4
Status: Closed
Resolution: Wont Fix
Priority: 5
Submitted By: Adam Monsen (meonkeys)
Assigned to: Brett Cannon (bcannon)
Summary: time.strptime() fails with unicode date string, de_DE locale

Initial Comment:
Trying to parse a German date string fails in Python
2.4.1. Test case attached.

Since there's no indenting, I suppose the test case can
also be pasted here:

import locale, time
locale.setlocale(locale.LC_TIME, 'de_DE')
date = u'10. September 2005 um 17:26'
format = '%d. %B %Y um %H:%M'
time.strptime(date, format)

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

----------------------------------------------------------------------

>Comment By: Adam Monsen (meonkeys)
Date: 2005-09-06 11:04

Message:
Logged In: YES 
user_id=259388

Ok, sounds good.

For anyone trying to work around this, unicode date strings
should be encoded to simple Python strings (type of str)
before passing into strptime. Here's an updated version of
the aforementioned test case demonstrating an acceptable
workaround:

import locale, time
locale.setlocale(locale.LC_TIME, 'de_DE')
date = u'September'.encode()
format = '%B'
time.strptime(date, format)

--
Adam Monsen <adamm at wazamatta.com>
http://adammonsen.com/

----------------------------------------------------------------------

Comment By: Brett Cannon (bcannon)
Date: 2005-09-02 14:05

Message:
Logged In: YES 
user_id=357491

OK, then that settles it.  If time.strftime() ever returned
unicode, I would say this needs fixing.  But since
time.strptime() is meant to be the mirror opposite of
time.strftime(), I am going to close this as "Won't Fix". 
If time.strftime() ever gets changed to return unicode, then
I will fix it.

----------------------------------------------------------------------

Comment By: Adam Monsen (meonkeys)
Date: 2005-09-02 13:57

Message:
Logged In: YES 
user_id=259388

I get a str from time.strftime().

>>> import time
>>> time.strftime('%B')
'September'
>>> time.strftime('%B').__class__
<type 'str'>

----------------------------------------------------------------------

Comment By: Brett Cannon (bcannon)
Date: 2005-09-02 13:37

Message:
Logged In: YES 
user_id=357491

Can you let me know what time.strftime() outputs for your
test case, specifically what type of basestring (str or
unicode)?

----------------------------------------------------------------------

Comment By: Adam Monsen (meonkeys)
Date: 2005-09-02 09:43

Message:
Logged In: YES 
user_id=259388

Here's a simpler, more precise test case (also attached):

import locale, time
locale.setlocale(locale.LC_TIME, 'de_DE')
date = u'September'; format = '%B'
time.strptime(date, format)

Here's the error I see:

Traceback (most recent call last):
  File "de_strptime_fail_simple.py", line 4, in ?
    time.strptime(date, format)
  File "/usr/lib/python2.4/_strptime.py", line 329, in strptime
    month = locale_time.f_month.index(found_dict['B'].lower())
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in
position 1: ordinal not in range(128)

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1280061&group_id=5470


More information about the Python-bugs-list mailing list