This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: time.strptime() fails with unicode date string, de_DE locale
Type: Stage:
Components: Library (Lib) Versions: Python 2.4
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: brett.cannon Nosy List: brett.cannon, meonkeys
Priority: normal Keywords:

Created on 2005-09-01 20:06 by meonkeys, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
bug.txt meonkeys, 2005-09-01 20:06 shows time.strptime() failure with a unicode German date string
de_strptime_fail_simple.py meonkeys, 2005-09-02 16:43 simpler, more precise demonstration of time.strptime() failure with a unicode German date string
Messages (6)
msg26177 - (view) Author: Adam Monsen (meonkeys) Date: 2005-09-01 20:06
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/
msg26178 - (view) Author: Adam Monsen (meonkeys) Date: 2005-09-02 16:43
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)
msg26179 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2005-09-02 20:37
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)?
msg26180 - (view) Author: Adam Monsen (meonkeys) Date: 2005-09-02 20:57
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'>
msg26181 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2005-09-02 21:05
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.
msg26182 - (view) Author: Adam Monsen (meonkeys) Date: 2005-09-06 18:04
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@wazamatta.com>
http://adammonsen.com/
History
Date User Action Args
2022-04-11 14:56:12adminsetgithub: 42332
2005-09-01 20:06:03meonkeyscreate