datestr2num, dateutil.parse and timezone problems

I am having some problems getting matplotlib.dates.datestr2num to handle
timezones in the datestring.

import matplotlib
matplotlib.dates.datestr2num('Jan 1, 2007 12:00 PDT')

732677.83333333337

matplotlib.dates.datestr2num('Jan 1, 2007 12:00 PST')

732677.83333333337

matplotlib.dates.datestr2num('Jan 1, 2007 12:00-08')

732677.83333333337

matplotlib.dates.datestr2num('Jan 1, 2007 12:00 UTC')

732677.5

matplotlib.dates.datestr2num('Jan 1, 2007 12:00 EST')

732677.5

matplotlib.dates.datestr2num('Jan 1, 2007 12:00-07')

732677.79166666663

The problem appears to lie with dateutil, as direct use of the
dateutil.parser.parse function shows the same problem:

import dateutil.parser
dateutil.parser.parse('Jan 1, 2007 12:00 EST')

datetime.datetime(2007, 1, 1, 12, 0)

dateutil.parser.parse('Jan 1, 2007 12:00 PDT')

datetime.datetime(2007, 1, 1, 12, 0, tzinfo=tzlocal())

dateutil.parser.parse('Jan 1, 2007 12:00 PST')

datetime.datetime(2007, 1, 1, 12, 0, tzinfo=tzlocal())

I am using dateutil version 1.2.

Any suggestions on how to get either matplotlib.dates.datestr2num or
dateutil.parser.parse to properly handle timezone information in the
datestring would be greatly appreciated.

Charles Seaton
OHSU/CMOP

···

--
View this message in context: http://www.nabble.com/datestr2num%2C-dateutil.parse-and-timezone-problems-tf4609462.html#a13162968
Sent from the matplotlib - users mailing list archive at Nabble.com.

Not sure how to answer this question vis-a-vid dateutil.parser, but
you may want to consider creating your own datestr -> datetime
converter using time.strptime and then allowing mpl to convert to
numbers using date2num. I think you can use the %Z format code for
timezones.

JDH

···

On 10/11/07, Charles Seaton <cseaton@...1743...> wrote:

Any suggestions on how to get either matplotlib.dates.datestr2num or
dateutil.parser.parse to properly handle timezone information in the
datestring would be greatly appreciated.

John,

I would prefer to be able to use the power and flexibility of datestr2num
and the underlying dateutil.parser, rather than writing my own parser.
However, looking further at dateutil.parser.parse, it takes an argument
tzinfos, which allows timezone names other than the local timezone and
UTC/GMT/Z to be understood within parse. I haven't worked out the full range
of functionality for the tzinfos argument, but a simple example would be:

tzlibrary = {'EST':-5*60*60,'EDT':-4*60*60,'EST':-6*60*60,
'CDT':-5*60*60,'PST':-8*60*60,'PDT':-7*60*60}
matplotlib.dates.date2num(dateutil.parser.parse('jan 1, 2007 12:00
PST',tzinfos=tzlibrary))

732677.83333333337

matplotlib.dates.date2num(dateutil.parser.parse('jan 1, 2007 12:00
PDT',tzinfos=tzlibrary))

732677.79166666663

matplotlib.dates.date2num(dateutil.parser.parse('jan 1, 2007 12:00
EDT',tzinfos=tzlibrary))

732677.66666666663

Actually, I can just duplicate datestr2num, but pass a tzinfos dictionary as
well, e.g.:

def datestr2num(d,tzinfos=None):
    """
    Convert a date string to a datenum using dateutil.parser.parse
    d can be a single string or a sequence of strings
    """
    if is_string_like(d):
        dt = dateutil.parser.parse(d,tzinfos=tzinfos)
        return date2num(dt)
    else:
        return date2num([dateutil.parser.parse(s,tzinfos=tzinfos) for s in
d])

explicitly passing a tzinfos of None does not cause any problems for
dateutil.parser.parse.

thanks,

Charles

John Hunter-4 wrote:

···

On 10/11/07, Charles Seaton <cseaton@...1743...> wrote:

Any suggestions on how to get either matplotlib.dates.datestr2num or
dateutil.parser.parse to properly handle timezone information in the
datestring would be greatly appreciated.

Not sure how to answer this question vis-a-vid dateutil.parser, but
you may want to consider creating your own datestr -> datetime
converter using time.strptime and then allowing mpl to convert to
numbers using date2num. I think you can use the %Z format code for
timezones.

JDH

--
View this message in context: http://www.nabble.com/datestr2num%2C-dateutil.parse-and-timezone-problems-tf4609462.html#a13167297
Sent from the matplotlib - users mailing list archive at Nabble.com.