9

I am trying to convert a string timestamp into a proper datetime object. The problem I am having is that there is a timezone offset and everything I am doing doesn't seem to work.

Ultimately I want to convert the string timestamp into a datetime object in my machines timezone.

# string timestamp     
date = "Fri, 16 Jul 2010 07:08:23 -0700"
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
alfredo
  • 961
  • 3
  • 10
  • 11

3 Answers3

10

The dateutil package is handy for parsing date/times:

In [10]: date = u"Fri, 16 Jul 2010 07:08:23 -0700"

In [11]: from dateutil.parser import parse

In [12]: parse(date)
Out[12]: datetime.datetime(2010, 7, 16, 7, 8, 23, tzinfo=tzoffset(None, -25200))

Finally, to convert into your local timezone,

In [13]: parse(date).astimezone(YOUR_LOCAL_TIMEZONE)
Indradhanush Gupta
  • 4,067
  • 10
  • 44
  • 60
ars
  • 120,335
  • 23
  • 147
  • 134
  • how do I take this and turn it into a time representation that matches my current timezone? and thanks for that btw :) – alfredo Jul 20 '10 at 03:57
  • Glad to help. You can use the `astimezone` method of the datetime object that's returned by parse. See this question for detail: http://stackoverflow.com/questions/79797/how-do-i-convert-local-time-to-utc-in-python – ars Jul 20 '10 at 04:13
  • Dateutil rocks! parsed my string ("2013-10-22T21:56:00.000-03:00") without me having to provide a format string. – fccoelho Oct 23 '13 at 11:29
5

It looks like datetime.datetime.strptime(d, '%a, %d %b %Y %H:%M:%S %z') should work, but according to this bug report there are issues with the %z processing. So you'll probably have to handle the timezone on your own:

import datetime

d = u"Fri, 16 Jul 2010 07:08:23 -0700"

d, tz_info = d[:-5], d[-5:]
neg, hours, minutes = tz_info[0], int(tz_info[1:3]), int(tz_info[3:])
if neg == '-':
    hours, minutes = hours * -1, minutes * -1

d = datetime.datetime.strptime(d, '%a, %d %b %Y %H:%M:%S ')
print d
print d + datetime.timedelta(hours = hours, minutes = minutes)
Chris B.
  • 85,731
  • 25
  • 98
  • 139
0

Here's a stdlib solution:

>>> from datetime import datetime
>>> from email.utils import mktime_tz, parsedate_tz
>>> datetime.fromtimestamp(mktime_tz(parsedate_tz(u"Fri, 16 Jul 2010 07:08:23 -0700")))
datetime.datetime(2010, 7, 16, 16, 8, 23) # your local time may be different

See also, Python: parsing date with timezone from an email.

Note: fromtimestamp() may fail if the local timezone had different UTC offset in the past (2010) and if it does not use a historical timezone database on the given platform. To fix it, you could use tzlocal.get_localzone(), to get a pytz tzinfo object representing your local timezone. pytz provides access to the tz database in a portable manner:

>>> timestamp = mktime_tz(parsedate_tz(u"Fri, 16 Jul 2010 07:08:23 -0700"))
>>> import tzlocal # $ pip install tzlocal
>>> str(datetime.fromtimestamp(timestamp, tzlocal.get_localzone()))
'2010-07-16 16:08:23+02:00'
Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670