Title: time.strptime does not use Java date format strings properly
Type: behaviour Severity: urgent
Components: Library Versions: 2.5.2b1
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: zyasoft Nosy List: proteusguy, zyasoft
Priority: high Keywords:

Created on 2010-10-04.05:36:45 by proteusguy, last changed 2010-10-16.03:05:46 by zyasoft.

msg6131 (view) Author: Ben Scherrey (proteusguy) Date: 2010-10-04.05:36:43
Trying to execute the following code works under c-python but fails
under jython:

import datetime

results under Jython 2.5.2b1 (Release_2_5_2beta1:7075, Jun 28 2010, 07:44:20):
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "/opt/jython2.5.2b1/Lib/", line 1499, in strptime
   return cls(*(_time.strptime(date_string, format))[0:6])
       at java.text.SimpleDateFormat.compile(
       at java.text.SimpleDateFormat.initialize(
       at java.text.SimpleDateFormat.<init>(
       at java.text.SimpleDateFormat.<init>(
       at org.python.modules.time.Time.strptime(
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(
       at java.lang.reflect.Method.invoke(

java.lang.IllegalArgumentException: Unterminated quote

results under Python 2.6.4 (r264:75706, Jun 16 2010, 19:34:45):
datetime.datetime(2010, 7, 5, 9, 20, 24)
msg6135 (view) Author: Jim Baker (zyasoft) Date: 2010-10-04.13:49:51
The problem here is that we do the actual parsing with java.text.SimpleDateFormat (, but Z for that library means parse a RFC 822 timezone (like -0700), not the 'Z' timezone (UTC).

We need to quote 'Z' to fix this bug; we should also quote all other letters not otherwise defined.
msg6136 (view) Author: Ben Scherrey (proteusguy) Date: 2010-10-04.14:53:37
Thanx for the diagnosis, Jim. Can you suggest a workaround while we wait for a fix in the (hopefully) next release?
msg6137 (view) Author: Jim Baker (zyasoft) Date: 2010-10-04.16:54:44
The only workaround I have is to omit the Z in the format specifier, since any extra trailing characters are ignored anyway.

So this works:

>>> datetime.datetime.strptime("2010-07-05T09:20:24Z","%Y-%m-%dT%H:%M:%S") 
datetime.datetime(2010, 7, 5, 9, 20, 24)
msg6138 (view) Author: Jim Baker (zyasoft) Date: 2010-10-04.17:31:48
org.python.modules.Time#py2java_format did not properly end the quote if the quoted string literal (in a-z, A-Z) was the last character in the format string.

I have made a fix of this problem in r7133, however, it needs an appropriate unit test to complete. Putting in pending now. As usual, this seems to be underspecified in the Python test suite, including looking at 2.7.
msg6172 (view) Author: Jim Baker (zyasoft) Date: 2010-10-16.03:05:46
Fixed in r7148. This could use more testing but it does test reasonably throughly the case here, an unterminated quoted literal in the Java format string.
Date User Action Args
2010-10-16 03:05:46zyasoftsetstatus: pending -> closed
resolution: accepted -> fixed
messages: + msg6172
2010-10-04 17:31:49zyasoftsetstatus: open -> pending
messages: + msg6138
2010-10-04 16:54:45zyasoftsetpriority: high
assignee: zyasoft
resolution: accepted
messages: + msg6137
2010-10-04 14:53:38proteusguysetmessages: + msg6136
2010-10-04 13:51:21zyasoftsettitle: datetime.strptime error? -> time.strptime does not use Java date format strings properly
2010-10-04 13:49:53zyasoftsetnosy: + zyasoft
messages: + msg6135
2010-10-04 05:36:45proteusguycreate