tuoheng_algN/test/语法/time/test.py

43 lines
1.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import time
# 获取当前时间戳 1685344329.938672
print(time.time())
# 获取当前时间,表示为计算机可处理的时间格式
print(time.gmtime())
# 时间格式化:将时间以合理的方式展示出来
print(time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()))
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())))
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
#
print(time.strptime("2023-05-29 07:21:07", "%Y-%m-%d %H:%M:%S"))
# 返回一个CPU级别的精确时间计数值, 单位为秒, 由于这个计算值七点不确定,连续调用差值才有意义
print(time.perf_counter())
# 获取当前时间
print(time.localtime(time.time()))
print(time.localtime())
# 接受时间元组并返回一个可读的形式为"Tue Dec 11 18:07:14 2008"2008年12月11日 周二18时07分14秒的24个字符的字符串。
print(time.asctime(time.localtime(time.time())))
print(time.asctime(time.localtime()))
# 获取当前时间并以易读方式表示,返回字符串
print(time.ctime()) # Mon May 29 15:16:00 2023
# 返回格林威治西部的夏令时地区的偏移秒数。如果该地区在格林威治东部会返回负值(如西欧,包括英国)。对夏令时启用地区才能使用。
print(time.altzone)
#
start = time.perf_counter()
start1 = time.process_time()
time.sleep(1)
end = time.perf_counter()
end1 = time.process_time()
print(end - start)
print(end1 - start1)
# 接受时间对象并返回时间戳1970纪元后经过的浮点秒数
print(time.mktime(time.localtime()))