python time.mktime()方法
time.mktime()方法
python time模块中的mktime()方法,可以将结构化的日期时间转化为时间戳,浮点数类型的值,单位为秒。获取时间戳的方法还可以使用time模块中的time()方法,二者不同的是,mktime()需要一个结构化的时间作为参数。
语法
mktime(tuple) -> floating point number
python源码中对mktime()方法的介绍:
Convert a time tuple in local time to seconds since the Epoch.
Note that mktime(gmtime(0)) will not generally return zero for most
time zones; instead the returned value will either be equal to that
of the timezone or altzone attributes on the time module.
参数
参数 | 描述 |
---|---|
tuple | 结构化的时间,比如time.localtime()的返回值类型:time.struct_time(tm_year=2023, tm_mon=3, tm_mday=5, tm_hour=13, tm_min=24, tm_sec=15, tm_wday=6, tm_yday=64, tm_isdst=0) |
返回值
python float类型值,以秒为单位的时间戳(距格林威治0时的总秒数)。
mktime()实例代码
>>> import time
>>> time.mktime(time.localtime())
1677993973.0