Python 日期模块 -- time

– 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值