Reputation: 1227
Given a time string I need to create a unixtime value. I can do it like this but it seems way overcomplicated.
def tounix(timestr):
from datetime import date, datetime
from time import mktime
return mktime(datetime.combine(date.today(),datetime.strptime(timestr,'%H%M%S').time()).timetuple())
if __name__ == '__main__':
import sys
print tounix(sys.argv[1])
My experience so far in python is that there is always a MUCH simpler way of doing things than the one I find. Anyone got any ideas here?
Upvotes: 0
Views: 61
Reputation: 1564
That looks pretty close to what I'd write. I'd just use the time module though.
from time import *
# make a time string
t = strftime('%H%M%S', localtime())
# convert time string to unix time (seconds since epoch)
mktime(localtime()[:3] + strptime(t, '%H%M%S')[3:])
Upvotes: 1