Arrow让Python时间处理更美好

Arrow: Better dates & times for Python

https://arrow.readthedocs.io/en/latest/

Arrow是一个用于时间处理的python库。它能够一键转化datestimestimestamps等多种时间格式,而不需要大量import各种时间模块和格式转化函数。十分便捷和人性化,能够极大程度简化你的代码。

原生时间操作需要对time`datetime\date`等多种格式进行转化再操作。

使用Arrow仅需两步,第一步接受各种类型时间类型(datetime,date,timestamps)转化为Arrow类型,第二步转化成自己需要的格式或进行操作。

import arrow

#获取当前时间的Arrow格式
i=arrow.now()
i
<Arrow [2019-11-21T17:04:57.018170+08:00]>

str(i)
'2019-11-21T17:04:57.018170+08:00'

转化时间戳
i.timestamp
1574327097

年 月 日 时 分 秒
i.format('YYYY-MM-DD HH:mm:ss')
'2019-11-21 17:04:57'

年 月 日
i.format('YYYY-MM-DD')
'2019-11-21'

时 分 秒
i.format('HH:mm:ss')
'17:04:57'

星期
i.format('MMM DD dddd ')
'Nov 21 Thursday '

66天后日期
i.shift(days=66).format('YYYY-MM-DD')
'2020-01-26'


修改日期,梦回2008
i.replace(year=2008, month=8, day=8).format('YYYY-MM-DD')
'2008-08-08'


Creation

获取当前时间

arrow.now()
#<Arrow [2019-11-21T17:23:21.463980+08:00]>

arrow.now('US/Pacific')
# <Arrow [2019-11-21T01:23:50.697479-08:00]>

arrow.utcnow()
# <Arrow [2019-11-21T09:24:57.171157+00:00]>

get 进行转化

from datetime import datetime
arrow.get(datetime(2019, 11, 21))
#<Arrow [2019-11-21T00:00:00+00:00]>

arrow.get(datetime(2019, 11, 21),'US/Pacific')

解析字符串格式:

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

Arrow objects can be instantiated directly too, with the same arguments as a datetime:
当天数为一位数字时 ,前面不可以+0

>>> arrow.get(2013, 5, 5)
<Arrow [2013-05-05T00:00:00+00:00]>

>>> arrow.Arrow(2013, 5, 5)
<Arrow [2013-05-05T00:00:00+00:00]>
arrow.get(2019,11,21).format("YYYY-MM-DD")
#'2019-11-21'

arrow.get("20191121").format("YYYY-MM-DD")
#'2019-11-21'

Properties

获取日期时间或时间戳表示

In [6]: a = arrow.utcnow()

In [7]: a.datetime
Out[7]: datetime.datetime(2019, 11, 21, 9, 41, 32, 402368, tzinfo=tzutc())

In [8]: a.timestamp
Out[8]: 1574329292

# Get a naive datetime, 
In [9]: a.naive
Out[9]: datetime.datetime(2019, 11, 21, 9, 41, 32, 402368)

# Get any datetime value:
In [10]: a.year
Out[10]: 2019

In [11]: a.day
Out[11]: 21


# Call datetime functions that return properties:
In [12]: a.date()
Out[12]: datetime.date(2019, 11, 21)

In [13]: a.time()
Out[13]: datetime.time(9, 41, 32, 402368)

Replace & Shift

Get a new Arrow object, with altered attributes, just as you would with a datetime:

In [14]: arw = arrow.utcnow()

In [15]: arw
Out[15]: <Arrow [2019-11-21T11:07:16.675758+00:00]>

In [16]: arw.replace(hour=4, minute=40)
Out[16]: <Arrow [2019-11-21T04:40:16.675758+00:00]>

Or, get one with attributes shifted forward or backward:

In [17]: arw.shift(weeks=+3)
Out[17]: <Arrow [2019-12-12T11:07:16.675758+00:00]>

In [18]: arw.shift(days=-14)
Out[18]: <Arrow [2019-11-07T11:07:16.675758+00:00]>

