PythonArrow日期时间模块
Python Arrow 日期时间模块1.arrow 简介
Arrow是一个Python库,为创建、操作、格式化和转换日期、时间和时间戳提供了一种合理且人性化的方法。它实现并更新datetime类型,填补功能上的空白,并提供支持许多常见创建场景的智能模块API。简单地说,它可以帮助您使用更少的导入和更少的代码来处理日期和时间。
Arrow 以时间之箭( arrow of time )命名,灵感来源于moment.js 和 requests。 2.arrow 安装pip install arrow3.使用示例>>>import arrow >>>arrow.get("2013-05-11T21:23:58.970460+07:00") >>>utc = arrow.utcnow() >>>utc >>>utc = utc.shift(hours=-1) >>>utc >>>local = utc.to("US/Pacific") >>>local >>>local.timestamp() 1368303838.970460 >>>local.format() "2013-05-11 13:23:58 -07:00" >>>local.format("YYYY-MM-DD HH:mm:ss ZZ") "2013-05-11 13:23:58 -07:00" >>>local.humanize() "an hour ago" >>>local.humanize(locale="ko-kr") "한시간 전"4.创建时间4.1 UTC时间
在实际中,我们需要一个全球时间。 全球时间可以避免时区和夏令时的混淆。 UTC(世界标准时间)是主要时间标准。 UTC 用于航空,天气预报,飞行计划,空中交通管制通关和映射。 与当地时间不同,UTC 不会随季节的变化而变化。 >>> import arrow >>> arrow.utcnow() 4.2本地时间
本地时间是特定区域或时区中的时间。 >>> arrow.now() >>> arrow.now("US/Pacific") 4.3时间戳
时间戳的数据类型为 int 或 float,通过时间戳创建时间: >>> arrow.get(1367900664) >>> arrow.get(1367900664.152325) >>>4.4使用 datetime 及时区>>> from datetime import datetime >>> arrow.get(datetime.utcnow()) >>> arrow.get(datetime(2022, 5, 7), "Asia/Shanghai") >>> from dateutil import tz >>> arrow.get(datetime(2013, 5, 5), tz.gettz("Asia/Shanghai")) >>> arrow.get(datetime(2013, 5, 7), tz.gettz("Asia/Shanghai")) >>> arrow.get(datetime.now(tz.gettz("Asia/Shanghai"))) 4.5解析字符串>>> arrow.get("2013-05-07 12:30:45", "YYYY-MM-DD HH:mm:ss") 4.6解析符合ISO 8601字符串(无格式字符串)>>> arrow.get("2013-05-07T15:35:00.000-07:00") 4.7实例化 Arrowclass arrow.arrow.Arrow(year, month, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, **kwargs)
参数: year ( int ) – 年. month ( int ) – 月. day ( int ) – 日. hour ( int ) – 时. 默认值 0. minute ( int ) – 分, 默认值 0. second ( int ) – 秒, 默认值 0. microsecond ( int ) – 毫秒. 默认值 0. tzinfo ( Union [ tzinfo , str , None ]) – 时区. 默认值 UTC. >>> arrow.get(2013, 5, 7) >>> arrow.Arrow(2013, 5, 7) 5.属性
创建 Arrow 实例后,可以直接获取其属性值。 >>> d = arrow.now() >>> d >>> d.year 2022 >>> d.month 5 >>> d.day 17 >>> d.hour 8 >>> d.minute 5 >>> d.second 7 >>> d.microsecond 2070835.1获取 datetime>>> d = arrow.now() >>> d >>> d.datetime datetime.datetime(2022, 5, 17, 8, 8, 29, 375589, tzinfo=tzlocal())5.2获取简单 datetime>>> d = arrow.utcnow() >>> d >>> d.naive datetime.datetime(2022, 5, 17, 0, 11, 5, 131193)5.3获取时区>>> d = arrow.now() >>> d.tzinfo tzlocal()5.4获取 date>>> d = arrow.now() >>> d >>> d.date() datetime.date(2022, 5, 17)5.5获取 time>>> d = arrow.now() >>> d >>> d.time() datetime.time(8, 19, 11, 103674)6.更新6.1 replace
通过更改属性得到新的时间: >>> d = arrow.now() >>> d >>> d.replace(hour=8, minute=30, second=0) >>> d.replace(tzinfo="US/Pacific") 6.2 shift
格式: shift(**kwargs)
使用复数属性名称相对移动其当前值: >>> d = arrow.now() >>> d >>> d.shift(years=9, days=10) >>> d.shift(years=-9, days=-10)
使用 weekday 参数相对移动当前值: >>> d.shift(weekday=5)
使用 weeks 参数相对移动当前值: >>> d.shift(weeks=3) 7.格式化>>> arrow.utcnow().format("YYYY-MM-DD HH:mm:ss ZZ") "2022-05-17 02:04:29 +00:00"
解析和格式化标记,与 strtime 不同。
Token
Output
Year
YYYY
2000, 2001, 2002 … 2012, 2013
YY
00, 01, 02 … 12, 13
Month
MMMM
January, February, March … 1
MMM
Jan, Feb, Mar … 1
MM
01, 02, 03 … 11, 12
M
1, 2, 3 … 11, 12
Day of Year
DDDD
001, 002, 003 … 364, 365
DDD
1, 2, 3 … 364, 365
Day of Month
DD
01, 02, 03 … 30, 31
D
1, 2, 3 … 30, 31
Do
1st, 2nd, 3rd … 30th, 31st
Day of Week
dddd
Monday, Tuesday, Wednesday … 2
ddd
Mon, Tue, Wed … 2
d
1, 2, 3 … 6, 7
ISO week date
W
2011-W05-4, 2019-W17
Hour
HH
00, 01, 02 … 23, 24
H
0, 1, 2 … 23, 24
hh
01, 02, 03 … 11, 12
h
1, 2, 3 … 11, 12
AM / PM
A
AM, PM, am, pm 1
a
am, pm 1
Minute
mm
00, 01, 02 … 58, 59
m
0, 1, 2 … 58, 59
Second
ss
00, 01, 02 … 58, 59
s
0, 1, 2 … 58, 59
Sub-second
S…
0, 02, 003, 000006, 123123123123… 3
Timezone
ZZZ
Asia/Baku, Europe/Warsaw, GMT … 4
ZZ
-07:00, -06:00 … +06:00, +07:00, +08, Z
Z
-0700, -0600 … +0600, +0700, +08, Z
Seconds Timestamp
X
1381685817, 1381685817.915482 … 5
ms or µs Timestamp
x
1569980330813, 1569980330813221 8.转换
按名称或tzinfo将UTC转换为其他时区: >>> utc = arrow.utcnow() >>> utc >>> utc.to("US/Pacific") >>> from dateutil import tz >>> utc.to(tz.gettz("US/Pacific")) >>> utc.to("Asia/Shanghai") >>>
也可以使用短格式: >>> utc.to("local") >>> utc.to("local").to("utc") >>>9.人性化
与现在相比较起来: >>> past = arrow.now().shift(hours=-1) >>> past.humanize() "an hour ago"
也可以使用另一个 Arrow 实例: >>> present = arrow.now() >>> future = present.shift(hours=2) >>> future.humanize(present) "in 2 hours"
将时间表示为相对时间,或仅包括距离: >>> present = arrow.now() >>> future = present.shift(minutes=66) >>> future.humanize(present, granularity="minute") "in 66 minutes" >>> future.humanize(present, granularity=["hour", "minute"]) "in an hour and 6 minutes" >>> present.humanize(future, granularity=["hour", "minute"]) "an hour and 6 minutes ago" >>> future.humanize(present, only_distance=True, granularity=["hour", "minute"]) "an hour and 6 minutes"
支持语言环境: >>> future = arrow.utcnow().shift(hours=1) >>> present = arrow.utcnow() >>> future.humanize(present, locale="ru") "через 59 минут" >>> future.humanize(present, locale="zh") "59分钟后"10.人性化逆运算
将人性化字符串转换成时间: >>> d = arrow.now() >>> d >>> d.dehumanize("2 days ago") >>> d.dehumanize("in a month")
支持语言环境: >>> d = arrow.now() >>> d >>> d.dehumanize("1年前",locale="zh") 11.范围和跨度
获取任何单位的时间跨度: >>> arrow.now().span("hour") (, ) >>> arrow.now().span("day") (, )
floor 与 ceil : >>> arrow.now().floor("hour") >>> arrow.now().ceil("hour")
获取时间范围: >>> start = datetime(2013, 4, 5, 14, 30) >>> end = datetime(2013, 4, 29, 15, 35) >>> for r in arrow.Arrow.span_range("hour", start, end): ... print(r) ... (, ) (, ) (, ) (, ) (, ) (, ) (, ) (, ) (, ) ......
或者只是在一段时间内迭代: >>> start = datetime(2013, 4, 5, 14, 30) >>> end = datetime(2013, 4, 29, 15, 35) >>> for r in arrow.Arrow.range("hour", start, end): ... print(repr(r)) ... ......12.运算>>> d1 = arrow.now() >>> d2 = arrow.Arrow(2013, 5, 1) >>> d1 - d2 datetime.timedelta(days=3303, seconds=12731, microseconds=797378) >>> (d1 - d2).total_seconds() 285391931.797378