time acquisition
time()
Used to obtain the current timestamp, that is, the internal time value of the computer, which is a floating point number (the number of seconds from January 1, 1970 to the present)
import time
print(time.time())
result
1642520186.166802
ctime()
Used to get the current time and represent it in a human-readable way, returning a string
import time
print(time.ctime())
result
Tue Jan 18 23:36:26 2022
gmtime()
Used to get the current time, expressed as a computer-processable time format
import time
print(time.gmtime())
result
time.struct_time(tm_year=2022, tm_mon=1, tm_mday=18, tm_hour=15, tm_min=36, tm_sec=26, tm_wday=1, tm_yday=18, tm_isdst=0)
time formatting
formatter
format string | Date/Time Description |
---|---|
%Y | years |
%m | month |
%B | month name |
%b | abbreviated month name |
%d | date |
%A | Week |
%a | week abbreviation |
%H | Hour (24h system) |
%l | Hour (12h system) |
%p | am/pm |
%M | minute |
%S | Second |
strftime(tpl,ts)
tpl is a format template string used to define the output effect
ts is the type of time variable inside the computer
import time
t=time.gmtime()
# %A 星期
# %p 上下午
#strftime()将时间按照特定格式输出
print(time.strftime("%Y-%m-%d %A %H:%M:%S %p",t))
result
2022-01-18 Tuesday 15:44:26 PM
strptime(str,tpl)
str is the time value in the form of a string
tpl is a formatted template string used to define the output effect
import time
t=time.gmtime()
#striptime()将一个时间字符串转化为计算机时间struct_time()形式
timeStr='2022-01-17 23:24:30'
print(time.strptime(timeStr,"%Y-%m-%d %H:%M:%S"))
result
time.struct_time(tm_year=2022, tm_mon=1, tm_mday=17, tm_hour=23, tm_min=24, tm_sec=30, tm_wday=0, tm_yday=17, tm_isdst=-1)
program timing
Program timing refers to the process of measuring the elapsed time of starting and stopping actions
perf_counter()
Returns a CPU-level precise time count value in seconds.
Since the starting point of this count value is uncertain, it makes sense to call the difference continuously
import time
start_time = time.perf_counter()
print(start_time)
end_time = time.perf_counter()
print(end_time)
print(end_time-start_time)
result
0.7581596
0.7581973
3.7699999999918354e-05
sleep(s)
s The sleep time, in seconds, can be a floating point number
import time
time.sleep(3)
print("程序休眠了3秒")
The program will output after 3 seconds
程序休眠了3秒
Example: Text Progress Bar
The first experience of the text progress bar
import time
scale=20
print("{:-^20}".format("执行开始"))
for i in range(scale+1):
a='='*i
b='-'*(scale-i)
c=i/scale*100
print("{:3.0f}%[{}>{}]".format(c,a,b))
time.sleep(0.2)
print("{:-^20}".format("执行结束"))
result
--------执行开始--------
0%[>--------------------]
5%[=>-------------------]
10%[==>------------------]
15%[===>-----------------]
20%[====>----------------]
25%[=====>---------------]
30%[======>--------------]
35%[=======>-------------]
40%[========>------------]
45%[=========>-----------]
50%[==========>----------]
55%[===========>---------]
60%[============>--------]
65%[=============>-------]
70%[==============>------]
75%[===============>-----]
80%[================>----]
85%[=================>---]
90%[==================>--]
95%[===================>-]
100%[====================>]
--------执行结束--------
Text progress bar single line dynamic refresh
import time
print("{:-^20}".format("执行开始"))
scale=20
for i in range(scale+1):
a='='*i
b='-'*(scale-i)
c=i/scale*100
#\r使光标回到行首,end=""使输出不换行
print("\r{:3.0f}%[{}>{}]".format(c,a,b),end="")
time.sleep(0.2)
print("\n"+"{:-^20}".format("执行结束"))
result
The full effect of the text progress bar
import time
scale=50
print("执行开始".center(scale//2,"-"))
start=time.perf_counter()
for i in range(scale+1):
a='='*i
b='-'*(scale-i)
c=i/scale*100
dur=time.perf_counter()-start
print("\r{:3.0f}%[{}>{}]{:.2f}s".format(c,a,b,dur),end="")
time.sleep(0.2)
print("\n"+"执行结束".center(scale//2,"-"))
result
The above is the content organized by the courseware of MOOC teacher Songtian, which is used for personal review