dateutil
handles the format straight out of the box:
from dateutil import parser
dt = parser.parse('2014-02-06 13:30:00-05:00')
Now you have a timezone-aware datetime.datetime()
value, including displaying it as a UTC ISO8601 timestamp:
from dateutil import tz
print dt.astimezone(tz.tzutc()).isoformat(' ')
This uses datetime.datetime.astimezone()
to transform the timezone-aware datetime
value to the UTC timezone.
Demo:
>>> from dateutil import parser
>>> from dateutil import tz
>>> dt = parser.parse('2014-02-06 13:30:00-05:00')
>>> print dt.astimezone(tz.tzutc()).isoformat(' ')
2014-02-06 18:30:00+00:00
This includes a timezone offset (of +00:00
), you can use the datetime.datetime.strftime()
method to format the string differently if desired.