0
点赞
收藏
分享

微信扫一扫

python教程:arrow库的用法


Python针对日期时间的处理提供了大量的package,类和方法,但在可用性上来看非常繁琐和麻烦

第三方库Arrow提供了一个合理的、人性化的方法来创建、操作、格式转换的日期,时间,和时间戳,帮助我们使用较少的导入和更少的代码来处理日期和时间。

$ pip install arrow

获取当前时间arrow.utcnow(), arrow.now()

>>> import arrow
>>> utc = arrow.utcnow() # 获取世界标准时间
>>> utc
<Arrow [2018-06-07T09:37:28.989983+00:00]>
>>> utc = arrow.now() # 获取本地时间
>>> utc
<Arrow [2018-06-07T17:40:19.019529+08:00]>
>>> arrow.now('US/Pacific') # 获取指定时区的时间
<Arrow [2018-06-07T02:41:54.815029-07:00]>

将时间戳转化为arrow对象arrow.get(timestamp)

>>> arrow.get(1519534533) 
<Arrow [2018-02-25T04:55:33+00:00]>

>>> arrow.get('1519534533')
<Arrow [2018-02-25T04:55:33+00:00]>

>>> arrow.get(1519534533.153443)
<Arrow [2018-02-25T04:55:33.153443+00:00]>

>>> arrow.get('1519534533.153443')
<Arrow [2018-02-25T04:55:33.153443+00:00]>

时间戳可以是int,float或者可以转化为float的字符串

将字符串转换为arrow对象arrow.get(string[,format_string])

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

遵循ISO-8601的字符串不需要格式字符串参数即可转换

>>> arrow.get('2018-02-24T13:00:00.000-07:00')
<Arrow [2018-02-24T13:00:00-07:00]>

可以从字符串中通过格式参数搜索时间

>>> arrow.get('June was born in May 1980', 'MMMM YYYY')
<Arrow [1980-05-01T00:00:00+00:00]>

直接创建arrow对象

>>> arrow.get(2018, 2, 24)
<Arrow [2018-02-24T00:00:00+00:00]>

>>> arrow.Arrow(2018, 2, 24)
<Arrow [2018-02-24T00:00:00+00:00]>

arrow对象属性datetime,timestamp,native,tzinfo

>>> a = arrow.utcnow()
>>> a.datetime
datetime.datetime(2018, 2, 24, 21, 15, 50, 841056, tzinfo=tzlocal())

>>> a.timestamp
1519478150

>>> a.naive
datetime.datetime(2018, 2, 24, 21, 58, 4, 309575)

>>> a.tzinfo
tzlocal()

按名称或tzinfo转换为时区

>>> arw = arrow.utcnow()
>>> arw
<Arrow [2018-06-07T11:16:51.695083+00:00]>
>>> arw.to('US/Pacific')
<Arrow [2018-06-07T04:16:51.695083-07:00]>

获取datetime对象的值

>>> a = arrow.now()
>>> a
<Arrow [2018-06-07T17:44:43.519166+08:00]>
>>> a.year # 当前年
2018
>>> a.month # 当前月份
6
>>> a.day # 当前天
7
>>> a.hour # 当前第几个小时
17
>>> a.minute # 当前多少分钟
44
>>> a.second # 当前多少秒
43
>>> a.timestamp # 获取时间戳
1528364683
>>> a.float_timestamp # 浮点数时间戳
1528364683.519166

时间推移a.shift(**kwargs)

shift方法获取某个时间之前或之后的时间,关键字参数为years,months,weeks,days,hours,seconds,microseconds

>>> a.shift(weeks=+3)    #三周后
<Arrow [2018-03-17T21:58:04.309575+08:00]>

>>> a.shift(days=-1) #一天前
<Arrow [2018-02-23T21:58:04.309575+08:00]

>>> a.shift(weekday=6) #距离最近a的星期日,weekday从0到6
<Arrow [2018-02-25T21:58:04.309575+08:00]>

时间替换a.replace(**kwargs)

返回一个被替换后的arrow对象,原对象不变

>>> a
<Arrow [2018-02-24T21:58:04.309575+08:00]>
>>> a.replace(hour=9)
<Arrow [2018-02-24T09:58:04.309575+08:00]>

格式化输出 a.format([format_string])

>>> a = arrow.now()
>>> a
<Arrow [2018-06-07T17:59:36.917894+08:00]>
>>> a.format()
'2018-06-07 17:59:36+08:00'
>>> a.format('YYYY-MM-DD HH:mm:ss ZZ')
'2018-06-07 17:59:36 +08:00'
>>> a.ctime() # 返回日期和时间的ctime格式化表示。
'Thu Jun 7 17:59:36 2018'
>>> a.weekday() # 以整数形式返回星期几(0-6)
3
>>> a.isoweekday() # 以整数形式返回一周中的ISO日(1-7)
4
>>> a.isocalendar() # 返回3元组(ISO年,ISO周数,ISO工作日)
(2018, 23, 4)
>>> a.toordinal() # 返回日期的格雷戈里序数
736852

人性化输出 a.humanize()

'''
学习中遇到问题没人解答?小编创建了一个Python学习交流QQ群:857662006
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
>>> present = arrow.utcnow()
>>> past = present.shift(hours=-1)
>>> past.humanize() #相对于当前时间
'an hour age'
>>> future = present.shift(hours=2)
>>> future.humanize(present) #相对于参数时间
'in 2 hours'
>>> past.humanize(present, locale='zh') #locale参数可以指定地区语言
'1天前'

时间范围和区间a.span(string), a.floor(), a.ceil() arrow.Arrow.span_range(),arrow.Arrow.range()

>>> a
<Arrow [2018-02-24T21:58:04.309575+08:00]>
>>> a.span('hour') #a所在的时间区间
(<Arrow [2018-02-24T21:00:00+08:00]>, <Arrow [2018-02-24T21:59:59.999999+08:00]>)
>>> a.floor('hour') #a所在区间的开始
<Arrow [2018-02-24T21:00:00+08:00]>
>>> a.ceil('hour') #a所在区间的结尾
<Arrow [2018-02-24T21:59:59.999999+08:00]

>>> start = datetime.datetime(2018, 2, 24, 12, 30)
>>> end = datetime.datetime(2018, 2, 24, 15, 20)
>>> for r in arrow.Arrow.span_range('hour',start,end): #获取start,end之间的时间区间
... print(r)
...
(<Arrow [2018-02-24T12:00:00+00:00]>, <Arrow [2018-02-24T12:59:59.999999+00:00]>)
(<Arrow [2018-02-24T13:00:00+00:00]>, <Arrow [2018-02-24T13:59:59.999999+00:00]>)
(<Arrow [2018-02-24T14:00:00+00:00]>, <Arrow [2018-02-24T14:59:59.999999+00:00]>)
(<Arrow [2018-02-24T15:00:00+00:00]>, <Arrow [2018-02-24T15:59:59.999999+00:00]>)
>>> for r in arrow.Arrow.range('hour',start,end): #获取间隔单位时间的时间
... print(r)
...
2018-02-24T12:30:00+00:00
2018-02-24T13:30:00+00:00
2018-02-24T14:30:00+00:00



举报

相关推荐

0 条评论