python从一天中的某个时间生成Posix
给定一个时间字符串,我需要创建一个unixtime值。我可以这样做,但似乎太复杂了python从一天中的某个时间生成Posix,python,Python,给定一个时间字符串,我需要创建一个unixtime值。我可以这样做,但似乎太复杂了 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__ =
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])
到目前为止,我在python方面的经验是,总是有一种比我发现的更简单的方法来做事情。有人有什么想法吗?看起来和我要写的非常接近。不过我还是用时间模块
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:])
那好多了。谢谢