提示:python对事件的处理
获取当前时间戳
time.time() - 功能简述:获取当前时间戳
作用:功能详述:获取当前时间戳(从世界标准时间的1970年1月1日00:00:00开始到当前这一时刻为止的总秒数),即计算机内部时间值,浮点数。
def test_time_time():
print(f'### {sys._getframe().f_code.co_name}')
print(time.time())
获取结构化时间
time.localtime() - 功能简述:获取结构化时间
time.gmtime() - 功能简述:获取结构化时间
作用:功能详述:获取当前时间,表示为计算机可处理的时间格式(struct_time格式)
def test_time_localtime():
print(f'### {sys._getframe().f_code.co_name}')
print(f'### 无参数 {time.localtime()}')
print(f'### 有参数,且参数为浮点数 {time.localtime(34.54)}') # 参数为浮点数
def test_time_gmtime():
print(f'### {sys._getframe().f_code.co_name}')
print(f'### 无参数 {time.gmtime()}')
print(f'### 有参数,且参数为浮点数 {time.gmtime(34.54)}') # 参数为浮点数
易读方式表示当前时间
time.strftime() - 功能简述:将时间以合理的方式展示出来
作用:功能详述:将时间以合理的方式展示出来
def test_time_ctime():
print(f'### {sys._getframe().f_code.co_name}')
print(f'### 无参数 {time.ctime()}')
将时间以合理的方式展示出来
time.strptime() - 功能简述:将该格式的字符串输出为struct_time.
作用:功能详述:将时间以合理的方式展示出来
def test_time_strftime():
print(f'### {sys._getframe().f_code.co_name}')
t=time.localtime()
print(f'### %Y-%m-%d %H:%M:%S {time.strftime("%Y-%m-%d %H:%M:%S",t)}')
print(f'### %B-%b-%a %A:%l:%p {time.strftime("%B-%b-%a %A:%l:%p",t)}')
将该格式的字符串输出为struct_time.
def test_time_strptime():
print(f'### {sys._getframe().f_code.co_name}')
print(time.strptime("2018-1-26 12:55:20",'%Y-%m-%d %H:%M:%S'))
程序计时
time.perf_counter() - 功能简述:程序计时
作用:功能详述:程序计时指测量起止动作所经历时间的过程
def test_time_perf_counter():
print(f'### {sys._getframe().f_code.co_name}')
start = time.perf_counter()
end = time.perf_counter()
print(f'### start {start}')
print(f'### end {end}')
print(f'### end - start {end - start}')