发布时间:2022-04-14 09:47:16 人气:140 作者:多测师
time包基于C语言的库函数(library functions)。Python的解释器通常是用C编写的,Python的一些函数也会直接调用C语言的库函数。
import time
print(time.time()) # wall clock time, unit: second
print(time.clock()) # processor clock time, unit: second
time.sleep()可以将程序置于休眠状态,直到某时间间隔之后再唤醒程序,让程序继续运行。
import time
print('start')
time.sleep(10) # sleep for 10 seconds
print('wake up')
当我们需要定时地查看程序运行状态时,就可以利用该方法。
time包还定义了struct_time对象。该对象实际上是将挂钟时间转换为年、月、日、时、分、秒……等日期信息,存储在该对象的各个属性中(tm_year, tm_mon, tm_mday...)。下面方法可以将挂钟时间转换为struct_time对象:
st = time.gmtime() # 返回struct_time格式的UTC时间
st = time.localtime() # 返回struct_time格式的当地时间, 当地时区根据系统环境决定。
s = time.mktime(st) # 将struct_time格式转换成wall clock time
以上内容为大家介绍了Python中的time包,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注多测师。https://www.e70w.com/xwzx/