python: 第三方时间库 arrow

 

import arrow

print("-"*25 + "获取当前时间" + "-"*25)
nowtime = arrow.now()
print(nowtime)
print(type(nowtime))

print("-"*25 + "获取当前时间(utc 世界标准时间)" + "-"*25)
utc_nowtime = arrow.utcnow()
print(utc_nowtime)
print(type(utc_nowtime))

print("-"*25 + "获取当前时间一个小时之前的时间" + "-"*25)
new_nowtime = nowtime.shift(hours=-1)
print(new_nowtime)

print("-"*25 + "调整时间" + "-"*25)
new_nowtime = nowtime.shift(days=+1, hours=-1)
print(new_nowtime)

print("-"*25 + "修改时间" + "-"*25)
new_nowtime = nowtime.replace(hour=4, minute=40)
print(new_nowtime)

print("-"*25 + "utc 时间转成当前时区的时间(转换时区)" + "-"*25)
local = utc_nowtime.to('Asia/Shanghai')
print(local)

print("-"*25 + "文本转时间对象" + "-"*25)
print(arrow.get('2020-05-11T21:23:58.970460+07:00'))

print("-"*25 + "当前时间的时间戳" + "-"*25)
now_timestamp = nowtime.timestamp
print(now_timestamp)

print("-"*25 + "时间戳转时间" + "-"*25)
new_nowtime = arrow.get(now_timestamp).to('Asia/Shanghai')
print(new_nowtime)

print("-"*25 + "当前时间格式化" + "-"*25)
print(nowtime.format())
print(nowtime.format("YYYY-MM-DD HH:mm:ss ZZ"))
print(nowtime.format("YYYY-MM-DD HH:mm:ss"))

print("-"*25 + "从string中解析时间对象" + "-"*25)
print(arrow.get('2020-06-12 18:52:45', 'YYYY-MM-DD HH:mm:ss'))
print(arrow.get('June was born in May 1980', 'MMMM YYYY'))

print("-"*25 + "转为标准对象库" + "-"*25)
print(nowtime.datetime)
print(nowtime.date())
print(nowtime.time())
print("当前年:{}".format(nowtime.year))
print("当前月:{}".format(nowtime.month))
print("当前日:{}".format(nowtime.day))
print("当前时:{}".format(nowtime.hour))
print("当前分:{}".format(nowtime.minute))
print("当前秒:{}".format(nowtime.second))
print("ctime 格式:{}".format(nowtime.ctime()))    # Fri Jun 12 15:49:56 2020
print("以整数形式返回星期几(0-6):{}".format(nowtime.weekday()))
print("以整数形式返回一周中的ISO日(1-7): {}".format(nowtime.isoweekday()))
print("返回3元组(ISO年,ISO周数,ISO工作日): {}".format(nowtime.isocalendar()))
print("返回日期的格雷戈里序数 : {}".format(nowtime.toordinal()))

print("-"*25 + '获取任意时间单位的时间跨度' + '-'*25)
print(arrow.now().span('hour'))
print(arrow.now().span('day'))
print(arrow.now().span('month'))
print(arrow.now().span('year'))

print("-"*25 + '得到任意单位时间中的最大值或最小值' + '-'*25)
print(arrow.now().floor('hour'))
print(arrow.now().ceil('hour'))
print(arrow.now().floor('day'))
print(arrow.now().ceil('day'))


print("-"*25 + "人性化时间" + "-"*25)
print(nowtime.humanize())                 # just now
print(nowtime.humanize(locale='zh_CN'))   # 刚才

"""打印:

-------------------------获取当前时间-------------------------
2020-06-12T15:58:52.729652+08:00
<class 'arrow.arrow.Arrow'>

-------------------------获取当前时间(utc 世界标准时间)-------------------------
2020-06-12T07:58:52.729749+00:00
<class 'arrow.arrow.Arrow'>

-------------------------获取当前时间一个小时之前的时间-------------------------
2020-06-12T14:58:52.729652+08:00

-------------------------调整时间-------------------------
2020-06-13T14:58:52.729652+08:00

-------------------------修改时间-------------------------
2020-06-12T04:40:52.729652+08:00

-------------------------utc 时间转成当前时区的时间(转换时区)-------------------------
2020-06-12T15:58:52.729749+08:00

-------------------------文本转时间对象-------------------------
2020-05-11T21:23:58.970460+07:00

-------------------------当前时间的时间戳-------------------------
1591948732

-------------------------时间戳转时间-------------------------
2020-06-12T15:58:52+08:00

-------------------------当前时间格式化-------------------------
2020-06-12 15:58:52+08:00
2020-06-12 15:58:52 +08:00
2020-06-12 15:58:52

-------------------------从string中解析时间对象-------------------------
2020-06-12T18:52:45+00:00
1980-05-01T00:00:00+00:00

-------------------------转为标准对象库-------------------------
2020-06-12 15:58:52.729652+08:00
2020-06-12
15:58:52.729652
当前年:2020
当前月:6
当前日:12
当前时:15
当前分:58
当前秒:52
ctime 格式:Fri Jun 12 15:58:52 2020
以整数形式返回星期几(0-6):4
以整数形式返回一周中的ISO日(1-7): 5
返回3元组(ISO年,ISO周数,ISO工作日): (2020, 24, 5)
返回日期的格雷戈里序数 : 737588

-------------------------获取任意时间单位的时间跨度-------------------------
(<Arrow [2020-06-12T15:00:00+08:00]>, <Arrow [2020-06-12T15:59:59.999999+08:00]>)
(<Arrow [2020-06-12T00:00:00+08:00]>, <Arrow [2020-06-12T23:59:59.999999+08:00]>)
(<Arrow [2020-06-01T00:00:00+08:00]>, <Arrow [2020-06-30T23:59:59.999999+08:00]>)
(<Arrow [2020-01-01T00:00:00+08:00]>, <Arrow [2020-12-31T23:59:59.999999+08:00]>)

-------------------------得到任意单位时间中的最大值或最小值-------------------------
2020-06-12T15:00:00+08:00
2020-06-12T15:59:59.999999+08:00
2020-06-12T00:00:00+08:00
2020-06-12T23:59:59.999999+08:00

-------------------------人性化时间-------------------------
just now
刚才

"""

 

posted @ 2020-06-12 16:01  KeenLeung  阅读(322)  评论(0编辑  收藏  举报