Python| time.gmtime() 方法
Python中的时间模块提供了各种与时间相关的功能。该模块属于 Python 的标准实用程序模块。
Time 模块的time.gmtime()
方法用于将自纪元以来以秒表示的时间转换为 UTC 中的time.struct_time
对象,其中tm_isdst属性始终为 0。
要将自纪元以来的以秒为单位的给定时间转换为本地时间的time.struct_time
对象,请使用time.localtime()
方法。
此方法返回一个带有命名元组接口的time.struct_time
对象。以下是time.struct_time
对象中存在的值:
Index | Attribute | Values |
---|---|---|
0 | tm_year | (for example, 1993) |
1 | tm_mon | range [1, 12] |
2 | tm_mday | range [1, 31] |
3 | tm_hour | range [0, 23] |
4 | tm_min | range [0, 59] |
5 | tm_sec | range [0, 61] |
6 | tm_wday | range [0, 6], Monday is 0 |
7 | tm_yday | range [1, 366] |
8 | tm_isdst | 0, 1 or -1 |
N/A | tm_zone | abbreviation of timezone name |
N/A | tm_gmtoff | offset east of UTC in seconds |
Syntax: time.gmtime([secs])
Parameter:
secs (optional): An integer or float value representing time in seconds. Fractions of specified seconds will be ignored. If the secs parameter is not provided or None then the current time as returned by time.time() method is used.
Return type: This method returns an object of class ‘time.struct_time’.
代码 #1:使用time.gmtime()
方法
Python3
# Python program to explain time.gmtime() method
# importing time module
import time
# If secs parameter
# is not given then
# the current time
# as returned by time.time() method
# is used
# Convert the current time in seconds
# since the epoch to a
# time.struct_time object in UTC
obj = time.gmtime()
# Print the time.struct.time object
print(obj)
Python3
# Python program to explain time.gmtime() method
# importing time module
import time
# Time in seconds
# since the epoch
secs = 40000
# Convert the given time in seconds
# since the epoch to a
# time.struct_time object in UTC
# using time.gmtime() method
obj = time.gmtime(secs)
# Print the time.struct_time object
print("time.struct_time object for seconds =", secs)
print(obj)
# Time in seconds
# since the epoch
secs = 40000.7856
# Convert the given time in seconds
# since the epoch to a
# time.struct_time object in UTC
# using time.gmtime() method
obj = time.gmtime(secs)
# Print the time.struct_time object
print("\ntime.struct_time object for seconds =", secs)
print(obj)
# Output for sec = 40000
# and secs = 40000.7856
# will be same because
# fractions in 40000.7856
# i.e .7856 will be ignored
time.struct_time(tm_year=2019, tm_mon=8, tm_mday=22, tm_hour=3, tm_min=53,
tm_sec=32, tm_wday=3, tm_yday=234, tm_isdst=0)
代码 #2:使用time.gmtime()
方法
Python3
# Python program to explain time.gmtime() method
# importing time module
import time
# Time in seconds
# since the epoch
secs = 40000
# Convert the given time in seconds
# since the epoch to a
# time.struct_time object in UTC
# using time.gmtime() method
obj = time.gmtime(secs)
# Print the time.struct_time object
print("time.struct_time object for seconds =", secs)
print(obj)
# Time in seconds
# since the epoch
secs = 40000.7856
# Convert the given time in seconds
# since the epoch to a
# time.struct_time object in UTC
# using time.gmtime() method
obj = time.gmtime(secs)
# Print the time.struct_time object
print("\ntime.struct_time object for seconds =", secs)
print(obj)
# Output for sec = 40000
# and secs = 40000.7856
# will be same because
# fractions in 40000.7856
# i.e .7856 will be ignored
time.struct_time object for seconds = 40000
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=11, tm_min=6,
tm_sec=40, tm_wday=3, tm_yday=1, tm_isdst=0)
time.struct_time object for seconds = 40000.7856
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=11, tm_min=6,
tm_sec=40, tm_wday=3, tm_yday=1, tm_isdst=0)
参考: https://docs。 Python.org/3/library/time.html#time.gmtime