编程教育资源分享平台

网站首页 > 后端开发 正文

Python Arrow 日期时间模块

luoriw 2024-02-01 14:23:24 后端开发 10 ℃ 0 评论

Python Arrow 日期时间模块

1.arrow 简介

Arrow是一个Python库,为创建、操作、格式化和转换日期、时间和时间戳提供了一种合理且人性化的方法。它实现并更新datetime类型,填补功能上的空白,并提供支持许多常见创建场景的智能模块API。简单地说,它可以帮助您使用更少的导入和更少的代码来处理日期和时间。

Arrow 以时间之箭( arrow of time )命名,灵感来源于moment.js 和 requests。

2.arrow 安装

pip install arrow

3.使用示例

>>>import arrow
>>>arrow.get('2013-05-11T21:23:58.970460+07:00')
<Arrow [2013-05-11T21:23:58.970460+07:00]>

>>>utc = arrow.utcnow()
>>>utc
<Arrow [2013-05-11T21:23:58.970460+00:00]>

>>>utc = utc.shift(hours=-1)
>>>utc
<Arrow [2013-05-11T20:23:58.970460+00:00]>

>>>local = utc.to('US/Pacific')
>>>local
<Arrow [2013-05-11T13:23:58.970460-07:00]>

>>>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()
<Arrow [2022-05-16T13:39:17.144108+00:00]>

4.2本地时间

本地时间是特定区域或时区中的时间。

>>> arrow.now()
<Arrow [2022-05-16T21:43:50.782509+08:00]>
>>> arrow.now('US/Pacific')
<Arrow [2022-05-16T06:44:13.213732-07:00]>

4.3时间戳

时间戳的数据类型为 int 或 float,通过时间戳创建时间:

>>> arrow.get(1367900664)
<Arrow [2013-05-07T04:24:24+00:00]>
>>> arrow.get(1367900664.152325)
<Arrow [2013-05-07T04:24:24.152325+00:00]>
>>>

4.4使用 datetime 及时区

>>> from datetime import datetime
>>> arrow.get(datetime.utcnow())
<Arrow [2022-05-16T13:59:21.162927+00:00]>

>>> arrow.get(datetime(2022, 5, 7), 'Asia/Shanghai')
<Arrow [2022-05-07T00:00:00+08:00]>

>>> from dateutil import tz
>>> arrow.get(datetime(2013, 5, 5), tz.gettz('Asia/Shanghai'))
<Arrow [2013-05-05T00:00:00+08:00]>
>>> arrow.get(datetime(2013, 5, 7), tz.gettz('Asia/Shanghai'))
<Arrow [2013-05-07T00:00:00+08:00]>

>>> arrow.get(datetime.now(tz.gettz('Asia/Shanghai')))
<Arrow [2022-05-16T22:06:49.733640+08:00]>

4.5解析字符串

>>> arrow.get('2013-05-07 12:30:45', 'YYYY-MM-DD HH:mm:ss')
<Arrow [2013-05-07T12:30:45+00:00]>

4.6解析符合ISO 8601字符串(无格式字符串)

>>> arrow.get('2013-05-07T15:35:00.000-07:00')
<Arrow [2013-05-07T15:35:00-07:00]>

4.7实例化 Arrow

class 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 [2013-05-07T00:00:00+00:00]>
>>> arrow.Arrow(2013, 5, 7)
<Arrow [2013-05-07T00:00:00+00:00]>

5.属性

创建 Arrow 实例后,可以直接获取其属性值。

>>> d = arrow.now()
>>> d
<Arrow [2022-05-17T08:05:07.207083+08:00]>
>>> d.year
2022
>>> d.month
5
>>> d.day
17
>>> d.hour
8
>>> d.minute
5
>>> d.second
7
>>> d.microsecond
207083

5.1获取 datetime

>>> d = arrow.now()
>>> d
<Arrow [2022-05-17T08:08:29.375589+08:00]>
>>> d.datetime
datetime.datetime(2022, 5, 17, 8, 8, 29, 375589, tzinfo=tzlocal())

5.2获取简单 datetime

>>> d = arrow.utcnow()
>>> d
<Arrow [2022-05-17T00:11:05.131193+00:00]>
>>> 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
<Arrow [2022-05-17T08:18:30.018946+08:00]>
>>> d.date()
datetime.date(2022, 5, 17)

5.5获取 time

>>> d = arrow.now()
>>> d
<Arrow [2022-05-17T08:19:11.103674+08:00]>
>>> d.time()
datetime.time(8, 19, 11, 103674)

6.更新

6.1 replace

通过更改属性得到新的时间:

>>> d = arrow.now()
>>> d
<Arrow [2022-05-17T09:27:37.050687+08:00]>
>>> d.replace(hour=8, minute=30, second=0)
<Arrow [2022-05-17T08:30:00.050687+08:00]>
>>> d.replace(tzinfo='US/Pacific')
<Arrow [2022-05-17T09:27:37.050687-07:00]>

