最後更新: 2022-08-05
目錄
- sleep()
- time()
- localtime()
- ctime()
- strftime()
- asctime()
import:
import time[,..]
sleep()
# Time in seconds.
time.sleep(0.1) # 0.1 second
time(): Return the time in seconds since the epoch
time.time() # Result: 1463623486.037193
localtime(): epoch -> truple
>>> time.localtime() # 另有 time.gmtime()
time.struct_time(tm_year=2016, tm_mon=5, tm_mday=19, tm_hour=10, tm_min=5, tm_sec=7, tm_wday=3, tm_yday=140, tm_isdst=0)
P.S.
# TypeError: structseq index must be integer
curr_year = time.localtime()
curr_year[0]
2016
ctime(): convert time in seconds to string
'Mon Dec 3 00:03:24 2012'
strftime(): 格式化輸出 (parse string to time tuple)
# Convert a tuple or struct_time representing a time as returned by gmtime() or localtime()
# to a string as specified by the format
# time.strftime(format[, t])
Usage:
>>> time.strftime("%Y-%m-%d %H:%M:%S")
'2013-12-13 17:50:58'
>>> time.strftime("%H %M %S", t1)
'20 00 1900 01 20'
asctime()
>>> time.asctime()
'Wed Oct 12 17:55:20 2016'
>>> time.asctime( time.localtime(time.time()) )
'Wed Oct 12 17:47:56 2016'
格式化輸入 (strptime):
# Parses str according to format string fmt and returns the instant in time-tuple format.
# time.strptime(str[, format])
t1 = time.strptime("20:00:00", '%H:%M:%S')