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

Set the locale to the user’s default setting #1654

Merged
merged 1 commit into from
May 3, 2024
Merged

Conversation

tom-kuca
Copy link
Contributor

@tom-kuca tom-kuca commented May 3, 2024

According to POSIX, a program which has not called setlocale(LC_ALL, '') runs using the portable 'C' locale. Calling setlocale(LC_ALL, '') lets it use the default locale as defined by the LANG variable.

Source: https://docs.python.org/3/library/locale.html#locale.getdefaultlocale

Applications typically start with a call of

import locale
locale.setlocale(locale.LC_ALL, '')

Source: https://docs.python.org/3/library/locale.html#locale.setlocale

Setting the locale fixes the detection of 24h format.

Details

pretty_datetime function checks if time.strftime("%p") is empty for branching between 12h / 24h:

alot/alot/helper.py

Lines 216 to 217 in 3a3fc88

ampm = d.strftime('%p').lower()
if len(ampm):

According to the documentation above, the locale falls back to C and the format is always 12h. Here is the program to check the behavior:

Before

import datetime
import locale

print(locale.getlocale(locale.LC_TIME))
print(datetime.datetime.now().strftime("%p"))

Output:

$ LANG=cs_CZ.UTF-8 python ~/prg/python/time.py 
(None, None)
PM

After

import datetime
import locale

locale.setlocale(locale.LC_ALL, '')    
print(locale.getlocale(locale.LC_TIME))
print(datetime.datetime.now().strftime("%p"))

Output:

$ LANG=cs_CZ.UTF-8 python ~/prg/python/time.py 
('cs_CZ', 'UTF-8')

Note that the format is blank.

$ python --version
Python 3.11.8

According to POSIX, a program which has not called setlocale(LC_ALL, '') runs using the portable 'C' locale. Calling setlocale(LC_ALL, '') lets it use the default locale as defined by the LANG variable.
Copy link
Owner

@pazz pazz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanations and the patch! I am actually quite surprised that one has to do anything in order to cause the application to use the users default locale and did not notice that the 12h format was used regardless of the locale.
For me this looks good.

@pazz pazz merged commit 7c2e43c into pazz:master May 3, 2024
3 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants