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: strftime('%x') does not use my locale
Type: behavior Stage: resolved
Components: Extension Modules Versions: Python 3.6, Python 3.4, Python 3.5, Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: eryksun, hobarrera, martin.panter, r.david.murray, xiang.zhang
Priority: normal Keywords:

Created on 2017-02-06 06:41 by hobarrera, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (8)
msg287082 - (view) Author: Hugo Osvaldo Barrera (hobarrera) Date: 2017-02-06 06:41
As the the posix spec for strftime:

%c   The preferred date and time representation for the current locale.
%x   The preferred date representation for the current locale without the time.

However, python doesn't seem to respect this:

$ python3.5 -c "from datetime import datetime; print(datetime.now().strftime('%x'))"
02/06/17

$ date +%x  # This one is right:
2017-02-06

$ echo $LC_TIME
en_DK.UTF-8

* The same applies for '%c'.
* The same applies for other python versions.
* The same applies regardless of LC_TIME and LANG.
* The same applies to time.strftime()
* I tried a few different LC_TIME values, with the same result every time.
msg287084 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2017-02-06 06:51
Have you tried enabling the locale with locale.setlocale()? I believe Python only enables LC_CTYPE by default, so other locale aspects like LC_TIME won’t work until they are enabled.
msg287085 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2017-02-06 06:55
>>> datetime.now().strftime("%x")
'02/06/17'
>>> from locale import setlocale, LC_TIME
>>> setlocale(LC_TIME)
'C'
>>> setlocale(LC_TIME, "en_US.utf8")
'en_US.utf8'
>>> datetime.now().strftime("%x")
'02/06/2017'
msg287086 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2017-02-06 06:59
As Martin said, you need to set the LC_TIME category using an empty string to use the locale LC_* environment variables. Python 3 sets LC_CTYPE at startup (on Unix platforms only), but LC_TIME is left in the initial C locale:

    >>> locale.setlocale(locale.LC_CTYPE, None)
    'en_DK.UTF-8'
    >>> locale.setlocale(locale.LC_TIME, None)
    'C'
    >>> time.strftime('%x')
    '02/06/17'

    >>> locale.setlocale(locale.LC_TIME, "")
    'en_DK.UTF-8'
    >>> time.strftime('%x')
    '2017-02-06'
msg287088 - (view) Author: Hugo Osvaldo Barrera (hobarrera) Date: 2017-02-06 07:22
It would seem that

    locale.setlocale(locale.LC_TIME, "")

fixes the issue. However, there seems to be no mention on this on the relevant documentation page[1], which is actually the source of my confusion.

As a "fix" to this issue (since I'm probably not the first nor last person to come across it), can we add a note regarding this below the table in section 8.1.8.

[1]: https://docs.python.org/3.6/library/datetime.html#strftime-strptime-behavior
msg287090 - (view) Author: Xiang Zhang (xiang.zhang) * (Python committer) Date: 2017-02-06 07:30
I doubt the info belongs to datetime module documentation. When you encounter locale related problems maybe it's better for you to refer locale module documentation, and it mentions this behaviour https://docs.python.org/3/library/locale.html#background-details-hints-tips-and-caveats.
msg287093 - (view) Author: Hugo Osvaldo Barrera (hobarrera) Date: 2017-02-06 07:41
The problem is that the datetime/strftime documentation describes "%c" as:

    Locale’s appropriate date and time representation.

However, this is not really accurate (this is not the *default* behaviour), that's why I was mentioning the clarification. IMHO, a mere mere link to that caveats section would be enough.

Maybe looking at the locale section sounded intuitive to you because you're already familiar with it, but for people who don't KNOW that there's a caveats section there, it's not really obvious.
msg287129 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-02-06 13:47
Yes, it is the default behavior, because the default locale, as for most (almost all?) programming languages (not just Python), is the C locale, until you change it.  The reason for this is to get consistent behavior no matter what the locale is set to, unless you explicitly enable the local locale (or some other locale) in your program.

Whether it is worth adding a link is a separate question, but we really don't get this question often, unlike some other FAQs.
History
Date User Action Args
2022-04-11 14:58:42adminsetgithub: 73643
2017-02-06 13:47:53r.david.murraysetnosy: + r.david.murray
messages: + msg287129
2017-02-06 07:41:24hobarrerasetmessages: + msg287093
2017-02-06 07:30:35xiang.zhangsetnosy: + xiang.zhang
messages: + msg287090
2017-02-06 07:22:14hobarrerasetmessages: + msg287088
2017-02-06 06:59:10eryksunsetstatus: open -> closed

nosy: + eryksun
messages: + msg287086

resolution: not a bug
stage: resolved
2017-02-06 06:55:33martin.pantersetmessages: + msg287085
2017-02-06 06:51:55martin.pantersetnosy: + martin.panter
messages: + msg287084
2017-02-06 06:41:40hobarreracreate