1

I have this readout Tue, 19 Jan 2016 16:12:18 -0800 (PST)

I'm storing emails in mongo and I'd like to convert this into a python time object so I can query Mongo based on time of the email.

What is the best way to do this?

Thanks!

Leb
  • 15,483
  • 10
  • 56
  • 75
Morgan Allen
  • 3,291
  • 8
  • 62
  • 86

4 Answers4

2

python-dateutil would make things simple:

>>> from dateutil.parser import parse
>>> parse("Tue, 19 Jan 2016 16:12:18 -0800 (PST)")
datetime.datetime(2016, 1, 19, 16, 12, 18, tzinfo=tzoffset(u'PST', -28800))
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • I did `sudo pip install python-dateutil` but I'm still getting `ImportError: No module named dateutil.parser` – Morgan Allen Jan 22 '16 at 20:38
  • @MorganAllen make sure you are installing it into the correct environment. Are you using virtual environments? – alecxe Jan 22 '16 at 20:41
  • I'm pretty new to engineering, so not that I know of? I normally just open up terminal...how do I check if I'm in a virtual environment? – Morgan Allen Jan 22 '16 at 20:42
  • I am getting this error (which I haven't seen before) `The directory '/Users/name/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.` – Morgan Allen Jan 22 '16 at 20:42
0

One way is to use the strptime functionality of the datetime module,

i.e.

import datetime
timestamp = "Tue, 19 Jan 2016 16:12:18 -0800 (PST)"
dt = datetime.datetime.strptime(timestamp, "%a, %d %b %Y %H:%M:%S %z (%Z)")

Please read up on Python's datetime module for find the guide about how these format strings work specifically so that you can build your own!

Alex Alifimoff
  • 1,850
  • 2
  • 17
  • 34
  • Awesome. I'm looking at the docs now -- I see that %z is for UTC offeset, but im getting the error `ValueError: 'z' is a bad directive in format '%a, %d %b %Y %H:%M:%S %z (%Z)'`. Any idea why? – Morgan Allen Jan 22 '16 at 20:34
  • Are you using Python <3.2? According to [this](https://bugs.python.org/issue6641) bug report and [this](https://stackoverflow.com/questions/2609259/converting-string-to-datetime-object-in-python) Stack Overflow post, it appears that in pre-3.2 Python, this is implemented on a system level, so if your system doesn't support that directive in C, it may not work. – Alex Alifimoff Jan 22 '16 at 20:43
  • I am using version 2.7.8...so that would explain it – Morgan Allen Jan 22 '16 at 20:43
  • You could simply cut out that information using split and join... your timezone information is already contained in the (PST) part, so you shouldn't need that to get your program to function properly! Sadly, there isn't a wildcard character for strptime. – Alex Alifimoff Jan 22 '16 at 20:46
  • i.e. s = timestamp.split(' '); timestamp = ' '.join(s[:5] + s[6:]) should theoretically do the trick. – Alex Alifimoff Jan 22 '16 at 20:49
0

To parse a time string from an email, you could use email package from stdlib:

>>> from email.utils import parsedate_tz, mktime_tz
>>> timestamp = mktime_tz(parsedate_tz('Tue, 19 Jan 2016 16:12:18 -0800 (PST)'))

where timestamp is "seconds since the Epoch". To get UTC time which you can pass to MongoDB:

>>> from datetime import datetime, timedelta
>>> utc_time = datetime(1970, 1, 1) + timedelta(seconds=timestamp)
>>> utc_time
datetime.datetime(2016, 1, 20, 0, 12, 18)

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

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670
0

Python 3.2+

from datetime import datetime
datetime.strptime("Tue, 19 Jan 2016 16:12:18 -0800 (PST)"[:31],
                  '%a, %d %b %Y %H:%M:%S %z')
dvska
  • 2,399
  • 1
  • 19
  • 14