以 Unix 时间戳格式获取当前 GMT 时间的最简单方法是什么? [英] What is the easiest way to get current GMT time in Unix timestamp format?
问题描述
Python 提供了不同的包(datetime
、time
、calendar
),可以看出 此处 以处理时间.我犯了一个大错误,使用以下内容获取当前 GMT 时间 time.mktime(datetime.datetime.utcnow().timetuple())
Python provides different packages (datetime
, time
, calendar
) as can be seen here in order to deal with time. I made a big mistake by using the following to get current GMT time time.mktime(datetime.datetime.utcnow().timetuple())
在 Unix 时间戳中获取当前 GMT 时间的简单方法是什么?
What is a simple way to get current GMT time in Unix timestamp?
推荐答案
我会使用 time.time() 获取自纪元以来以秒为单位的时间戳.
I would use time.time() to get a timestamp in seconds since the epoch.
import time
time.time()
输出:
1369550494.884832
对于大多数平台上的标准 CPython 实现,这将返回一个 UTC 值.
For the standard CPython implementation on most platforms this will return a UTC value.
这篇关于以 Unix 时间戳格式获取当前 GMT 时间的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!