python time.gmtime()方法,转时间戳为结构化时间
time.gmtime()方法
python gmtime()为time模块中的方法,可以将以秒为单位的时间戳转化为UTC时区的结构化日期时间,该结构化日期为time.struct_time类型,类似tuple。
语法
gmtime([seconds]) -> (tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst)
提示:其中的“->”符号指定gmtime()方法的返回值类型。
python源码中对gmtime()方法的介绍:
Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a.
GMT). When 'seconds' is not passed in, convert the current time instead.
If the platform supports the tm_gmtoff and tm_zone, they are available as
attributes only.
参数
参数 | 描述 |
---|---|
seconds | 可选参数,指定时间戳的值,如果不传递,则默认使用当前日期的时间戳——convert the current time instead.。 |
返回值
python 结构化时间time.struct_time类型值。
gmtime()实例代码
>>> import time
>>> time.time()
1677995715.813766 #将作为参数传递给第二个time.gmtime()方法
>>> time.gmtime()
time.struct_time(tm_year=2023, tm_mon=3, tm_mday=5, tm_hour=5, tm_min=55, tm_sec=29, tm_wday=6, tm_yday=64, tm_isdst=0)
>>> time.gmtime(1677995715.813766)
time.struct_time(tm_year=2023, tm_mon=3, tm_mday=5, tm_hour=5, tm_min=55, tm_sec=15, tm_wday=6, tm_yday=64, tm_isdst=0)