time.mktime()
Time模塊的方法用於將time.struct_time對象或包含與time.struct_time對象相對應的9個元素的元組轉換為自本地時間開始經過的秒數。
此方法是time.localtime()
它將自紀元以來的秒數表示的時間轉換為本地時間中的time.struct_time對象。
以下是time.struct_time對象中存在的值:
index | 屬性 | 值 |
---|---|---|
0 | tm_year | (例如,1993年) |
1個 | tm_mon | 範圍[1,12] |
2 | tm_mday | 範圍[1,31] |
3 | tm_hour | 範圍[0,23] |
4 | tm_min | 範圍[0,59] |
5 | tm_sec | 範圍[0,61] |
6 | tm_wday | 範圍[0,6],星期一為0 |
7 | tm_yday | 範圍[1,366] |
8 | tm_isdst | 0、1或-1 |
不適用 | tm_zone | 時區名稱的縮寫 |
不適用 | tm_gmtoff | 以秒為單位偏移UTC以東 |
注意:紀元是時間開始的時間點,它取決於平台。在Windows和大多數Unix係統上,紀元是1970年1月1日,00:00:00(UTC),leap秒不計入自紀元以來的秒數。要檢查給定平台上的紀元,我們可以使用time.gmtime(0)。
用法: time.mktime(t)
參數:
t:一個time.struct_time對象或包含9個與time.struct_time對象相對應的元素的元組
返回類型:此方法返回一個浮點值,該值表示自紀元以來以秒表示的時間。
代碼1:用於time.mktime()
方法
# Python program to explain time.mktime() method
# importing time module
import time
# time.gmtime() method will returns
# a time.struct_time object in UTC
# for the time expressed in seconds
# since the epoch
seconds = 1000000
obj1 = time.gmtime(seconds)
# Print time.struct_time object (in UTC)
print(obj1)
# Convert the time.struct_time
# object to local time expressed in
# seconds since the epoch
# using time.mktime() method
time_sec = time.mktime(obj1)
# Print the local time in seconds
print("\nLocal time (in seconds):", time_sec)
# time.strptime() method parse
# a string representing a time
# according to the given format
# and returns a time.struct_time object
# Time string
t = "14 Sep 2019 10:50:00"
# Parse the time string using
# time.strptime() method
obj2 = time.strptime(t, "% d % b % Y % H:% M:% S")
# Convert the time.struct_time
# object to local time expressed in
# seconds since the epoch
# using time.mktime() method
time_sec = time.mktime(obj2)
# Print the local time in seconds
print("\nLocal time (in seconds):", time_sec)
輸出:
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=12, tm_hour=13, tm_min=46, tm_sec=40, tm_wday=0, tm_yday=12, tm_isdst=0) Local time (in seconds):980200.0 Local time (in seconds):1568438400.0
代碼2:如果參數是元組
# Python program to explain time.mktime() method
# importing time module
import time
# A tuple containing 9 elements
# corresponding to time.struct_time object
# for example:consider the below object
# time.struct_time(tm_year = 2019, tm_mon = 9, tm_mday = 13,
# tm_hour = 1, tm_min = 30, tm_sec = 26, tm_wday = 4,
# tm_yday = 256, tm_isdst = 0)
# Tuple corresponding to above
# time.struct_time object will be
tup = (2019, 9, 13, 1, 30, 26, 4, 256, 0)
# Convert the above specified tuple
# to local time expressed in seconds
# since the epoch
# using time.mktime() method
time_sec = time.mktime(tup)
# Print the time
print("Local Time (in seconds since the epoch):", time_sec)
輸出:
Local Time (in seconds since the epoch):1568318426.0
代碼3:顯示time.mktime()
方法是...的反函數time.localtime()
方法
# Python program to explain time.mktime() method
# importing time module
import time
# Get the current time
# expressed in seconds
# since the epoch using
# time.time() method
curr_time = time.time()
# Print the value
# returned by time.time() method
print("Current time (in seconds since the epoch):", curr_time)
# Convert the time expressed in seconds
# since the epoch to
# a time.struct_time object
# in local time using
# time.localtime() method
obj = time.localtime(curr_time)
# Print the time.struct_time object
print("\ntime.struct_time object:")
print(obj, "\n")
# Convert the time.struct_time object
# back to the time expressed
# in seconds since the epoch
# in local time using
# time.mktime() method
time_sec = time.mktime(obj)
# Print the time
print("Time (in seconds since the epoch):", time_sec)
輸出:
Current time (in seconds since the epoch):1568318426.2286296 time.struct_time object: time.struct_time(tm_year=2019, tm_mon=9, tm_mday=13, tm_hour=1, tm_min=30, tm_sec=26, tm_wday=4, tm_yday=256, tm_isdst=0) Time (in seconds since the epoch):1568318426.0
參考文獻: https://docs.python.org/3/library/time.html#time.mktime
相關用法
- Python next()用法及代碼示例
- Python os.dup()用法及代碼示例
- Python set()用法及代碼示例
- Python Decimal max()用法及代碼示例
- Python PIL ImageOps.fit()用法及代碼示例
- Python os.rmdir()用法及代碼示例
- Python sympy.det()用法及代碼示例
- Python Decimal min()用法及代碼示例
- Python os.readlink()用法及代碼示例
- Python os.writev()用法及代碼示例
- Python os.readv()用法及代碼示例
- Python PIL RankFilter()用法及代碼示例
- Python os.rename()用法及代碼示例
- Python os.sendfile()用法及代碼示例
注:本文由純淨天空篩選整理自ihritik大神的英文原創作品 Python | time.mktime() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。