![](/style/images/good.png)
2
![](/style/images/bad.png)
更好的Python时间日期库 Arrow
source link: http://wwj718.github.io/post/%E6%8A%80%E6%9C%AF/arrow-better-dates-and-times-for-python/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
更好的Python时间日期库 Arrow
2015-12-02
#问题 使用Python标准库来处理时区、日期转换、格式输出之类的问题,十分繁琐。标准库虽然提供了完备的功能,但常用方法分布得十分散乱。每次使用都得在文档里奔波
#Arrow Arrow提供了明智、友好的的方式来创建(操作/格式化/转换)时间(日期/时间戳) ,如此一来常用函数易于调用,而不是藏于深巷。只需要查阅一处文档即可
Arrow的灵感来自moment.js 和 requests
#安装 pip install arrow
#快速开始
import arrow
utc = arrow.utcnow()
utc #<Arrow [2015-12-02T01:55:16.190145+00:00]>
utc_Beijing = utc.replace(hours=+8) #北京时间 东八区
utc_Beijing #<Arrow [2015-12-02T09:55:16.190145+00:00]>
#local
local = utc.to('Asia/Shanghai')
local #<Arrow [2015-12-02T09:55:16.190145+08:00]>
local.timestamp #1449021316
local.format() #u'2015-12-02 09:55:16+08:00'
local.format('YYYY-MM-DD HH:mm:ss ZZ')#u'2015-12-02 09:55:16 +08:00'
local.humanize()#u'2 minutes ago'
print local.humanize(locale='zh_cn') #3分钟前
arrow.get('2013-05-11T21:23:58.970460+00:00')#<Arrow [2013-05-11T21:23:58.970460+00:00]>
文章作者 种瓜
上次更新 2015-12-02
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK