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.