In [19]: arw.shift(days =14)
Out[19]: <Arrow [2019-12-05T11:07:16.675758+00:00]>

In [20]: arw.shift(years=1)
Out[20]: <Arrow [2020-11-21T11:07:16.675758+00:00]>

Format

In [21]: arrow.utcnow().format('YYYY-MM-DD HH:mm:ss ZZ')
Out[21]: '2019-11-21 11:11:14 +00:00'

Ranges & Spans

Get the time span of any unit:

In [22]: arrow.utcnow().span('hour')
Out[22]:
(<Arrow [2019-11-21T11:00:00+00:00]>,
 <Arrow [2019-11-21T11:59:59.999999+00:00]>)

Or just get the floor and ceiling:

In [23]: arrow.utcnow().floor('hour')
Out[23]: <Arrow [2019-11-21T11:00:00+00:00]>

In [24]: arrow.utcnow().ceil('hour')
Out[24]: <Arrow [2019-11-21T11:59:59.999999+00:00]>

You can also get a range of time spans:

arrow.Arrow.span_rang()返回一个元组

start = datetime(2013, 5, 5,)
end = datetime(2013,6, 5,)
for r in arrow.Arrow.span_range('day', start, end):
    print(r)
    print(type(r))
    

# (<Arrow [2013-05-05T00:00:00+00:00]>, <Arrow [2013-05-05T23:59:59.999999+00:00]>)
# <class 'tuple'>

Or just iterate over a range of time:

start = datetime(2013, 5, 5, 12, 30)
end = datetime(2013, 5, 5, 17, 15)
for r in arrow.Arrow.range('hour', start, end):
    print( repr(r))

<Arrow [2013-05-05T12:30:00+00:00]>
<Arrow [2013-05-05T13:30:00+00:00]>
<Arrow [2013-05-05T14:30:00+00:00]>
<Arrow [2013-05-05T15:30:00+00:00]>
<Arrow [2013-05-05T16:30:00+00:00]>


'或者'
start =arrow.get (2013, 5, 5, 12, 30)
end = arrow.get(2013, 5, 5, 17, 15)
for r in arrow.Arrow.range('hour', start, end):
    print( repr(r))


'对获取范围内的日期进行格式化'
'range'的到的范围,为闭区间
start =arrow.get (2013, 5, 5, 12, 30)
end = arrow.get(2013, 5, 10, 17, 15)
for r in arrow.Arrow.range('day', start, end):
    print(r.format("YYYY-MM-DD"))

2013-05-05
2013-05-06
2013-05-07
2013-05-08
2013-05-09
2013-05-10

Supported Tokens

在这里插入图片描述
支持正则表达式

>>> fmt = r"ddd[\s+]MMM[\s+]DD[\s+]HH:mm:ss[\s+]YYYY"
>>> arrow.get("Mon Sep 08 16:41:45 2014", fmt)
<Arrow [2014-09-08T16:41:45+00:00]>

>>> arrow.get("Mon \tSep 08   16:41:45     2014", fmt)
<Arrow [2014-09-08T16:41:45+00:00]>

>>> arrow.get("Mon Sep 08   16:41:45   2014", fmt)
<Arrow [2014-09-08T16:41:45+00:00]>

Method

arrow.api

arrow.api.get(*args, **kwargs)
arrow.api.utcnow()

>>> import arrow
>>> arrow.utcnow()
<Arrow [2013-05-08T05:19:07.018993+00:00]>
arrow.api.now(tz=None)
>>> import arrow
>>> arrow.now()
<Arrow [2013-05-07T22:19:11.363410-07:00]>

>>> arrow.now('US/Pacific')
<Arrow [2013-05-07T22:19:15.251821-07:00]>

>>> arrow.now('+02:00')
<Arrow [2013-05-08T07:19:25.618646+02:00]>

>>> arrow.now('local')
<Arrow [2013-05-07T22:19:39.130059-07:00]>

arrow.arrow

Provides the Arrow class, an enhanced datetime replacement.

https://arrow.readthedocs.io/en/latest/#api-guide

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值