zl程序教程

您现在的位置是:首页 >  后端

当前栏目

python基础===pendulum '''Python datetimes made easy.'''

Python基础 &# 39 Easy .&# made
2023-09-14 09:12:07 时间

https://pypi.python.org/pypi/pendulum

 

Pendulum的一大优势是内嵌式取代Python的datetime类,可以轻易地将它整合进已有代码,并且只在需要的时候才进行调用。作者精心调校了时区,确保时区准确,还让每个实例都对时区敏感,默认使用UTC。还有就是提供了扩展的timedelta,使datetime计算更加容易。

与现有的其他库不同,它提供了一个可预测行为的API,这样就可以知道后面会发生什么。如果你正开发使用datetimes的复杂项目,这个库能让你更轻松!想要了解更多,请查阅文档。

>>> 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.hours
23
>>> 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'