[Tutor] Date comparison across time zones

Kent Johnson kent37 at tds.net
Tue Jan 13 19:01:02 CET 2009


On Tue, Jan 13, 2009 at 11:45 AM, frenc1z 1z <frenc1z at gmail.com> wrote:

> I would like to compare some dates (date+time really). The dates all have
> the following RFC2822 format:
>
> Eg. is d1 < d2?
> d1 = "Tue, 13 Jan 2009 03:27:29 -0800"
> d2 = "Tue, 13 Jan 2009 02:40:00 -0600"
>
> Been trying to parse these dates as follows: print datetime.strptime("Tue,
> 13 Jan 2009 02:40:00 -0800", "%a, %d %b %Y %H:%M:%S %z").
>
> I get the following error on the conversion "ValueError: 'z' is a bad
> directive in format '%a, %d %b %Y %H:%M:%S %z'".

The correct format code is %Z (upper case). However that still doesn't
work, I don't have a complete solution for you.

In [8]: datetime.strptime("Tue, 13 Jan 2009 02:40:00 EST", "%a, %d %b
%Y %H:%M:%S %Z")
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)

C:\Project\Play\<ipython console> in <module>()

C:\Python25\lib\_strptime.py in strptime(data_string, format)
    328     if not found:
    329         raise ValueError("time data did not match format:
data=%s  fmt=%s" %
--> 330                          (data_string, format))
    331     if len(data_string) != found.end():
    332         raise ValueError("unconverted data remains: %s" %

ValueError: time data did not match format:  data=Tue, 13 Jan 2009
02:40:00 EST  fmt=%a, %d %b %Y %H:%M:%S %Z

Kent


More information about the Tutor mailing list