python之time模块介绍

2022年8月26日 247点热度 0人点赞 0条评论

用途:

时间模块, 提供了各种与时间相关的函数

导入:

import time

常用方法:

time.time()
获取时间戳 

1661524128.4208298
time.ctime()

获取当前时间,并返回一个以人类可读方式的字符串

Fri Aug 26 14:44:07 2022
time.sleep(secs)
暂停执行给定的秒数time.sleep(1) 暂停1秒
time.localtime() 将一个时间戳转换为当前时区的struct_time

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() 将一个时间戳转换为UTC时区(0时区)的struct_time

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')

把一个时间字符串解析为时间元组

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 本地相应的时间表示

接收以时间元组,并返回以可读字符串表示的当地时间

2022-08-26 14:34:12
time.asctime(time.localtime())

接受时间元组并返回一个可读的形式

Fri Aug 26 14:53:59 2022

示例:

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()) # 获取当前时间 strprint(time.gmtime()) # 返回当前时间print(time.localtime())# print(time.mktime((2022,8,15)))print(time.strftime('%Y-%m-%d')) # 格式化时间 2022-08-26print(time.strftime('%Y-%m-%d %H:%M:%S')) # 格式化时间 2022-08-26 09:27:38print(time.strftime('%A')) # Fridayprint(time.strftime('%B')) # Augustprint(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])

总结:

以上就是今天的内容,希望对你有所帮助。

81320python之time模块介绍

这个人很懒,什么都没留下

文章评论