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

date.strftime and datetime.strftime format dates wrong in some locales. #130528

Open
petergraham opened this issue Feb 25, 2025 · 5 comments
Open
Labels
3.12 bugs and security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes extension-modules C modules in the Modules dir stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@petergraham
Copy link

petergraham commented Feb 25, 2025

Bug report

Bug description:

date.strftime and datetime.strftime format dates wrong in some locales. For example, when the locale is Bulgaria the '%A' format string returns an incorrect value.

>>> locale.setlocale(locale.LC_TIME, 'bg')    # 'bg' is Bulgaria
>>> date_str = datetime.date(2025, 2, 25).strftime('%A') # %A is locale's day of the week
>>> date_str
'âòîðíèê'
>>> date_str.encode('cp1252').decode('cp1251')  # I think we're interpreting a cp1251 string as cp1252
'вторник'  # Google tells me this is Tuesday in Bulgarian

This also fails with the '%x' format string

I've only tested this on Windows.

My guess is that CPython is making a narrow character API call and using the wrong code page. It should be using the wide character call instead.

CPython versions tested on:

3.13

Operating systems tested on:

Windows

@petergraham petergraham added the type-bug An unexpected behavior, bug, or error label Feb 25, 2025
@picnixz

This comment has been minimized.

@picnixz picnixz added extension-modules C modules in the Modules dir stdlib Python modules in the Lib dir labels Feb 25, 2025
@picnixz
Copy link
Member

picnixz commented Feb 25, 2025

Ok, second question. I think what's being returned with %A is encoded with something that is not necessarily useful for the LC_TIME language. CP-1252 is a legacy default format on Windows according to Wikipedia, and I assume that it's as if we had "no localized encoding".

@serhiy-storchaka you're more of an expert in this case, is it the expected behaviour? namely, should we expect the caller to actually perform the encode/decode themselves or is it something that's wrong with datetime?

@tomasr8
Copy link
Member

tomasr8 commented Feb 25, 2025

I think locale.getlocale(locale.LC_TIME)[1] should return the encoding used by strftime

@serhiy-storchaka
Copy link
Member

Oh-oh, it is yet one issue similar to #69998 and bpo-28604. The workaround at he user side is to set LC_CTYPE to the same value as LC_TIME. We can fix this at Python side, but at the cost of making strftime() slower and potentially more thread-unsafe (it is already not thread-safe in sense that it can fail if the environment is changed in other thread, but now it will be able to break other threads by changing the environment).

@serhiy-storchaka serhiy-storchaka added 3.12 bugs and security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes labels Feb 25, 2025
@donBarbos
Copy link
Contributor

I reproduce your case on Linux, so first of all, if you don't have locale (you can check it with locale -a command), you will get locale.Error: unsupported setting, but all available locales were utf8 so I think there was no error:

>>> locale.setlocale(locale.LC_TIME, 'ka_GE.utf8')
'ka_GE.utf8'
>>> datetime.date(2025, 2, 25).strftime('%A')
'სამშაბათი' # Tuesday
>>> locale.setlocale(locale.LC_TIME, 'ka_GE')
Traceback (most recent call last):
  File "<python-input-8>", line 1, in <module>
    locale.setlocale(locale.LC_TIME, 'ka_GE')
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^
  File "./lib/python3.13/locale.py", line 615, in setlocale
    return _setlocale(category, locale)
locale.Error: unsupported locale setting

(I checked the behavior on the main branch and also version 3.13)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 bugs and security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes extension-modules C modules in the Modules dir stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
Development

No branches or pull requests

5 participants