Skip to main content

Python datetimes made easy.

Project description

Pendulum Build status

Python datetimes made easy.

Supports Python 2.7+, 3.2+ 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 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 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.0.0.tar.gz (64.9 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.0.0-cp36-cp36m-manylinux1_x86_64.whl (108.8 kB view details)

Uploaded CPython 3.6m

pendulum-1.0.0-cp36-cp36m-manylinux1_i686.whl (109.5 kB view details)

Uploaded CPython 3.6m

pendulum-1.0.0-cp36-cp36m-macosx_10_11_x86_64.whl (101.0 kB view details)

Uploaded CPython 3.6mmacOS 10.11+ x86-64

pendulum-1.0.0-cp35-cp35m-manylinux1_x86_64.whl (108.8 kB view details)

Uploaded CPython 3.5m

pendulum-1.0.0-cp35-cp35m-manylinux1_i686.whl (109.4 kB view details)

Uploaded CPython 3.5m

pendulum-1.0.0-cp35-cp35m-macosx_10_11_x86_64.whl (101.0 kB view details)

Uploaded CPython 3.5mmacOS 10.11+ x86-64

pendulum-1.0.0-cp27-cp27m-manylinux1_x86_64.whl (108.6 kB view details)

Uploaded CPython 2.7m

pendulum-1.0.0-cp27-cp27m-manylinux1_i686.whl (109.2 kB view details)

Uploaded CPython 2.7m

pendulum-1.0.0-cp27-cp27m-macosx_10_11_x86_64.whl (101.0 kB view details)

Uploaded CPython 2.7mmacOS 10.11+ x86-64

File details

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

File metadata

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

File hashes

Hashes for pendulum-1.0.0.tar.gz
Algorithm Hash digest
SHA256 a6968b1ad6a943160f63dba47a896685b8b2977066e5dc102e466d1ab3f93061
MD5 e3db44cfb2be549a7724a387c2803d30
BLAKE2b-256 90b733ce759a4a8b2543b98a42689b7ce360b2b7ccc67ec1ec8901d97a5feb7a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pendulum-1.0.0-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 158fbba156884ba54db32e33776dcc0f374d2fe52d4487c3ec0c5890b0960d79
MD5 a34ac9055e3aab42ea658050c6d06c22
BLAKE2b-256 5c8da2bbfc0f023195e09eb87f44560aa8c7c2b963c20a1cf4c356c8feeb0232

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pendulum-1.0.0-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 90c2d6ef8f5cfec03a1bbad72a727f28f58f24309033330988b27692f2667be3
MD5 57fbe235e4bc54853cbbf6f513e89659
BLAKE2b-256 a9b5623a4bb52347bf729fb9e3ad1f2fbd9321c3ed759c3621b3d99a621bbf62

See more details on using hashes here.

File details

Details for the file pendulum-1.0.0-cp36-cp36m-macosx_10_11_x86_64.whl.

File metadata

File hashes

Hashes for pendulum-1.0.0-cp36-cp36m-macosx_10_11_x86_64.whl
Algorithm Hash digest
SHA256 3b43453f39f569bced312123cfb92bdcedc96172232e25f7f33a76e680898f99
MD5 6acd567b83f622fce0bdd6d838cc9609
BLAKE2b-256 bf4a24fea31866117d29240d3e452af82a147ff5a0c044affd36e22c143b8c0d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pendulum-1.0.0-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 b74534477f8dee612641ded377064198b9a298cd2efcd492f9de6ff5e89d89ed
MD5 b812457e65a46ede7a04c51779d70dd9
BLAKE2b-256 d31df5e26051744418d46687212c98e572adfd7be6f5e493244646583caedb82

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pendulum-1.0.0-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 b849f0fa85e4df25fe77069b0ef0f10148e1f50d8651f2c2c6a879e559046f71
MD5 2db24580f4fddf8a64525144913d76be
BLAKE2b-256 a16e26a1965822a95838d341b8976c3a2b01373b0c3c80cafb0c552cd679d256

See more details on using hashes here.

File details

Details for the file pendulum-1.0.0-cp35-cp35m-macosx_10_11_x86_64.whl.

File metadata

File hashes

Hashes for pendulum-1.0.0-cp35-cp35m-macosx_10_11_x86_64.whl
Algorithm Hash digest
SHA256 d3e538d8ae6eccd5c86423932dd41264898d1bc81bee9eb53a8607d8afc5c4ab
MD5 9a042804daf3fece57f4c4dedf95464a
BLAKE2b-256 cc64af870d0bbbc0e79b48b98924c7d31300344202b83eb00b3c4cc252a22f5d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pendulum-1.0.0-cp27-cp27m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 6127d0f2008e8155330c16aa9cf6545533bb0f91efee73fcefb09c6f782969e3
MD5 dc5ab9a2758d770bd1fd2c237e676832
BLAKE2b-256 ab47b159a21474a39af3a8f1d475c523815fe1fe416d54fb5419b32af62358c1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pendulum-1.0.0-cp27-cp27m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 64386a74f3bc592c929e0ff4515ff7bb94bf09c4f42cc6e96bf4ee071c9d356a
MD5 d420a878a79e28315b8ba44de87ad7d4
BLAKE2b-256 3da87ac73c3babe0bd20654d6a07543331159ff6586df6f35d11c5b666260eca

See more details on using hashes here.

File details

Details for the file pendulum-1.0.0-cp27-cp27m-macosx_10_11_x86_64.whl.

File metadata

File hashes

Hashes for pendulum-1.0.0-cp27-cp27m-macosx_10_11_x86_64.whl
Algorithm Hash digest
SHA256 32e00b36bd9555778fab3948d071b565b16c349e17e0857ac5060542a6aa1e97
MD5 a2fa5ddd41743b9050fa84b8330b586e
BLAKE2b-256 fbf94945f9b7b93e3a3bd966d8d80d9eab157a8a1add20beb77f2758aac56dc4

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