2

I need to calculate the number of seconds since the epoch from a well-formed string, such as u'Wed Jun 24 20:19:10 PDT 2015'. The following code does that:

def seconds_from_epoch(date, date_pattern='%a %b %d %H:%M:%S %Z %Y'):
    epoch = int(time.mktime(time.strptime(date, date_pattern)))
    return epoch

>>> d=u'Wed Jun 24 20:19:10 PDT 2015'
>>> s=seconds_from_epoch(d)
>>> s
1435202350

The problem is that the strings come from an unpredictable variety of time zones, and the solution above only works if the timezone is the same as that of the running Python script itself (or apparently GMT/UTC, which does not help).

So what I need is something that does exactly the same as the code above, but for all time zones.

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
user3457334
  • 171
  • 2
  • 5

3 Answers3

2

The dateutil parser can convert almost any string to a datetime object, so you can even loosen the well-formed constraint; but anyway, datetime objects in Python 3 have the convenient timestamp method:

>>> d = dateutil.parser.parse('Wed Jun 24 20:19:10 PDT 2015')
>>> d.timestamp()
1435166350.0
>>> d = dateutil.parser.parse('Wed Jun 24 20:19:10 UTC 2015')
>>> d.timestamp()
1435177150.0

Since you're probably using Python 2, here's a manual calculation:

>>> d = dateutil.parser.parse('Wed Jun 24 20:19:10 PDT 2015')
>>> (d - datetime.datetime(1970, 1, 1)).total_seconds()
Traceback (most recent call last):
    ...
TypeError: cant subtract offset-naive and offset-aware datetimes
>>> # :(
>>> (d - datetime.datetime(1970, 1, 1, tzinfo=d.tzinfo)).total_seconds()
1435177150.0 # UTC
>>> ((d - datetime.datetime(1970, 1, 1, tzinfo=d.tzinfo)) - d.utcoffset()).total_seconds()
1435166350.0 # The original timezone
Dan Gittik
  • 3,460
  • 3
  • 17
  • 24
1

See Python strptime() and timezones? particularly answer of Joe Shaw,

I would use https://dateutil.readthedocs.org/en/latest/ , dateutil library already takes care of timezones. The reason why you won't get timezoned date from strptime is that it does not support timezones.

A naive object does not contain enough information to unambiguously locate itself relative to other date/time objects. Whether a naive object represents Coordinated Universal Time (UTC), local time, or time in some other timezone is purely up to the program, just like it’s up to the program whether a particular number represents metres, miles, or mass. Naive objects are easy to understand and to work with, at the cost of ignoring some aspects of reality.

see https://docs.python.org/2/library/datetime.html#datetime.datetime.strptime

Community
  • 1
  • 1
Visgean Skeloru
  • 2,237
  • 1
  • 24
  • 33
0
def total_seconds(dt): #just in case you are using a python that the datetime library does not provide this automagically
    print dt.days*24*60+dt.seconds

from dateutil.parser import parse as date_parse

print total_seconds(date_parse(date_string))

you will need to pip install python-dateutil

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179