string datetime format to time() unix time format in python -
i know must covered question elsewhere, questions saw on looked going in opposite direction. understand how convert time.time()
human-readable datetime format follows:
>>> import time, datetime >>> thetime = time.time() >>> thetime 1375289544.41976 >>> datetime.datetime.fromtimestamp(thetime).strftime("%y-%m-%d %h:%m:%s") '2013-07-31 12:51:08'
what's function going inverse direction?
>>> thetime = '2013-07-30 21:23:14.744643' >>> time.strptime(thetime, "%y-%m-%d %h:%m:%s") traceback (most recent call last): file "<stdin>", line 1, in <module> file "/usr/lib/python2.7/_strptime.py", line 454, in _strptime_time return _strptime(data_string, format)[0] file "/usr/lib/python2.7/_strptime.py", line 328, in _strptime data_string[found.end():]) valueerror: unconverted data remains: .744643
i'd go string seconds since epoch format.
update
this i'm using, seems inelegant---there must function already, right?
def datetostamp(x): thetime = x.split('.') return(time.mktime(time.strptime(thetime[0], "%y-%m-%d %h:%m:%s")) + float('.'+thetime[1])) >>> thetime = '2013-07-30 21:23:14.744643' >>> datetostamp(thetime) 1375233794.744643
update 2
maybe more simply, i'm missing format code fractions of seconds?
you were missing format fractions of second.
time.strptime(thetime, "%y-%m-%d %h:%m:%s.%f")
Comments
Post a Comment