'''asctime()'''import time# Get Current timeprint(time.asctime())# =========================================================# Get time in seconds since the epochseconds = time.time()# Convert to struct_timetime_struct = time.localtime(seconds)# Convert to a stringtime_str = time.asctime(time_struct)print(time_str)# =========================================================# July 20, 1969, 20:17 UTC, when man first landed on the moontimestamp =14182900# Convert to a struct_time in UTCtime_struct = time.gmtime(timestamp)# Convert to a Stringprint(time.asctime(time_struct))
執行結果:
Fri Jul 7 19:03:15 2023
Fri Jul 7 19:03:15 2023
Sun Jun 14 03:41:40 1970
PYTHON
'''ctime()'''mport time# Gives current timeprint(time.ctime())# July 20, 1969, 20:17 UTC, when man first landed on the moontimestamp =14182900print(time.ctime(timestamp))
執行結果:
Output: Fri Jul 7 17:13:47 2023
Output: Sun Jun 14 03:41:40 1970
gmtime(), localtime(), mktime()
gmtime([secs]) 將 Epoch ,轉換為 UTC (Coordinated Universal Time) 格式的 struct_time 物件。如果未提供 secs,它將返回 UTC 時間。
'''mktime()'''import time# =========================================================# 【Converting the current time to epoch time】# Get the current timenow = time.localtime()# convert struct_time to epoch timeepoch_time = time.mktime(now)print("Epoch time:", epoch_time)# =========================================================# 【Converting a specific time to epoch time】# Define a time tupletime_tuple = (2023,7,7,15,30,0,4,188,-1) # July 7, 2023, 15:30:00# convert the time tuple to epoch timeepoch_time = time.mktime(time_tuple)print("Epoch time:", epoch_time)# =========================================================# 【Comparing two dates】# Define two time tuplestime_tuple1 = (2023,7,7,15,30,0,4,188,-1) # July 7, 2023, 15:30:00time_tuple2 = (2023,8,7,15,30,0,1,219,-1) # August 7, 2023, 15:30:00# Convert the time tuples to epoch timeepoch_time1 = time.mktime(time_tuple1)epoch_time2 = time.mktime(time_tuple2)# Compare the epoch timesprint("Time difference in seconds:", epoch_time2 - epoch_time1)
執行結果:
Epoch time: 1688727762.0
Epoch time: 1688715000.0
Time difference in seconds: 2678400.0
strftime(), strptime()
strftime(format[, t]) 將 struct_time 物件轉換為 format 指定的 String。 format 詳見:strftime()。 如果未提供 t,則使用 localtime() 返回的當前時間。 format 必須是字符串。
strptime(string[, format]) 根據 format 解析表示時間的 string,返回 struct_time 物件。
time module 的 struct_time 不夠複雜,無法計算不同年份的日期之間的天數,為此我們可以使用使用 datetime module。
PYTHON
'''strptime()'''import timet = time.gmtime()print(t)# =========================================================# Extract specific valuest = time.localtime()print(f"Year: {t.tm_year}")print(f"Month: {t.tm_mon}")print(f"Day of the week: {t.tm_wday}")# Extract specific values (using indices)print(f"Year: {t[0]}")print(f"Month: {t[1]}")print(f"Day of the week: {t[6]}")# =========================================================# Calculate the number of days between two datesdate1 ="2023-07-01"date2 ="2023-07-07"struct_time1 = time.strptime(date1, "%Y-%m-%d")struct_time2 = time.strptime(date2, "%Y-%m-%d")days_difference = struct_time2.tm_yday - struct_time1.tm_ydayprint(f"The number of days between {date1} and {date2} is {days_difference}.")# =========================================================# Calculate the number of days between dates from different yearsfrom datetime import datetimedate1 ="2023-07-01"date2 ="2024-07-07"# Convert strings to datetime objectsdatetime1 = datetime.strptime(date1, "%Y-%m-%d")datetime2 = datetime.strptime(date2, "%Y-%m-%d")# Subtract datetimes and extract the number of daysdays_difference = (datetime2 - datetime1).daysprint(f"The number of days between {date1} and {date2} is {days_difference}.")
執行結果:
time.struct_time(tm_year=2023, tm_mon=7, tm_mday=7,
tm_hour=13, tm_min=55, tm_sec=10, tm_wday=4, tm_yday=188, tm_isdst=0)
Year: 2023
Month: 7
Day of the week: 4
Year: 2023
Month: 7
Day of the week: 4
The number of days between 2023-07-01 and 2023-07-07 is 6.
The number of days between 2023-07-01 and 2024-07-07 is 372.