python日期时间变得简单
pendulum的Python项目详细描述
python日期时间变得简单。
支持python2.7和3.4+。
>>>importpendulum>>>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)>>>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.datetime(2013,3,31,2,30,tz='Europe/Paris')'2013-03-31T03:30:00+02:00'# 2:30 does not exist (Skipped time)# Proper handling of dst transitions>>>just_before=pendulum.datetime(2013,3,31,1,59,59,999999,tz='Europe/Paris')'2013-03-31T01:59:59.999999+01:00'>>>just_before.add(microseconds=1)'2013-03-31T03:00:00+02:00'
为什么是钟摆?
本机datetime实例对于基本情况已经足够了,但是当您面对更复杂的用例时 它们经常显示出局限性,而且使用起来并不那么直观。 Pendulum在仍然依赖标准库的情况下,提供了更干净、更易于使用的api。 所以仍然是datetime,但更好。
与python的其他datetime库不同,peopling是一个替代品。 对于标准的datetime类(它继承自该类),基本上,您可以替换所有的datetime 在代码中的实例实例(检查的库存在异常) 使用type函数的对象类型,例如sqlite3或PyMySQL。
它还删除了原始日期时间的概念:每个Pendulum实例都可以识别时区 默认情况下在UTC中使用,以便于使用。
钟摆还通过提供更直观的方法和属性改进了标准的timedelta类。
为什么不是箭?
箭头是目前python最流行的日期时间库,但是它的行为 而且api可能是不稳定和不可预测的。get()方法可以接收几乎任何内容 它会尽力返回一些内容,但在某些情况下却无法处理:
arrow.get('2016-1-17')# <Arrow [2016-01-01T00:00:00+00:00]>pendulum.parse('2016-1-17')# <Pendulum [2016-01-17T00:00:00+00:00]>arrow.get('20160413')# <Arrow [1970-08-22T08:06:53+00:00]>pendulum.parse('20160413')# <Pendulum [2016-04-13T00:00:00+00:00]>arrow.get('2016-W07-5')# <Arrow [2016-01-01T00:00:00+00:00]>pendulum.parse('2016-W07-5')# <Pendulum [2016-02-19T00:00:00+00:00]># Working with DSTjust_before=arrow.Arrow(2013,3,31,1,59,59,999999,'Europe/Paris')just_after=just_before.replace(microseconds=1)'2013-03-31T02:00:00+02:00'# Should be 2013-03-31T03:00:00+02:00(just_after.to('utc')-just_before.to('utc')).total_seconds()-3599.999999# Should be 1e-06just_before=pendulum.datetime(2013,3,31,1,59,59,999999,'Europe/Paris')just_after=just_before.add(microseconds=1)'2013-03-31T03:00:00+02:00'(just_after.in_timezone('utc')-just_before.in_timezone('utc')).total_seconds()1e-06
有几个例子表明,不能总是相信arrow具有一致的 对传递给它的数据的行为。
限制
即使DateTime类是datetime的子类,也有一些罕见的情况 它不能直接替换本机类。以下是报告病例的列表(非详尽) 可能的解决方案(如果有的话):
- sqlite3将默认使用type()函数来确定对象的类型。要解决这个问题,您可以注册一个新适配器:
frompendulumimportDateTimefromsqlite3importregister_adapterregister_adapter(DateTime,lambdaval:val.isoformat(' '))
- mysqlclient(以前的MySQLdb)和PyMySQL将默认使用type()函数来确定对象的类型。要解决这个问题,您可以注册一个新适配器:
importMySQLdb.convertersimportpymysql.convertersfrompendulumimportDateTimeMySQLdb.converters.conversions[DateTime]=MySQLdb.converters.DateTime2literalpymysql.converters.conversions[DateTime]=pymysql.converters.escape_datetime
- django将使用isoformat()方法在数据库中存储日期时间。但是,由于pendulum始终是时区感知的,因此isoformat()将始终返回偏移量信息,至少对于mysql数据库是这样。要解决这个问题,您可以创建自己的DateTimeField,也可以使用前面的MySQLdb:
fromdjango.db.modelsimportDateTimeFieldasBaseDateTimeFieldfrompendulumimportDateTimeclassDateTimeField(BaseDateTimeField):defvalue_to_string(self,obj):val=self.value_from_object(obj)ifisinstance(value,DateTime):returnvalue.to_datetime_string()return''ifvalisNoneelseval.isoformat()
贡献
我们欢迎您的贡献,特别是本地化方面的贡献。
开始
要使用钟摆代码库,您需要在本地克隆项目 并通过poetry安装所需的依赖项。
$ git clone git@github.com:sdispater/pendulum.git $ poetry install
本地化
如果您想帮助本地化,有两种不同的情况:语言环境已经存在。 或者不。如果不存在区域设置,则需要使用^ {TT28 }实用工具:
创建它。./clock locale:create <your-locale>
它将在pendulum/locales中生成一个以您的区域设置命名的目录,如下所示 结构:
<your-locale>/ - custom.py - locale.py
不能修改locale.py文件。它包含由 CLDR数据库。
custom.py文件是您要修改的文件。它包含所需的数据 由CLDR数据库未提供的摆锤。你可以拿en 数据作为参考,以查看需要哪些数据。
您还应该为创建或修改的区域设置添加测试。