python日期时间变得简单

pendulum的Python项目详细描述


https://img.shields.io/pypi/v/pendulum.svghttps://img.shields.io/pypi/l/pendulum.svghttps://img.shields.io/codecov/c/github/sdispater/pendulum/master.svgPendulum Build status

python日期时间变得简单。

支持python2.73.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函数的对象类型,例如sqlite3PyMySQL

它还删除了原始日期时间的概念:每个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 数据作为参考,以查看需要哪些数据。

您还应该为创建或修改的区域设置添加测试。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
如何上传文件。HTML/Javascript格式的mp3文件并发送到Java后端   eclipse问题:使用Selenium WebDriver(java)从下拉列表中查找元素   java如何通过通知恢复应用程序   java Repast聚合数据集,但在Repast Simphony中分别针对每个实例   java为什么收到Http/1.1400错误请求?   java如何简单地检查请求体是否为空或请求体是否有空字段?   java JTable:如何避免重复行和聚合项目数量   java如何部署和访问Dropwizard应用程序   java找不到基本类weblogic。部署者   JavaSpringWebMVC互动程序从相同的定义扩展而来   javascript Xsl transformToDocument不适用于chrome浏览器,但适用于firefox   java创建存储泛型类型对象的ArrayList数组   如何在Java中禁用抛出异常或fillInStackTrace()   使用ConstraintValidator使用两种类型进行java自定义bean验证   java组织。百里香。例外情况。TemplateProcessingException:连接href