Skip to main content

Python datetimes made easy.

Project description

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

Python datetimes made easy.

Supports Python 2.7+, 3.4+ and PyPy.

>>> 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'

Why Pendulum?

Native datetime instances are enough for basic cases but when you face more complex use-cases they often show limitations and are not so intuitive to work with. Pendulum provides a cleaner and more easy to use API while still relying on the standard library. So it’s still datetime but better.

Unlike other datetime libraries for Python, Pendulum is a drop-in replacement for the standard datetime class (it inherits from it), so, basically, you can replace all your datetime instances by Pendulum instances in you code (exceptions exist for libraries that check the type of the objects by using the type function like sqlite3 or PyMySQL for instance).

It also removes the notion of naive datetimes: each Pendulum instance is timezone-aware and by default in UTC for ease of use.

Pendulum also improves the standard timedelta class by providing more intuitive methods and properties.

Why not Arrow?

Arrow is the most popular datetime library for Python right now, however its behavior and API can be erratic and unpredictable. The get() method can receive pretty much anything and it will try its best to return something while silently failing to handle some cases:

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 DST
just_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-06

just_before = pendulum.create(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

Those are a few examples showing that Arrow cannot always be trusted to have a consistent behavior with the data you are passing to it.

Limitations

Even though the Pendulum class is a subclass of datetime there are some rare cases where it can’t replace the native class directly. Here is a list (non-exhaustive) of the reported cases with a possible solution, if any:

  • sqlite3 will use the type() function to determine the type of the object by default. To work around it you can register a new adapter:

from pendulum import Pendulum
from sqlite3 import register_adapter

register_adapter(Pendulum, lambda val: val.isoformat(' '))
  • mysqlclient (former MySQLdb) and PyMySQL will use the type() function to determine the type of the object by default. To work around it you can register a new adapter:

import MySQLdb.converters
import pymysql.converters

from pendulum import Pendulum

MySQLdb.converters.conversions[Pendulum] = MySQLdb.converters.DateTime2literal
pymysql.converters.conversions[Pendulum] = pymysql.converters.escape_datetime
  • django will use the isoformat() method to store datetimes in the database. However since pendulum is always timezone aware the offset information will always be returned by isoformat() raising an error, at least for MySQL databases. To work around it you can either create your own DateTimeField or use the previous workaround for MySQLdb:

from django.db.models import DateTimeField as BaseDateTimeField
from pendulum import Pendulum


class DateTimeField(BaseDateTimeField):

    def value_to_string(self, obj):
        val = self.value_from_object(obj)

        if isinstance(value, Pendulum):
            return value.to_datetime_string()

        return '' if val is None else val.isoformat()

Resources

Contributing

Contributions are welcome, especially with localization. Check the languages already supported, and if you want to add a new one, take the en file as a starting point and add tests accordingly.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pendulum-1.4.2.tar.gz (75.1 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

pendulum-1.4.2-cp36-cp36m-manylinux1_x86_64.whl (127.5 kB view details)

Uploaded CPython 3.6m

pendulum-1.4.2-cp36-cp36m-manylinux1_i686.whl (127.4 kB view details)

Uploaded CPython 3.6m

pendulum-1.4.2-cp36-cp36m-macosx_10_13_x86_64.whl (113.5 kB view details)

Uploaded CPython 3.6mmacOS 10.13+ x86-64

pendulum-1.4.2-cp35-cp35m-manylinux1_x86_64.whl (127.5 kB view details)

Uploaded CPython 3.5m

pendulum-1.4.2-cp35-cp35m-manylinux1_i686.whl (127.3 kB view details)

Uploaded CPython 3.5m

pendulum-1.4.2-cp27-cp27m-manylinux1_x86_64.whl (127.2 kB view details)

Uploaded CPython 2.7m

pendulum-1.4.2-cp27-cp27m-manylinux1_i686.whl (127.1 kB view details)

Uploaded CPython 2.7m

File details

Details for the file pendulum-1.4.2.tar.gz.

File metadata

  • Download URL: pendulum-1.4.2.tar.gz
  • Upload date:
  • Size: 75.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pendulum-1.4.2.tar.gz
Algorithm Hash digest
SHA256 39a255776528afe11ea0d57814f9bf3729c1e0b99063af2e5c6cfd750c3e1f7f
MD5 ca17102ae2c14f73075e8b881fb4cbcd
BLAKE2b-256 df34219d94459b4dc1c33e0cd93af5fdabea19f9df3573fbaa4ae00eba0ad5bb

See more details on using hashes here.

File details

Details for the file pendulum-1.4.2-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for pendulum-1.4.2-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 ff0c5fa3af4a471a218408c448b804ac6bccb105127727474f4e83c0e4072e97
MD5 df236d929186aa2efab8018a53ee061f
BLAKE2b-256 406d17acccf0bef59b45bdafedf0d907f6c3b04b7352eb2f80788592edf62690

See more details on using hashes here.

File details

Details for the file pendulum-1.4.2-cp36-cp36m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for pendulum-1.4.2-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 bcca072f82e84b419efec1320cd3ee5c230d263f3a601b146651ed4db77d89f0
MD5 34102f4b8ed14475fc9e35b8cac7a1bf
BLAKE2b-256 9dcdb746dd8d57842c0bd38aa8bbbd8c92c9e24fee7b9f4c1d746a63b913f918

See more details on using hashes here.

File details

Details for the file pendulum-1.4.2-cp36-cp36m-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for pendulum-1.4.2-cp36-cp36m-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 8199206c479b13947dcac63c025575d035331bb3819d1783dc1d568a11962906
MD5 a00c1d81899791f0e6a7f8585492afc9
BLAKE2b-256 2f70dd143c0e9077d6087fca0f7c71218bb1ea58f4e7ceb8562707a528cd4ef5

See more details on using hashes here.

File details

Details for the file pendulum-1.4.2-cp35-cp35m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for pendulum-1.4.2-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 8798aeca58b3dd7ffdc5a4993c9eaafedc4048165429e8f499ddd62c73bf3964
MD5 989ce88d2fc32ec456fd27b4344833c2
BLAKE2b-256 e0b9c22bcff38e52f9c55d6fd416a66a51bb945a7ea10245e479157c60adb3d4

See more details on using hashes here.

File details

Details for the file pendulum-1.4.2-cp35-cp35m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for pendulum-1.4.2-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 0c14388546db6605a860b8b7112cb69d0b11c9ce5e072210504544e0d4575799
MD5 20a4566d7cebd37231a97d62fb336c21
BLAKE2b-256 0e450162e61268a9c996f396b68f2f15a6f901373a3e6e77a3e3f102fc5aa8b7

See more details on using hashes here.

File details

Details for the file pendulum-1.4.2-cp27-cp27m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for pendulum-1.4.2-cp27-cp27m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 3c85e8cbc91f45e1cc916cc9180b34153cd6aaaaacfb51a48b3156318314fa82
MD5 f829c119914cee315d77ca639b016685
BLAKE2b-256 c2ffde762a2994e51cd1333b705aa55a3006068b393aa56e9fb6b9deb1eea86d

See more details on using hashes here.

File details

Details for the file pendulum-1.4.2-cp27-cp27m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for pendulum-1.4.2-cp27-cp27m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 881efe37328de0785c0731d462e1485a45712f2cd5cb55907d6c15458460ebeb
MD5 00a4e6ea8d20c306888571607207b14d
BLAKE2b-256 925b8fa2dd0be580a318a296ad0e87cb4234ed70c3c60394f9f8f005025af8a3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page