1、arrow
arrow 是一个专门处理时间和日期的轻量级 python 库,它提供了一种合理、智能的方式来创建、操作、格式化、转换时间和日期,并提供了一个支持许多常见构建方案的智能模块 api 。简单来说,它可以帮你以更简便的操作和更少的代码来使用日期和时间。其设计灵感主要来源于 moment.js 和 requests 。
quick start
$ pip install arrow
>>> import arrow>>> utc = arrow.utcnow()>>> utc>>> utc = utc.replace(hours=-1)>>> utc>>> local = utc.to('us/pacific')>>> local>>> arrow.get('2013-05-11t21:23:58.970460+00:00')>>> local.timestamp1368303838>>> local.format()'2013-05-11 13:23:58 -07:00'>>> local.format('yyyy-mm-dd hh:mm:ss zz')'2013-05-11 13:23:58 -07:00'>>> local.humanize()'an hour ago'>>> local.humanize(locale='ko_kr')'1 '
2、delorean
delorean 提供了一个相比于 datetime 和 pytz 的更好的抽象,让你处理时间更容易。它有很多有用的处理时区的特性,标准化时区或者从一个时区改变到另外一个时区。
from datetime import datetimeimport pytzest = pytz.timezone('us/eastern')d = datetime.now(pytz.utc)d = est.normalize(d.astimezone(est))return d
from delorean import deloreand = delorean()d = d.shift('us/eastern')return d
3、pendulum
原生的 datetime 足够应付基本情况,但当面对更复杂的用例时,通常会有的捉襟见肘,不那么直观。 pendulum 在标准库的基础之上,提供了一个更简洁,更易于使用的 api ,旨在让 python datetime 更好用。
>>> import pendulum>>> now_in_paris = pendulum.now('europe/paris')>>> now_in_paris'2016-07-04t00:49:58.502116+02:00'# seamless timezone switching>>> now_in_paris.in_timezone('utc')'2016-07-03t22:49:58.502116+00:00'>>> tomorrow = pendulum.now().add(days=1)>>> last_week = pendulum.now().subtract(weeks=1)>>> if pendulum.now().is_weekend():... print('party!')'party!'>>> past = pendulum.now().subtract(minutes=2)>>> past.diff_for_humans()>>> '2 minutes ago'>>> delta = past - last_week>>> delta.hours23>>> delta.in_words(locale='en')'6 days 23 hours 58 minutes'# proper handling of datetime normalization>>> pendulum.create(2013, 3, 31, 2, 30, 0, 0, 'europe/paris')'2013-03-31t03:30:00+02:00' # 2:30 does not exist (skipped time)# proper handling of dst transitions>>> just_before = pendulum.create(2013, 3, 31, 1, 59, 59, 999999, 'europe/paris')'2013-03-31t01:59:59.999999+01:00'>>>just_before.add(microseconds=1)'2013-03-31t03:00:00+02:00'
4、dateutil
dateutil 是 datetime 标准库的一个扩展库,几乎支持以所有字符串格式对日期进行通用解析,日期计算灵活,内部数据更新及时。
>>> from dateutil.relativedelta import *>>> from dateutil.easter import *>>> from dateutil.rrule import *>>> from dateutil.parser import *>>> from datetime import *>>> now = parse(sat oct 11 17:13:46 utc 2003)>>> today = now.date()>>> year = rrule(yearly,dtstart=now,bymonth=8,bymonthday=13,byweekday=fr)[0].year>>> rdelta = relativedelta(easter(year), today)>>> print(today is: %s % today)today is: 2003-10-11>>> print(year with next aug 13th on a friday is: %s % year)year with next aug 13th on a friday is: 2004>>> print(how far is the easter of that year: %s % rdelta)how far is the easter of that year: relativedelta(months=+6)>>> print(and the easter of that year is: %s % (today+rdelta))and the easter of that year is: 2004-04-11
5、moment
用于处理日期/时间的 python 库,设计灵感同样是来源于 moment.js 和 requests ,设计理念源自 times python 模块。
usage
import momentfrom datetime import datetime# create a moment from a stringmoment.date(12-18-2012)# create a moment with a specified strftime formatmoment.date(12-18-2012, %m-%d-%y)# moment uses the awesome dateparser library behind the scenesmoment.date(2012-12-18)# create a moment with words in itmoment.date(december 18, 2012)# create a moment that would normally be pretty hard to domoment.date(2 weeks ago)# create a future moment that would otherwise be really difficultmoment.date(2 weeks from now)# create a moment from the current datetimemoment.now()# the moment can also be utc-basedmoment.utcnow()# create a moment with the utc time zonemoment.utc(2012-12-18)# create a moment from a unix timestampmoment.unix(1355875153626)# create a moment from a unix utc timestampmoment.unix(1355875153626, utc=true)# return a datetime instancemoment.date(2012, 12, 18).date# we can do the same thing with the utc methodmoment.utc(2012, 12, 18).date# create and format a moment using moment.js semanticsmoment.now().format(yyyy-m-d)# create and format a moment with strftime semanticsmoment.date(2012, 12, 18).strftime(%y-%m-%d)# update your moment's time zonemoment.date(datetime(2012, 12, 18)).locale(us/central).date# alter the moment's utc time zone to a different time zonemoment.utcnow().timezone(us/eastern).date# set and update your moment's time zone. for instance, i'm on the# west coast, but want nyc's current time.moment.now().locale(us/pacific).timezone(us/eastern)# in order to manipulate time zones, a locale must always be set or# you must be using utc.moment.utcnow().timezone(us/eastern).date# you can also clone a moment, so the original stays unalterednow = moment.utcnow().timezone(us/pacific)future = now.clone().add(weeks=2)
6、when.py
提供对用户非常友好的特性来帮助执行常见的日期和时间操作。