datetime.strptime difficulties

4,404 views
Skip to first unread message

Luke Graybill

unread,
Nov 26, 2008, 8:33:30 PM11/26/08
to utahp...@googlegroups.com
Hello all, I'm having some issues here with datetime.strptime not performing as documented, and before I resign myself to manually parsing this string, I thought I'd check with some brighter minds.

I'm using the following code to attempt to convert an ISO-8601 formatted string such as '2008-11-20T04:00:00-0700' to a datetime object:

>>> value = '2008-11-20T04:00:00-0700'
>>> datetime.strptime(value, '%Y-%m-%dT%H:%M:%S%z')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.5/_strptime.py", line 320, in strptime
    (bad_directive, format))
ValueError: 'z' is a bad directive in format '%Y-%m-%dT%H:%M:%S%z'

According to the documentation, this directive SHOULD be accepted. I asked in #python on freenode as well, and they pointed me to `man date` which also lists %z as a valid formatting directive.

Thanks for any suggestions,

Luke

Ryan McGuire

unread,
Nov 26, 2008, 8:59:20 PM11/26/08
to Utah Python User Group
The docs also say that %z is new in version 2.6... so are you running
2.6?


On Nov 26, 8:33 pm, "Luke Graybill" <killa...@gmail.com> wrote:
> Hello all, I'm having some issues here with datetime.strptime not performing
> as documented, and before I resign myself to manually parsing this string, I
> thought I'd check with some brighter minds.
>
> I'm using the following code to attempt to convert an ISO-8601 formatted
> string such as '2008-11-20T04:00:00-0700' to a datetime object:
>
> >>> value = '2008-11-20T04:00:00-0700'
> >>> datetime.strptime(value, '%Y-%m-%dT%H:%M:%S%z')
>
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "/usr/lib/python2.5/_strptime.py", line 320, in strptime
>     (bad_directive, format))
> ValueError: 'z' is a bad directive in format '%Y-%m-%dT%H:%M:%S%z'
>
> According to the
> documentation<http://docs.python.org/library/datetime.html#id1>,

Ryan McGuire

unread,
Nov 26, 2008, 9:02:33 PM11/26/08
to Utah Python User Group
I think I read that wrong actually, so nevermind

Jonathan Ellis

unread,
Nov 26, 2008, 9:13:11 PM11/26/08
to utahp...@googlegroups.com
the strptime and strftime stuff is not really portable, because python
is lazy and passes your arguments more or less straight through to the
C library it was built against. I specifically remember seeing %z not
working because of the platform's C library before.

sucks, doesn't it?

-Jonathan

Luke Graybill

unread,
Nov 26, 2008, 9:15:39 PM11/26/08
to utahp...@googlegroups.com
I think the docs only meant that 2.6 has new behavior where strftime returns empty strings for %z on time zone naive objects. After testing in 2.5.2, %z does work for strftime:

Python 2.5.2 (r252:60911, Oct  5 2008, 19:24:49)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime, pytz
>>> dt = datetime.datetime(2000,11,20,4,tzinfo=pytz.utc)
>>> dt.strftime('%z')
'+0000'
>>> datetime.datetime.strptime('+0000', '%z')

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.5/_strptime.py", line 320, in strptime
    (bad_directive, format))
ValueError: 'z' is a bad directive in format '%z'

That note in the docs about 2.6 isn't very clear.

Byron Clark

unread,
Nov 26, 2008, 10:01:30 PM11/26/08
to utahp...@googlegroups.com
On Wed, Nov 26, 2008 at 06:33:30PM -0700, Luke Graybill wrote:
> Hello all, I'm having some issues here with datetime.strptime not performing
> as documented, and before I resign myself to manually parsing this string, I
> thought I'd check with some brighter minds.

I've never had much luck with datetime.strptime, but python-dateutil[1] has
a great dateparser (parser.parse()).

[1] http://labix.org/python-dateutil

--
Byron Clark

Luke Graybill

unread,
Nov 26, 2008, 10:57:33 PM11/26/08
to utahp...@googlegroups.com
Looks good, I'll give it a shot. Thanks to everyone for the input.
Reply all
Reply to author
Forward
0 new messages