Arrow

安装

pip install arrow

使用

获取当前时间

返回的是Arrow对象

arrow.now()
# 2022-05-06T09:32:41.296610+08:00

arrow.utcnow()
# 2022-05-06T01:50:44.670980+00:00

获取当前时间戳

arrow.now().timestamp
# 1651800761

arrow.now().float_timestamp
# 1651800761.29661

获取datetime属性值

获取当前时间的年、月、日、时、分、秒

now = arrow.now()  # 2022-05-06T09:32:41.296610+08:00

now.year  # 2022
now.month  # 5
now.day  # 6
now.hour  # 9
now.minute  # 32
now.second  # 41 

将arrow对象转为字符串

将arrow对象转为字符串格式化输出

now = arrow.now()  # 2022-05-06T09:32:41.296610+08:00

now.format()  # '2022-05-06 09:32:41+08:00'
now.format('YYYY-MM-DD HH:mm:ss ZZ')  # '2022-05-06 09:32:41 +08:00'
now.format('YYYY-MM-DD HH:mm:ss')  # '2022-05-06 09:32:41'
now.format('YYYYMMDD')  # '20220506'
now.format('X')  # '1651800761'  字符串格式的时间戳
now.format('MMMM')  # 'May' 英文的月份

将时间戳转化为arrow对象

arrow.get(1651800761)  #  2022-05-06T01:32:41+00:00

# 转换为字符串
arrow.get(1651800761).format('YYYY-MM-DD HH:mm:ss')  # '2022-05-06 09:32:41'

从字符串中获取时间

日期和时间格式的任一侧都可以用以下列表中的一个标点符号分隔:,.;:?!"\‘[]{}()<>`

arrow.get("Cool date: 2019-10-31T09:12:45.123456+04:30.", "YYYY-MM-DDTHH:mm:ss.SZZ")  # <Arrow [2019-10-31T09:12:45.123456+04:30]>
arrow.get("Tomorrow (2019-10-31) is Halloween!", "YYYY-MM-DD")  # <Arrow [2019-10-31T00:00:00+00:00]>
arrow.get("Halloween is on 2019.10.31.", "YYYY.MM.DD")  # <Arrow [2019-10-31T00:00:00+00:00]>

# 错误示例
arrow.get("It's Halloween tomorrow (2019-10-31)!", "YYYY-MM-DD")  # 引发异常,因为日期后面有多个标点符号

字符串转换为时间戳

arrow.get("2022-05-01").int_timestamp  # 1651363200
arrow.get("2022-05-01").float_timestamp  # 1651363200.0
arrow.get("2022-05-01").timestamp()  # 1651363200.0

arrow对象转换为datetime对象

arrow.now().datetime  # 2022-05-06 09:32:41.296610+08:00

时间偏移

shift()

now = arrow.now()  # 2022-05-06T09:32:41.296610+08:00

# 后3天
now.shift(days=3).format('YYYY:MM:DD HH:mm:ss')  # '2022:05:09 09:32:41'

# 前2天
now.shift(days=-2).format('YYYY:MM:DD HH:mm:ss')  # '2022:05:04 09:32:41'

# 上1年
now.shift(years=-1).format('YYYY:MM:DD HH:mm:ss')  # '2021:05:04 09:32:41'

# 下2个月
now.shift(months=2).format('YYYY:MM:DD HH:mm:ss')  # '2022:07:06 09:32:41'

# 2小时后
now.shift(hours=2).format('YYYY:MM:DD HH:mm:ss')  # '2022:05:06 11:32:41'

# 1分钟前
now.shift(minutes=-1).format('YYYY:MM:DD HH:mm:ss')  # '2022:05:06 09:34:41'

# 30秒前
now.shift(seconds=-30).format('YYYY:MM:DD HH:mm:ss')  # '2022:05:06 09:34:11'

也可以多个参数同时使用

# 30秒前
now.shift(hours=2, minutes=-1, seconds=-30).format('YYYY:MM:DD HH:mm:ss') 

其它方法

humanize() 人性化输出

本地化、人性化表示返回相对时间差异

now = arrow.now()
now.humanize()  # '26 seconds ago'
now.humanize(locale='zh-cn')  # ‘26秒前’


now = arrow.now()
future = now.shift(hours=2)
future.humanize(now, locale='zh-cn')  # '2小时后'

span() 获取任何单位的时间跨度

now.span('hour')  # (<Arrow [2022-05-06T15:00:00+08:00]>, <Arrow [2022-05-06T15:59:59.999999+08:00]>)

floor() # 时间所在区间的开始

now.floor('hour')  # <Arrow [2022-05-06T15:00:00+08:00]>

ceil() # 时间所在区间的结尾

now.ceil('hour')  # <Arrow [2022-05-06T15:59:59.999999+08:00]>

源码解析

以下列举一些库中重要的文件及关键的方法,目前安装的是Arrow1.2.2版本

Python基础库

在arrow.py文件中,导入了calendar、datetime、time、dateutil时间处理库

本作品采用《CC 协议》,转载必须注明作者和本文链接
vance
vance
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!