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

# Parsing of a date with wrong day
arrow.get('2015-06-31')
# <Arrow [2015-06-01T00:00:00+00:00]>

pendulum.parse('2016-06-31')
# ValueError: day is out of range for month

# fromtimestamp with timezone displays wrong offset
arrow.Arrow.fromtimestamp(0, pytz.timezone('Europe/Paris'))
# <Arrow [1970-01-01T01:00:00+00:09]>

pendulum.from_timestamp(0, pytz.timezone('Europe/Paris'))
# fromtimestamp() is also possible
# <Pendulum [1970-01-01T01:00:00+01: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.

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-0.6.6.tar.gz (50.8 kB view details)

Uploaded Source

Built Distributions

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

pendulum-0.6.6-cp35-cp35m-manylinux1_x86_64.whl (91.7 kB view details)

Uploaded CPython 3.5m

pendulum-0.6.6-cp35-cp35m-manylinux1_i686.whl (92.4 kB view details)

Uploaded CPython 3.5m

pendulum-0.6.6-cp35-cp35m-macosx_10_11_x86_64.whl (83.9 kB view details)

Uploaded CPython 3.5mmacOS 10.11+ x86-64

pendulum-0.6.6-cp27-cp27m-manylinux1_x86_64.whl (91.5 kB view details)

Uploaded CPython 2.7m

pendulum-0.6.6-cp27-cp27m-manylinux1_i686.whl (92.2 kB view details)

Uploaded CPython 2.7m

pendulum-0.6.6-cp27-cp27m-macosx_10_11_x86_64.whl (83.9 kB view details)

Uploaded CPython 2.7mmacOS 10.11+ x86-64

File details

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

File metadata

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

File hashes

Hashes for pendulum-0.6.6.tar.gz
Algorithm Hash digest
SHA256 b8fa6a3ac32d8904234ee07e620c3d6aefd8338c644bf8e053d580936ce6e74d
MD5 977f430b57a34b6d2fe98c8c08bbc703
BLAKE2b-256 f823cb6facfce303ba6215fe355dad6e9d2a6a027b7e65152c7ca8c0d94247bd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pendulum-0.6.6-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 b77405c52f202c5f70ed5e7b6812bc9c02a508bd3eac731299d979d4e604ce82
MD5 2fcfd6847e9d28ef353a74727824fbba
BLAKE2b-256 3db9072ed2eb041d02299e092fd53e5bd610d4bbcd2875c941fd1acb696ef200

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pendulum-0.6.6-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 f9553beef2acd91e36dce6df0a2435beb05db258c268066c0ff69da88d21db80
MD5 eda4b68ec97c589e06a059b0e3083e04
BLAKE2b-256 7d9463318b67e98256ebee3ca36d00e7d943aba0429d4457ee26854dbc37a149

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pendulum-0.6.6-cp35-cp35m-macosx_10_11_x86_64.whl
Algorithm Hash digest
SHA256 b293584305db262c723446d59b37286be848ee10b31569f5ce2b08c64f2828d3
MD5 c4d3e8cf64684bf22e425a7864b499de
BLAKE2b-256 9de502338e5704c7e7e5d29b9c09cb17b213d974e071d9f7df4bf356732e4a87

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pendulum-0.6.6-cp27-cp27m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 c53b31359b9205f59afb2289e72a8983d50de1fcbbbb332bf14d518a533d3ba9
MD5 0f7ae2263cc235da6d8e7be6c77cb7e1
BLAKE2b-256 9f21bc23ccdd59b47af0e818b315f632d40ea90778d087fab127fc583a11907e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pendulum-0.6.6-cp27-cp27m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 4f8a62cbaeea56f99beccfdb50b6e82461a54477eaa0c72bb6fd68e699b1e583
MD5 c724953b3d1e1c4a3a8c6d026065afee
BLAKE2b-256 c0f93c12d6aa0aeb83fd20a4a686b597c017f799f997f97cf494ac9c21f6ed48

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pendulum-0.6.6-cp27-cp27m-macosx_10_11_x86_64.whl
Algorithm Hash digest
SHA256 ad3f4d50dccf2c47fa8f1f8ed0cc4cac568f7c631c58703c07724a84aa39842b
MD5 cc67a39cf243eaaf11b8e04c768719b8
BLAKE2b-256 e9d678bd9581d156d18cc23b609a3043214631147c3edd34a70a673f0c3637ab

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