栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python标准库datetime之time模块详解

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python标准库datetime之time模块详解

Python标准库datetime之time模块详解

上一篇博文中我们学习了datetime库中的日期模块(date),本文我们来学习下时间模块(time)。
传送门:点击即可跳转日期模块

1、时间对象类
  • 主要处理时、分、秒操作
  • 创建对象常用的参数有(小时,分钟,秒,微秒)
  • 创建对象的函数为datetime.time()
  • 使用datetime.time()按传入的参数创建对象,不传参时创建最小的时间对象
  • 1小时 = 60分钟 = 3600秒 = 36000分秒 = 360000厘秒 = 3600000毫秒 = 36000000000微秒
  • 1微秒 = 1000纳秒 = 1000000皮秒
2、查看极值
# 时间最大值
print(datetime.time.max)                # 23:59:59.999999
print(type(datetime.time.max))          # 
# 时间最小值
print(datetime.time.min)                # 00:00:00
print(type(datetime.time.min))          # 
# time对象之间最小可解析的差值(1微秒)
print(datetime.time.resolution)         # 0:00:00.000001
print(type(datetime.time.resolution))   # 

3、时间对象的创建
  • 创建对象函数datetime.time(hour, minute, second, microsecond)
  • 参数顺序从左到右依次是:hour(小时)、minute(分钟)、second(秒)、microsecond(微秒)
  • 按位置传参数时必须保证参数值在规定的范围内且顺序依次为小时、分钟、秒、微秒
  • 推荐使用关键字传参创建
print(datetime.time())                      # 00:00:00
print(datetime.time(12))                    # 12:00:00
print(datetime.time(12, 2))                 # 12:02:00
print(datetime.time(12, 2, 54))             # 12:02:54
print(datetime.time(12, 2, 54, 999999))     # 12:02:54.999999
  • 关键字传参,按传入的参数创建对象
print(datetime.time(microsecond=99999))     # 00:00:00.099999
print(datetime.time(microsecond=99999, second=8, hour=9, minute=59))     # 09:59:08.099999

4、时间替换函数
  • 时间替换函数:replace(self , hour, minute, second, microsecond)
  • self是一个必传的参数(datetime.time对象),hour、minute、 second、 microsecond参数可选
  • 按位置传参数时必须保证参数值在规定的范围内
  • 顺序从左到右依次是:hour(小时)、minute(分钟)、second(秒)、microsecond(微秒)
4.1、缺少参数错误示例
# TypeError: unbound method time.replace() needs an argument
print(datetime.time.replace()) 

4.2、参数值错误示例
my_time = datetime.time(microsecond=99999, second=8, hour=9, minute=59)
print(datetime.time.replace(my_time, 25), my_time)

4.3、常见用法
# 时间替换
# 创建对象
my_time = datetime.time(microsecond=99999, second=8, hour=9, minute=59)
# 只替换小时
print(datetime.time.replace(my_time, hour=23), my_time)             # 23:59:08.099999 09:59:08.099999
print(datetime.time.replace(my_time, 23), my_time)                  # 23:59:08.099999 09:59:08.099999
# 只替换分钟数
print(datetime.time.replace(my_time, minute=23), my_time)           # 09:23:08.099999 09:59:08.099999
# 按位置传参替换分钟,必须传小时、分钟两个位置参数,否则报语法错误
print(datetime.time.replace(my_time, 9, 23), my_time)               # 09:23:08.099999 09:59:08.099999
# 只替换秒数
print(datetime.time.replace(my_time, second=23), my_time)           # 09:59:23.099999 09:59:08.099999
# 同理,按位置传参替换秒数时,必须传小时、分钟、秒数三个位置参数
print(datetime.time.replace(my_time, 9, 59, 15), my_time)           # 09:59:15.099999 09:59:08.099999
# 只替换微妙数
print(datetime.time.replace(my_time, microsecond=23), my_time)      # 09:59:08.000023 09:59:08.099999
# 同理,按位置传参替换微妙数时,必须传小时、分钟、秒数、微秒数三个位置参数
print(datetime.time.replace(my_time, 9, 59, 8, 23), my_time)       # 09:59:08.000023 09:59:08.099999

5、时间对象的属性
  • 对象的属性(时、分、秒、微秒)
my_time = datetime.time(microsecond=99999, second=8, hour=9, minute=59)
# 获取时间对象的属性(时、分、秒、微秒)
print(f"{my_time}中的小时为:{my_time.hour}")             # 09:59:08.099999中的小时为:9
print(f"{my_time}中的分钟为:{my_time.minute}")           # 09:59:08.099999中的分钟为:59
print(f"{my_time}中的秒数为:{my_time.second}")           # 09:59:08.099999中的秒数为:8
print(f"{my_time}中的微秒为:{my_time.microsecond}")      # 09:59:08.099999中的微秒为:99999
# 结合时间对象属性,我们就可以按位置传参进行单个属性的替换了
print(datetime.time.replace(my_time, my_time.hour, my_time.minute, my_time.second, 23))     # 09:59:08.000023

6、格式化时间
  • 常用的格式 %H(小时)、%M(分钟)、%S(秒)、%f(微秒)、%h(简写的月份名,一般为英文简写)
# 创建对象
my_time = datetime.time(microsecond=99999, second=8, hour=9, minute=59)
# 常用的格式 %H(小时)、%M(分钟)、%S(秒)、%f(微秒)、%h(简写的月份名,一般为英文简写)
print(f"{my_time}格式化后是:", datetime.time.strftime(my_time, "%H:%M:%S.%f"))              # 09:59:08.099999
print(f"{my_time}格式化后是:", datetime.time.strftime(my_time, "%h:%M:%S.%f"))
print(f"{my_time}格式化的类型是:", type(datetime.time.strftime(my_time, "%H:%M:%S.%f")))     # 
# 保留至毫秒,利用字符串切片实现
print(f"{my_time}格式化后是:", datetime.time.strftime(my_time, "%H:%M:%S.%f")[:-3])         # 格式化后是: 09:59:08.099

7、ISO标准时间格式转换
  • ISO标准的时间格式(两位小时:两位分钟:两位秒.6位微秒)。如08:07:06:000006)
7.1、将时间对象转化为ISO标准格式的字符串
print(datetime.time.isoformat(my_time), type(datetime.time.isoformat(my_time)))     # 08:07:06.000006 
print(datetime.time(12), datetime.time.isoformat(datetime.time(12)))                # 12:00:00 12:00:00
# 转换前后类型查看, 转换前:,转换后:
print(type(datetime.time(12)), type(datetime.time.isoformat(datetime.time(12))))  

7.2、将ISO标准时间格式的字符串转换为时间类型(datetime.time)
# 创建对象
my_time = datetime.time(microsecond=6, second=6, hour=8, minute=7)
# 将ISO标准时间格式的字符串转换为时间类型(datetime.time)
# 08:07:06.000001 
print(datetime.time.fromisoformat("08:07:06.000001"), type(datetime.time.fromisoformat("08:07:06.000001")))

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1009377.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号