– Start
点击此处观看本系列配套视频。
from datetime import time, timezone, timedelta
china_tz = timezone(timedelta(hours=8), 'Asia/Shanghai')
# 构造 time(hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)
t = time(16, 28, 22)
t = time(16, 28, 22, 999)
t = time.fromisoformat('16:28:22')
t = time(16, 28, 22, 999, china_tz)
# 类属性
print(time.min) # 00:00:00
print(time.max) # 23:59:59.999999
print(time.resolution) # 0:00:00.000001
# 实例属性
print(t.hour) # 16
print(t.minute) # 28
print(t.second) # 22
print(t.microsecond) # 999
print(t.tzinfo) # Asia/Shanghai
print(t.fold) # 0
# 方法
print(t.isoformat()) # 16:28:22.000999+08:00
print(t.strftime('%H:%M:%S')) # 16:28:22
# time 是不可变的,replace 不会修改原 time
next_hour = t.replace(hour=t.hour + 1)
print(next_hour)
print(t.tzname()) # 时区名
print(t.utcoffset()) # 相对 UTC 时差
print(t.dst()) # 转换为夏令时
# 比较时间
t1 = time(16, 28, 22)
t2 = time(16, 28, 23)
if t1 < t2:
print(f'{t1} is before {t2}')
– 更多参见:Python 精萃
– 声 明:转载请注明出处
– Last Updated on 2018-09-22
– Written by ShangBo on 2018-09-22
– End