use:
时间模块, 提供了各种与时间相关的函数
import:
import time
Common method:
time.time() |
get timestamp
1661524128.4208298
|
time.ctime() |
Get the current time and return a human-readable string Fri Aug 26 14:44:07 2022
|
time.sleep(secs) |
Pause execution for the given number of seconds , time.sleep(1) pauses for 1 second |
time.localtime() | Convert a timestamp to a struct_time of the current time zone
time.struct_time(tm_year=2022, tm_mon=8, tm_mday=26, tm_hour=14, tm_min=28, tm_sec=48, tm_wday=4, tm_yday=238, tm_isdst=0)
|
time.gmtime() | struct_time that converts a timestamp to UTC time zone (time zone 0)
time.struct_time(tm_year=2022, tm_mon=8, tm_mday=26, tm_hour=14, tm_min=31, tm_sec=25, tm_wday=4, tm_yday=238, tm_isdst=0)
|
time.strptime('2022-08-26 ', '%Y-%m-%d') |
Parse a time string into a time tuple time.struct_time(tm_year=2022, tm_mon=8, tm_mday=26, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=4, tm_yday=238, tm_isdst=-1)
|
time.strftime("%Y-%m-%d %X",time.localtime()) #%X local corresponding time representation |
Takes a tuple of times and returns the local time as a readable string 2022-08-26 14:34:12
|
time.asctime(time.localtime()) |
Takes a time tuple and returns a readable form Fri Aug 26 14:53:59 2022 |
Example:
import time # 导入的是time
def precision(num):
n = str(num).split('e')
if len(n) == 1:
return 0
x = 0 if len(n[0].split('.')) == 1 else len(n[0].split('.')[1])
return x + abs(int(n[1]))
# print(time.__doc__) # 获取文档信息
print(time.asctime()) # 默认当前时间
print(time.time()) # 返回自历元以来的当前时间(以秒为单位)。
time.sleep(1) # 等待1秒 seconds: [名词] 秒的复数 second【形容词】第二的
print(time.clock()) # time.clock() 返回第一次调用该方法到现在的秒数,其精确度高于1微秒
print(time.perf_counter())
print("perf_counter:" + str(time.perf_counter())) # 用于基准测试的性能计数器
print('{num:.{precision}f}'.format(num=time.perf_counter(),
precision=precision(time.perf_counter()))) # 使用字符串格式将您的数字打印到所需的精度
print(time.ctime()) # 获取当前时间 str
print(time.gmtime()) # 返回当前时间
print(time.localtime())
# print(time.mktime((2022,8,15)))
print(time.strftime('%Y-%m-%d')) # 格式化时间 2022-08-26
print(time.strftime('%Y-%m-%d %H:%M:%S')) # 格式化时间 2022-08-26 09:27:38
print(time.strftime('%A')) # Friday
print(time.strftime('%B')) # August
print(time.strftime('%I')) # 返回当前时间点 如9点 12小时制
t = time.gmtime()
print(time.strftime('%Y-%m-%d %H:%M:%S', t))
time_tuple = time.localtime()
string = time.strftime('%Y年%m月%d日'.encode('unicode_escape').decode('utf8'), time_tuple).encode('utf-8').decode(
'unicode_escape')
print(string) # 打印带中文年月日的日期 2022年08月26日
str1 = time.strftime("%Y{}%m{}%d{} %H{}%M{}%S{}", time_tuple)
print(str1.format('年', '月', '日', '时', '分', '秒')) # 2022年08月26日 09时51分36秒
"""
Commonly used format codes:
%Y Year with century as a decimal number.
%m Month as a decimal number [01,12].
%d Day of the month as a decimal number [01,31].
%H Hour (24-hour clock) as a decimal number [00,23].
%M Minute as a decimal number [00,59].
%S Second as a decimal number [00,61].
%z Time zone offset from UTC.
%a Locale's abbreviated weekday name.
%A Locale's full weekday name.
%b Locale's abbreviated month name.
%B Locale's full month name.
%c Locale's appropriate date and time representation.
%I Hour (12-hour clock) as a decimal number [01,12].
%p Locale's equivalent of either AM or PM.
"""
str2 = '2022-02-02 12:45:36'
print(time.strptime(str2, '%Y-%m-%d %H:%M:%S'))
print(time.strptime(str2, '%Y-%m-%d %H:%M:%S')[0])
Summarize:
The above is the content of today, I hope to help you.