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.4.tar.gz (75.0 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.4-cp36-cp36m-manylinux1_x86_64.whl (123.7 kB view details)

Uploaded CPython 3.6m

pendulum-1.4.4-cp36-cp36m-manylinux1_i686.whl (123.5 kB view details)

Uploaded CPython 3.6m

pendulum-1.4.4-cp36-cp36m-macosx_10_13_x86_64.whl (383.9 kB view details)

Uploaded CPython 3.6mmacOS 10.13+ x86-64

pendulum-1.4.4-cp35-cp35m-manylinux1_x86_64.whl (123.7 kB view details)

Uploaded CPython 3.5m

pendulum-1.4.4-cp35-cp35m-manylinux1_i686.whl (123.5 kB view details)

Uploaded CPython 3.5m

pendulum-1.4.4-cp27-cp27m-manylinux1_x86_64.whl (123.4 kB view details)

Uploaded CPython 2.7m

pendulum-1.4.4-cp27-cp27m-manylinux1_i686.whl (123.2 kB view details)

Uploaded CPython 2.7m

File details

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

File metadata

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

File hashes

Hashes for pendulum-1.4.4.tar.gz
Algorithm Hash digest
SHA256 601e52cb0425e94b1784b6613a9085e0066ae1fa1915d18771884b67e93cac5c
MD5 85ff49b389e0d251efdd95f2b8638810
BLAKE2b-256 85a59fc15751f9725923b170ad37d6c61031fc9e941bafd5288ca6ee51233284

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pendulum-1.4.4-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 76ee830b4b57a3f8244a228505bf9c55285cc92f1a200c8578b0ca54f8185861
MD5 6a197496340b96348b25a48a0d873b10
BLAKE2b-256 304702f04abed54918d2a3f1da602a8254247670b2e1a99b4b1f02734a27e71e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pendulum-1.4.4-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 253983de6d64a01909c2524e4ab27febd0d3987d001ea6ab93a7b945fdc0e6c6
MD5 d4f2dfb526363b38a62f722cc5923f80
BLAKE2b-256 691024d5376a9d1604766196c3ddd13be1c1616ad0eeee31bf127b32cdcdd587

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pendulum-1.4.4-cp36-cp36m-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 4c945ed6a3b0afab8c2f1b1e3e26bb23ad0a9be6f201604111a8217cea78e7ab
MD5 adc05b64c3cc76f9a9c067b1fe86f43a
BLAKE2b-256 05202b6261cadc4d71f6c08bd0986732acbe474c08d97194a6bfc025bff10a05

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pendulum-1.4.4-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 f30fb1149e4f67b3aaa9eae874dca7bbf49788ac121d702486f5b9fe549e7920
MD5 206497d3fc5fe4400aea9cda44c3b217
BLAKE2b-256 ccbaa7ef16485be134f0acec118b6b9036741d664cc0042c7d6e48c613ea9d88

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pendulum-1.4.4-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 3d8b280a903fb25bdba258203bbcd0533c5c04a65878f6e0700931dedd2bae72
MD5 f4b3b3d3cbab82b0bf379f4fe16be120
BLAKE2b-256 cb7d37a9b89c3bfbceafe083f52e5d783a39e79b4d7998f133bfbeee94830858

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pendulum-1.4.4-cp27-cp27m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 b9a7ef02ad6255292f35218c595f8be35e0ca3c7ac19e633ff2de96480f26ab3
MD5 3b8ea5304556af34be7683ecaf913c37
BLAKE2b-256 879f8d1dc7789430b746b671533297ed633c44af05b33da8d395389afb27f9e8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pendulum-1.4.4-cp27-cp27m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 501670f3b1d581395ec4094aff7c13dca6b699d1810cf15c446433b9e736eb4a
MD5 490eaf9e3cc60c261ea00d9087547efe
BLAKE2b-256 3bd299d1ad74abb8feab462ed065f35f694bf4b6465052a84b0007269371895d

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