Jas*_*ker 47 python datetime posix
如何将日期时间或日期对象转换为python中的POSIX时间戳?有一些方法可以从时间戳中创建一个日期时间对象,但我似乎没有找到任何明显的方法以相反的方式进行操作.
ken*_*der 54
import time, datetime
d = datetime.datetime.now()
print time.mktime(d.timetuple())
Run Code Online (Sandbox Code Playgroud)
fix*_*ark 22
对于UTC计算,calendar.timegm是逆的time.gmtime.
import calendar, datetime
d = datetime.datetime.utcnow()
print calendar.timegm(d.timetuple())
Run Code Online (Sandbox Code Playgroud)
请注意,Python now(3.5.2)在对象中包含了一个内置方法datetime:
>>> import datetime
>>> now = datetime.datetime.now()
>>> now.timestamp() # Local time
1509315202.161655
>>> now.replace(tzinfo=datetime.timezone.utc).timestamp() # UTC
1509329602.161655
Run Code Online (Sandbox Code Playgroud)