📜  Python| time.localtime() 方法

📅  最后修改于: 2022-05-13 01:55:00.796000             🧑  作者: Mango

Python| time.localtime() 方法

Python中的时间模块提供了各种与时间相关的功能。该模块属于 Python 的标准实用程序模块。
Time 模块time.localtime()方法用于将自纪元以来以秒表示的时间转换为本地时间的time.struct_time对象。
要将自纪元以来的给定时间(以秒为单位)转换为 UTC 中的time.struct_time对象,请使用time.gmtime()方法。
此方法返回一个带有命名元组接口的time.struct_time对象。以下是time.struct_time对象中存在的值:

IndexAttributeValues
0tm_year(for example, 1993)
1tm_monrange [1, 12]
2tm_mdayrange [1, 31]
3tm_hourrange [0, 23]
4tm_minrange [0, 59]
5tm_secrange [0, 61]
6tm_wdayrange [0, 6], Monday is 0
7tm_ydayrange [1, 366]
8tm_isdst0, 1 or -1
N/Atm_zoneabbreviation of timezone name
N/Atm_gmtoffoffset east of UTC in seconds

代码 #1:使用time.localtime()方法

Python3
# Python program to explain time.localtime() 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 Local time
obj = time.localtime()
 
# Print the time.struct.time object
print(obj)
 
# We can change it to
# Day Mon date Hour:Min:Sec year
# format using time.asctime() method
t = time.asctime(obj)
print(t)


Python3
# Python program to explain time.localtime() method
 
# importing time module
import time
 
# Time in seconds
# since the epoch
secs = 950000000
 
# Convert the given time in seconds
# since the epoch to a
# time.struct_time object in local time
# using time.localtime() method
obj = time.localtime(secs)
 
# Print the time.struct_time object
print("time.struct_time object for seconds =", secs)
print(obj)
 
 
# Time in seconds
# since the epoch
secs = 950000000.81956
 
# Convert the given time in seconds
# since the epoch to a
# time.struct_time object in local time
# using time.localtime() method
obj = time.localtime(secs)
 
# Print the time.struct_time object
print("\ntime.struct_time object for seconds =", secs)
print(obj)
 
 
# Output for secs = 950000000
# and secs = 950000000.81956
# will be same because
# fractions in 950000000.81956
# i.e .81956 will be ignored


输出:
time.struct_time(tm_year=2019, tm_mon=8, tm_mday=22, tm_hour=10, tm_min=3,
tm_sec=15, tm_wday=3, tm_yday=234, tm_isdst=0)
Thu Aug 22 10:03:15 2019

代码 #2:使用time.localtime()方法

Python3

# Python program to explain time.localtime() method
 
# importing time module
import time
 
# Time in seconds
# since the epoch
secs = 950000000
 
# Convert the given time in seconds
# since the epoch to a
# time.struct_time object in local time
# using time.localtime() method
obj = time.localtime(secs)
 
# Print the time.struct_time object
print("time.struct_time object for seconds =", secs)
print(obj)
 
 
# Time in seconds
# since the epoch
secs = 950000000.81956
 
# Convert the given time in seconds
# since the epoch to a
# time.struct_time object in local time
# using time.localtime() method
obj = time.localtime(secs)
 
# Print the time.struct_time object
print("\ntime.struct_time object for seconds =", secs)
print(obj)
 
 
# Output for secs = 950000000
# and secs = 950000000.81956
# will be same because
# fractions in 950000000.81956
# i.e .81956 will be ignored
输出:
time.struct_time object for seconds = 950000000
time.struct_time(tm_year=2000, tm_mon=2, tm_mday=8, tm_hour=14, tm_min=23,
tm_sec=20, tm_wday=1, tm_yday=39, tm_isdst=0)

time.struct_time object for seconds = 950000000.81956
time.struct_time(tm_year=2000, tm_mon=2, tm_mday=8, tm_hour=14, tm_min=23,
tm_sec=20, tm_wday=1, tm_yday=39, tm_isdst=0)

参考: https://docs。 Python.org/3/library/time.html#time.localtime