[Tutor] Formatting questions regarding datetime.isoformat()

eryksun eryksun at gmail.com
Thu Sep 6 09:35:54 CEST 2012


On Wed, Sep 5, 2012 at 11:00 PM, staticsafe <me at staticsafe.ca> wrote:
>
> In [68]: showinfo['RFC3339']
> Out[68]: '2012-09-10T21:00:00-4:00'

You can parse the ISO format using the dateutil module:

http://labix.org/python-dateutil

    >>> from dateutil.parser import parse
    >>> show_time = parse('2012-09-10T21:00:00-4:00')

This produces a time-zone aware datetime object. You can pass its
tzinfo to datetime.now() to get the current time as an aware datetime
object:

    >>> now = datetime.now(show_time.tzinfo)

Or you could use datetime.now(dateutil.tz.tzutc()) for a UTC tzinfo.
It doesn't matter if you're only interested in the timedelta.


More information about the Tutor mailing list