python-time库的使用

 time库的使用

1.时间获取,

 

 2.时间格式化类似字符格式化需要一个展示模板

 

 %Y 年份; %m 月份;%B 月份名称;%b 月份名称缩写;%A 星期(Monday~Sunday);

%a 星期缩写(Mon~Sun);%H 小时(24h); %I  小时 (12h); %p上午/下午; %M 分钟; %S 秒

 

strftime() 和 strptime() 互为一对函数。

strftime() 计算机可操作的时间变成字符串

strptime() 字符串变计算机可操作的时间

3.程序计时:程序计时指测量起止动作所经历时间的过程

测量时间:perf_counter()

产生时间:sleep()  //让机器休眠或产生一定的时间

 

例子

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("
{:^3.0f}%[{}->{}]{:.2f}s".format(c,a,b,dur),end='')
    time.sleep(0.1)
print("
"+"执行结束".center(scale//2,'-'))

  

原文地址:https://www.cnblogs.com/ShallByeBye/p/12462124.html