6.2 shift

格式:

shift(**kwargs)

使用复数属性名称相对移动其当前值:

>>> d = arrow.now()
>>> d
<Arrow [2022-05-17T09:34:13.612358+08:00]>
>>> d.shift(years=9, days=10)
<Arrow [2031-05-27T09:34:13.612358+08:00]>
>>> d.shift(years=-9, days=-10)
<Arrow [2013-05-07T09:34:13.612358+08:00]>

使用 weekday 参数相对移动当前值:

>>> d.shift(weekday=5)
<Arrow [2022-05-21T09:34:13.612358+08:00]>

使用 weeks 参数相对移动当前值:

>>> d.shift(weeks=3)
<Arrow [2022-06-07T09:34:13.612358+08:00]>

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
<Arrow [2022-05-17T02:19:01.870480+00:00]>
>>> utc.to('US/Pacific')
<Arrow [2022-05-16T19:19:01.870480-07:00]>
>>> from dateutil import tz
>>> utc.to(tz.gettz('US/Pacific'))
<Arrow [2022-05-16T19:19:01.870480-07:00]>
>>> utc.to('Asia/Shanghai')
<Arrow [2022-05-17T10:19:01.870480+08:00]>
>>>

也可以使用短格式:

>>> utc.to('local')
<Arrow [2022-05-17T10:19:01.870480+08:00]>
>>> utc.to('local').to('utc')
<Arrow [2022-05-17T02:19:01.870480+00:00]>
>>>

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
<Arrow [2022-05-17T10:45:17.665357+08:00]>
>>> d.dehumanize("2 days ago")
<Arrow [2022-05-15T10:45:17.665357+08:00]>
>>> d.dehumanize("in a month")
<Arrow [2022-06-17T10:45:17.665357+08:00]>

支持语言环境:

>>> d = arrow.now()
>>> d
<Arrow [2022-05-17T10:47:26.858750+08:00]>
>>> d.dehumanize('1年前',locale='zh')
<Arrow [2021-05-17T10:47:26.858750+08:00]>

11.范围和跨度

获取任何单位的时间跨度:

>>> arrow.now().span('hour')
(<Arrow [2022-05-17T10:00:00+08:00]>, <Arrow [2022-05-17T10:59:59.999999+08:00]>)
>>> arrow.now().span('day')
(<Arrow [2022-05-17T00:00:00+08:00]>, <Arrow [2022-05-17T23:59:59.999999+08:00]>)

floor 与 ceil :

>>> arrow.now().floor('hour')
<Arrow [2022-05-17T10:00:00+08:00]>
>>> arrow.now().ceil('hour')
<Arrow [2022-05-17T10:59:59.999999+08:00]>

获取时间范围:

>>> 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)
...
(<Arrow [2013-04-05T14:00:00+00:00]>, <Arrow [2013-04-05T14:59:59.999999+00:00]>)
(<Arrow [2013-04-05T15:00:00+00:00]>, <Arrow [2013-04-05T15:59:59.999999+00:00]>)
(<Arrow [2013-04-05T16:00:00+00:00]>, <Arrow [2013-04-05T16:59:59.999999+00:00]>)
(<Arrow [2013-04-05T17:00:00+00:00]>, <Arrow [2013-04-05T17:59:59.999999+00:00]>)
(<Arrow [2013-04-05T18:00:00+00:00]>, <Arrow [2013-04-05T18:59:59.999999+00:00]>)
(<Arrow [2013-04-05T19:00:00+00:00]>, <Arrow [2013-04-05T19:59:59.999999+00:00]>)
(<Arrow [2013-04-05T20:00:00+00:00]>, <Arrow [2013-04-05T20:59:59.999999+00:00]>)
(<Arrow [2013-04-05T21:00:00+00:00]>, <Arrow [2013-04-05T21:59:59.999999+00:00]>)
(<Arrow [2013-04-05T22:00:00+00:00]>, <Arrow [2013-04-05T22:59:59.999999+00:00]>)
......

或者只是在一段时间内迭代:

>>> 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))
...
<Arrow [2013-04-05T14:30:00+00:00]>
<Arrow [2013-04-05T15:30:00+00:00]>
<Arrow [2013-04-05T16:30:00+00:00]>
<Arrow [2013-04-05T17:30:00+00:00]>
<Arrow [2013-04-05T18:30:00+00:00]>
<Arrow [2013-04-05T19:30:00+00:00]>
<Arrow [2013-04-05T20:30:00+00:00]>
<Arrow [2013-04-05T21:30:00+00:00]>
<Arrow [2013-04-05T22:30:00+00:00]>
......

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



Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表
最新留言