1

A time string like this: 'Thu Nov 26 17:49:28 +0000 2015'.

Need to convert it to UTC timestamp, tried several methods found from online, but not work.

What's the correct way to convert it? Thx.

S1U
  • 825
  • 1
  • 14
  • 23
  • Possible duplicate of [Parsing time string in Python](http://stackoverflow.com/questions/10494312/parsing-time-string-in-python) – donkopotamus Jan 11 '16 at 03:20

3 Answers3

0

Use datetime.datetime.strptime() with the proper format specifier, and then use the timestamp() method:

>>> import datetime
>>> d = 'Thu Nov 26 17:49:28 +0000 2015'
>>> datetime.datetime.strptime(d, '%a %b %d %H:%M:%S %z %Y').timestamp()
1448560168.0
TigerhawkT3
  • 44,764
  • 6
  • 48
  • 82
0

To get "seconds since epoch" from an rfc 5322 date-time string (like in email Date header):

>>> from email.utils import parsedate_tz, mktime_tz
>>> mktime_tz(parsedate_tz('Thu Nov 26 17:49:28 +0000 2015'))
1448560168
jfs
  • 346,887
  • 152
  • 868
  • 1,518
0

If you like Pandas, this is very simple

In [32]: import pandas as pd
# Construct date time index
In [33]: dt_index = pd.DatetimeIndex(['Thu Nov 26 17:49:28 +0000 2015'])
# Get the epoch timestamp in seconds
In [35]: dt_index.astype('int64')/1e9
Out[35]: array([  1.44856017e+09])

In [36]: dt_index.astype('int64')[0]/1e9
Out[36]: 1448560168.0
Nipun Batra
  • 9,415
  • 7
  • 47
  • 72