python中time库的使用

time库介绍:python中time库的使用

time库包括三类函数:

1、时间获取:time() ctime() gtime
2、时间格式化:strftime() strptime()
3、程序计时:sleep() perf_counter()

1、时间获取
python中time库的使用
python中time库的使用

2、时间格式化
即将时间以合理的方式展示出来

格式化:类似字符串的格式化,需要有展示模板
展示模板由特定的格式化控制符组成
函数:strftime()

python中time库的使用

特定的格式化字符的表示:

python中time库的使用

python中time库的使用

另外,也可以使用strptime()函数将一串时间的字符串转化为时间表示格式。如将“20190725”转化为时间的表达式,因为只有在特殊情况下才会使用,在此不多介绍。

3、程序计时
perf_counter()函数,计时
python中time库的使用

sleep()函数,产生时间
python中time库的使用
总结:
time库的三类函数:

1、时间获取:time() ctime() gtime

2、时间格式化:strftime() strptime()

3、程序计时:sleep() perf_counter()