惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

小众软件
小众软件
博客园 - Franky
罗磊的独立博客
G
Google Developers Blog
The GitHub Blog
The GitHub Blog
P
Proofpoint News Feed
Recent Announcements
Recent Announcements
V
V2EX
F
Fortinet All Blogs
阮一峰的网络日志
阮一峰的网络日志
Blog — PlanetScale
Blog — PlanetScale
月光博客
月光博客
U
Unit 42
GbyAI
GbyAI
A
About on SuperTechFans
WordPress大学
WordPress大学
Engineering at Meta
Engineering at Meta
雷峰网
雷峰网
Microsoft Azure Blog
Microsoft Azure Blog
Martin Fowler
Martin Fowler
D
DataBreaches.Net
The Cloudflare Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
MongoDB | Blog
MongoDB | Blog

夜行人

回家路上 第一期的直播演示项目 震动检测器 正能量 在线参观CodeLab Neverland 发布 CodeLab Adapter 3.3.1 DynamicTable 之 纸糊方向盘 CodeLab DynamicTable: 一个可实施的技术方案 CodeLab Insight 发布 Alpha 版 情人节 Home Assistant 周报 && IoT 周报 (02) Joplin: 关注隐私的 Evernote 开源替代软件 浏览器的未来与 Web 传感器 Home Assistant 周报 && IoT 周报 (01) 百宝箱(01) 论自由 介绍 WebThings Home Assistant 周报 && iot 周报 (00) 百宝箱(00) 毛姆读书心得 传世之作 周末徒步 CodeLab Adapter ❤️ Jupyter/Python 航班 躲雨 夏令营途中 [译]思想--作为一种技术 The future of coding 美国之行 三门问题的程序模拟
更好的Python时间日期库 Arrow
2015-12-02 · via 夜行人

#问题 使用Python标准库来处理时区、日期转换、格式输出之类的问题,十分繁琐。标准库虽然提供了完备的功能,但常用方法分布得十分散乱。每次使用都得在文档里奔波

#Arrow Arrow提供了明智、友好的的方式来创建(操作/格式化/转换)时间(日期/时间戳) ,如此一来常用函数易于调用,而不是藏于深巷。只需要查阅一处文档即可

Arrow的灵感来自moment.jsrequests

#安装 pip install arrow

#快速开始

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
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