Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

time.strptime() fails with unicode date string, de_DE locale #42332

Closed
meonkeys mannequin opened this issue Sep 1, 2005 · 6 comments
Closed

time.strptime() fails with unicode date string, de_DE locale #42332

meonkeys mannequin opened this issue Sep 1, 2005 · 6 comments
Assignees
Labels
stdlib Python modules in the Lib dir

Comments

@meonkeys
Copy link
Mannequin

meonkeys mannequin commented Sep 1, 2005

BPO 1280061
Nosy @brettcannon
Files
  • bug.txt: shows time.strptime() failure with a unicode German date string
  • de_strptime_fail_simple.py: simpler, more precise demonstration of time.strptime() failure with a unicode German date string
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/brettcannon'
    closed_at = <Date 2005-09-06.18:04:24.000>
    created_at = <Date 2005-09-01.20:06:03.000>
    labels = ['library']
    title = 'time.strptime() fails with unicode date string, de_DE locale'
    updated_at = <Date 2005-09-06.18:04:24.000>
    user = 'https://bugs.python.org/meonkeys'

    bugs.python.org fields:

    activity = <Date 2005-09-06.18:04:24.000>
    actor = 'meonkeys'
    assignee = 'brett.cannon'
    closed = True
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2005-09-01.20:06:03.000>
    creator = 'meonkeys'
    dependencies = []
    files = ['1766', '1767']
    hgrepos = []
    issue_num = 1280061
    keywords = []
    message_count = 6.0
    messages = ['26177', '26178', '26179', '26180', '26181', '26182']
    nosy_count = 2.0
    nosy_names = ['brett.cannon', 'meonkeys']
    pr_nums = []
    priority = 'normal'
    resolution = 'wont fix'
    stage = None
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue1280061'
    versions = ['Python 2.4']

    @meonkeys
    Copy link
    Mannequin Author

    meonkeys mannequin commented Sep 1, 2005

    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/

    @meonkeys meonkeys mannequin closed this as completed Sep 1, 2005
    @meonkeys meonkeys mannequin assigned brettcannon Sep 1, 2005
    @meonkeys meonkeys mannequin added the stdlib Python modules in the Lib dir label Sep 1, 2005
    @meonkeys
    Copy link
    Mannequin Author

    meonkeys mannequin commented Sep 2, 2005

    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)

    @brettcannon
    Copy link
    Member

    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)?

    @meonkeys
    Copy link
    Mannequin Author

    meonkeys mannequin commented Sep 2, 2005

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

    @brettcannon
    Copy link
    Member

    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.

    @meonkeys
    Copy link
    Mannequin Author

    meonkeys mannequin commented Sep 6, 2005

    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/

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 9, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant