码迷,mamicode.com
首页 > 编程语言 > 详细

arrow python处理日期时间

时间:2020-01-03 12:07:45      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:ros   http   lan   替换   第三方库   native   cno   href   nbsp   

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

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

 pip install arrow

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

>>> arrow.utcnow()
<Arrow [2013-05-07T04:20:39.369271+00:00]>

>>> arrow.now()
<Arrow [2013-05-06T21:20:40.841085-07:00]>

>>> arrow.now(US/Pacific)
<Arrow [2013-05-06T21:20:44.761511-07:00]>

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

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

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

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

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

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

>>> arrow.get(datetime(2013, 5, 5), US/Pacific)
<Arrow [2013-05-05T00:00:00-07:00]>

>>> from dateutil import tz
>>> arrow.get(datetime(2013, 5, 5), tz.gettz(US/Pacific))
<Arrow [2013-05-05T00:00:00-07:00]>

>>> arrow.get(datetime.now(tz.gettz(US/Pacific)))
<Arrow [2013-05-06T21:24:49.552236-07:00]>

直接创建arrow对象

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

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

>>> arrow.get(‘2013-09-30T15:34:00.000-07:00‘)
<Arrow [2013-09-30T15:34:00-07:00]>
>>> 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对象属性    datetime,timestamp,native,tzinfo

>>> a = arrow.utcnow()
>>> a.datetime
datetime.datetime(2013, 5, 7, 4, 38, 15, 447644, tzinfo=tzutc())

>>> a.timestamp
1367901495

>>> a.naive
datetime.datetime(2013, 5, 7, 4, 38, 15, 447644)

>>> a.tzinfo
tzutc()

>>> a.year
2013

>>> a.date()
datetime.date(2013, 5, 7)

>>> a.time()
datetime.time(4, 38, 15, 447644)

Replace & Shift

替换和推移时间

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

 

>>> arw = arrow.utcnow()
>>> arw
<Arrow [2013-05-12T03:29:35.334214+00:00]>

>>> arw.replace(hour=4, minute=40)
<Arrow [2013-05-12T04:40:35.334214+00:00]>

>>> arw.shift(weeks=+3)
<Arrow [2013-06-02T03:29:35.334214+00:00]>

>>> arw.replace(tzinfo=‘US/Pacific‘)
<Arrow [2013-05-12T03:29:35.334214-07:00]>

格式化输出 和 类型转换

>>> arrow.utcnow().format(YYYY-MM-DD HH:mm:ss ZZ)
2013-05-07 05:23:16 -00:00
>>> utc = arrow.utcnow()
>>> utc
<Arrow [2013-05-07T05:24:11.823627+00:00]>

>>> utc.to(US/Pacific)
<Arrow [2013-05-06T22:24:11.823627-07:00]>

>>> utc.to(tz.gettz(US/Pacific))
<Arrow [2013-05-06T22:24:11.823627-07:00]>
>>> utc.to(local)
<Arrow [2013-05-06T22:24:11.823627-07:00]>

>>> utc.to(local).to(utc)
<Arrow [2013-05-07T05:24:11.823627+00:00]>

人性化输出    a.humanize()

>>> past = arrow.utcnow().shift(hours=-1)
>>> past.humanize()
an hour ago

>>> present = arrow.utcnow()
>>> future = present.shift(hours=2)
>>> future.humanize(present)
‘in 2 hours

>>> present = arrow.utcnow()
>>> future = present.shift(hours=2)
>>> future.humanize(present)
‘in 2 hours>>> future.humanize(present, only_distance=True)
‘2 hours

>>> present = arrow.utcnow()
>>> 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)
>>> future.humanize(a, locale=‘ru‘)
‘через 2 час(а,ов)

参考:github

arrow python处理日期时间

标签:ros   http   lan   替换   第三方库   native   cno   href   nbsp   

原文地址:https://www.cnblogs.com/clbao/p/12144113.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!