-
-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
strptime(.., '%c') fails to parse output of strftime('%c', ..) in some locales #53203
Comments
The following code: import locale, time
locale.setlocale(locale.LC_ALL, "fr_FR.UTF-8")
t = time.localtime()
s = time.strftime('%c', t)
time.strptime('%c', s) Raises ValueError: time data '%c' does not match format 'Mer 9 jui 16:14:46 2010' in any locale where month follows day in '%c' format. Note that attached C code works as expected on my OSX laptop. I wonder it it would make sense to call platform strptime where available? I wonder if platform support for strptime has improved since 2002 when _strptime.py was introduced. |
Adding bpo-8915 as a dependency because deducing D_T_FMT locale setting from strftime output seems impossible: >>> locale.nl_langinfo(locale.D_T_FMT)
'%a %b %e %H:%M:%S %Y' |
Victor, You may be interested because your native language is implicated. :-) |
time.strptime(s, '%c' ) ? |
Oh my. It certainly took a long time to recognize a silly mistake! Thanks. |
My tests were wrong but the problem does exist. I am attaching a script that tests strptime(.., '%c') for all locales installed on my system (an unmodified US Mac OS X 10.6.6). The only failing locale that I recognize is Hebrew (he_IL). Eli, what do you think about this?
$ ./python.exe cfmt.py
am_ET [ማክሰ ጃንዩ 11 18:56:18 2011] %A %B %d %H:%M:%S %Y != %a %b %e %H:%M:%S %Y
et_EE [T, 11. jaan 2011. 18:56:18] %a, %d. %B %Y. %H:%M:%S != %a, %d. %b %Y. %T
he_IL [EST 18:56:18 2011 ינו 11 ג'] %Z %H:%M:%S %Y %B %d %a != %Z %H:%M:%S %Y %b %d %a |
|
On Tue, Jan 11, 2011 at 7:26 PM, Roumen Petrov <report@bugs.python.org> wrote:
According to what standard? POSIX defines it as %c Replaced by the locale's appropriate date and time representation. http://pubs.opengroup.org/onlinepubs/009695399/functions/strftime.html and the manual page on my system agrees: %c is replaced by national representation of time and date. |
On Linux, cfmt.py fails on fr_FR locale (the only valid locale in the list of tested locales): The problem is the month format: locale.nl_langinfo(locale.D_T_FMT) returns '%a %d %b %Y %T %Z', but _strptime (LocaleTime().LC_date_time) uses '%a %d %B %Y %H:%M:%S %Z' => '%b' vs '%B'. _strptime.LocalTime.__calc_date_time() uses strftime('%c') and then parse the output to get the complete format. But it uses strftime('%c') with the march month, and in french, march is formatted 'mars' for both month formats (%b *and* %B). _strptime.LocalTime.__calc_date_time() should detect that the month has the same format with %b and %B, and try other timestamps (other months). |
Alexander, I get the same error for the he_IL locale. Will look into this |
The problem for Hebrew appears to be the same as the one Victor stated for French. March in Hebrew is also a 3-letter word which means it's equal to its abbreviation. |
I'm attaching a patch for Lib/_strptime.py that handles the month differently in __calc_date_time. It cycles all months, trying to find one where the full and abbrev names are different and matches it against the timestamp created by strftime. This solution is a hack, but so is the whole __calc_date_time function :-) [IMHO] All tests pass and I also tried it manually with all the problematic locales reported by Alexander - seems to work correctly. If this looks OK to you guys I can commit and backport. |
On Sat, Jan 15, 2011 at 2:20 AM, Eli Bendersky <report@bugs.python.org> wrote:
I am not sure how to proceed. On one hand, I opened this issue to I made this issue depend on bpo-8915 because I think strptime should I don't think this fix solves all the problems. For example, in most '%a %b %e %H:%M:%S %Y'
>>> LocaleTime().LC_date_time
'%a %b %d %H:%M:%S %Y' This does not seem to be an issue because strptime with %d seems to be On the patch itself:
Eli, what do you think yourself: should we try to perfect the hack or |
Python is not C! |
Alexander,
With understanding of (2) I will be able to also logically reason about the next steps :-) |
You pretty much hit the nail on the head. Some platforms don't have strptime or did not have it at the time this code was written. The locale module is probably more recent than this code as well. |
Alexander, but still - this isn't just an implementation of strptime. strptime, AFAIU strptime gets the format string as a parameter and uses it to parse a date string into a "tm" struct. So why do we need to parse a date string *without* a format string in Python, resorting to heuristics and pseudo-AI instead? |
Eli, Given your last comment, are you still proposing your patch for inclusion or should we take the bpo-8915 approach? |
In some locales (for example French and Hebrew), the default month used in __calc_date_time has the same name in full and abbreviated form. So the code failed to correctly distinguish formats %b and %B. Co-authored-by: Eli Bendersky <eliben@gmail.com>
In some locales (for example French and Hebrew), the default month used in __calc_date_time has the same name in full and abbreviated form. So the code failed to correctly distinguish formats %b and %B. Co-authored-by: Eli Bendersky <eliben@gmail.com>
I agree that using the OS
So I took the Eli's patch and added numerous tests for |
I tested with many locales and found several cases in which this patch did not work:
There are similar bugs related to ambiguity of the day of the week name. For example, in Kashubian, the month name matches the day of the week name for the sample datetime. In Yoruba, an abbreviated day names are used with suffix "ọjọ ́", and this matches the full day name, but not all full day name have such suffix. Many locales fail due to space-padded numbers or non-ASCII digits. I am not sure what of these bugs I'll fix here and what will leave for other issues. |
Run them with different locales and different date and time. Add the @run_with_locales() decorator to run the test with multiple locales. Improve the run_with_locale() context manager/decorator -- it now catches only expected exceptions and reports the testsas skipped if no appropriate locale is available.
Run them with different locales and different date and time. Add the @run_with_locales() decorator to run the test with multiple locales. Improve the run_with_locale() context manager/decorator -- it now catches only expected exceptions and reports the test as skipped if no appropriate locale is available.
Run them with different locales and different date and time. Add the @run_with_locales() decorator to run the test with multiple locales. Improve the run_with_locale() context manager/decorator -- it now catches only expected exceptions and reports the test as skipped if no appropriate locale is available.
Run them with different locales and different date and time. Add the @run_with_locales() decorator to run the test with multiple locales. Improve the run_with_locale() context manager/decorator -- it now catches only expected exceptions and reports the test as skipped if no appropriate locale is available. (cherry picked from commit 19984fe) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Run them with different locales and different date and time. Add the @run_with_locales() decorator to run the test with multiple locales. Improve the run_with_locale() context manager/decorator -- it now catches only expected exceptions and reports the test as skipped if no appropriate locale is available. (cherry picked from commit 19984fe) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Run them with different locales and different date and time. Add the @run_with_locales() decorator to run the test with multiple locales. Improve the run_with_locale() context manager/decorator -- it now catches only expected exceptions and reports the test as skipped if no appropriate locale is available. (cherry picked from commit 19984fe) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Run them with different locales and different date and time. Add the @run_with_locales() decorator to run the test with multiple locales. Improve the run_with_locale() context manager/decorator -- it now catches only expected exceptions and reports the test as skipped if no appropriate locale is available. (cherry picked from commit 19984fe)
For now, this PR fixes |
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:
bugs.python.org fields:
Linked PRs
The text was updated successfully, but these errors were encountered: