Issue1964

classification
Title: time.strptime() does not support %f in format
Type: behaviour Severity: normal
Components: Versions:
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: fwierzbicki Nosy List: Arfrever, amak, fwierzbicki, santa4nt
Priority: high Keywords: patch

Created on 2012-08-28.08:38:11 by Arfrever, last changed 2013-03-24.01:24:15 by santa4nt.

Files
File name Uploaded Description Edit Remove
issue1964.patch santa4nt, 2013-03-24.01:24:09 address both issues
Messages
msg7429 (view) Author: Arfrever Frehtes Taifersar Arahesis (Arfrever) Date: 2012-08-28.08:38:10
time.strptime() does not support %f in format.
%f was added in CPython 2.6: http://bugs.python.org/issue1158

CPython 2.7:
$ python2.7 -c 'import time; print(time.strptime("0", "%f"))'
time.struct_time(tm_year=1900, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=1, tm_isdst=-1)
$

Jython 2.7:
$ jython2.7 -c 'import time; print(time.strptime("0", "%f"))'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
        at java.text.SimpleDateFormat.compile(SimpleDateFormat.java:845)
        at java.text.SimpleDateFormat.initialize(SimpleDateFormat.java:659)
        at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:585)
        at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:560)
        at org.python.modules.time.Time.strptime(Time.java:707)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:601)

java.lang.IllegalArgumentException: java.lang.IllegalArgumentException: Illegal pattern character 'f'
$

src/org/python/modules/time/Time.java contains the following code, which does not detect format not translatable to Java:
        String jformat = py2java_format(format);
        if (jformat == null) {
            // Format not translatable to java, fallback to _strptime
            return pystrptime(data_string, format);
        }
        SimpleDateFormat d = new SimpleDateFormat(jformat);

Another symptom of probably the same bug is wrong exception (java.lang.IllegalArgumentException instead of ValueError documented in documentation of time.strptime):
$ python2.7 -c 'import time; print(time.strptime("", "%e"))'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib64/python2.7/_strptime.py", line 467, in _strptime_time
    return _strptime(data_string, format)[0]
  File "/usr/lib64/python2.7/_strptime.py", line 317, in _strptime
    (bad_directive, format))
ValueError: 'e' is a bad directive in format '%e'
$ jython2.7 -c 'import time; print(time.strptime("", "%e"))'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
        at java.text.SimpleDateFormat.compile(SimpleDateFormat.java:845)
        at java.text.SimpleDateFormat.initialize(SimpleDateFormat.java:659)
        at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:585)
        at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:560)
        at org.python.modules.time.Time.strptime(Time.java:707)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:601)

java.lang.IllegalArgumentException: java.lang.IllegalArgumentException: Illegal pattern character 'e'
$
msg7440 (view) Author: Arfrever Frehtes Taifersar Arahesis (Arfrever) Date: 2012-08-28.20:52:07
Maybe something like the following could be used:

SimpleDateFormat d;
try {
    d = new SimpleDateFormat(jformat);
} catch (IllegalArgumentException e) {
    return pystrptime(data_string, format);
}
msg7974 (view) Author: Santoso Wijaya (santa4nt) Date: 2013-03-24.01:19:06
Attaching a patch to address both '%f' format support, IllegalArgumentException -> ValueError conversion, and unit tests for both.
History
Date User Action Args
2013-03-24 01:24:15santa4ntsetfiles: - issue1964.patch
2013-03-24 01:24:09santa4ntsetfiles: + issue1964.patch
2013-03-24 01:19:11santa4ntsettype: behaviour
2013-03-24 01:19:07santa4ntsetfiles: + issue1964.patch
keywords: + patch
messages: + msg7974
nosy: + santa4nt
2013-02-27 18:31:59fwierzbickisetpriority: high
assignee: fwierzbicki
2012-08-28 22:03:22amaksetnosy: + amak
2012-08-28 20:52:08Arfreversetmessages: + msg7440
2012-08-28 20:46:11Arfreversetnosy: + fwierzbicki
2012-08-28 08:38:11Arfrevercreate