-2

I have a value like this:

var="11-feb-15:04:02:21"

I need to convert to epoch time:

I tried this:

var.strftime('%s')

I get this error:

AttributeError: 'str' object has no attribute 'strftime'

Any ideas? I

user1471980
  • 10,127
  • 48
  • 136
  • 235
  • 1
    There is so much wrong in that one line: `strftime` isn't a method of `str` (it's a method of `datetime.datetime`); it's not for converting *from* `str` *to* `datetime` (it's for converting *from* `datetime` *to* `str`); and `%s` is nowhere near enough information to explain how you think that string should be interpreted. Maybe [RTFM](https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior)? – jonrsharpe Feb 11 '15 at 21:37
  • related: [Converting datetime.date to UTC timestamp in Python](http://stackoverflow.com/q/8777753/4279) – jfs Feb 11 '15 at 21:38
  • @jonrsharpe: unrelated: `time.strftime('%s')` is `str(int(time.time()))` on some systems. – jfs Feb 11 '15 at 22:28

2 Answers2

6

I hope you will go through this example and look up the corresponding documentation.

>>> s = "11-feb-15:04:02:21"
>>> timestruct = time.strptime(s, "%d-%b-%y:%H:%M:%S")
>>> time.mktime(timestruct)
1423623741.0

The format specifiers are defined here: https://docs.python.org/2/library/time.html#time.strftime

The docs are also clear on the fact that when you want to convert from struct_time in local time to seconds since the epoch you need to use mktime().

Dr. Jan-Philip Gehrcke
  • 33,287
  • 14
  • 85
  • 130
  • why would the OP look up any documentation? they have every thing they need right there. – njzk2 Feb 11 '15 at 21:44
  • @njzk2: I kind of agree with you. However, I myself have found the time/datetime docs a little confusing from time to time, especially as a beginner. It is always a great help to look at a working solution, and *then* to try to understand *why* it works. If he/she does not use that offer, it's his/her problem. I definitely do not expect to get upvoted, though ;-). – Dr. Jan-Philip Gehrcke Feb 11 '15 at 21:46
  • note: local time may be ambiguous (e.g., during end-of-DST transition ("fall back")) and the local timezone may have different utc offset in the past: `time.mktime()` may return a wrong result. – jfs Feb 11 '15 at 22:31
1

To convert a time string that represents local time into "seconds since the Epoch":

#!/usr/bin/env python
import time
from datetime import datetime
import pytz # $ pip install pytz
from tzlocal import get_localzone # $ pip install tzlocal

tt = time.strptime("11-feb-15:04:02:21", "%d-%b-%y:%H:%M:%S")
naive_dt = datetime(*tt[:5]+(min(tt[5], 59),))
dt = get_localzone().localize(naive_dt, is_dst=None)
timestamp = (dt - datetime(1970, 1, 1, tzinfo=pytz.utc)).total_seconds()
print(timestamp) # -> 1423623741.0

This solution raises an exception if the input time is ambiguous or non-existent e.g., during a DST transition -- set is_dst parameter to True/False to disambiguate the time and/or call tz.normalize() method to adjust non-existent local times.

To get a proper utc offset, the tz database is used (get_localzone() returns the local timezone as a pytz timezone object).

time.strptime() is used to support leap seconds e.g., "1-jul-15:1:59:60" is converted to datetime(2015, 7, 1, 1, 59, 59) -- note: seconds are truncated to 59.

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