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.7.0.tar.gz (62.3 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.7.0-cp36-cp36m-macosx_10_11_x86_64.whl (97.8 kB view details)

Uploaded CPython 3.6mmacOS 10.11+ x86-64

pendulum-0.7.0-cp35-cp35m-manylinux1_x86_64.whl (105.5 kB view details)

Uploaded CPython 3.5m

pendulum-0.7.0-cp35-cp35m-manylinux1_i686.whl (106.1 kB view details)

Uploaded CPython 3.5m

pendulum-0.7.0-cp35-cp35m-macosx_10_11_x86_64.whl (97.8 kB view details)

Uploaded CPython 3.5mmacOS 10.11+ x86-64

pendulum-0.7.0-cp27-cp27m-manylinux1_x86_64.whl (105.3 kB view details)

Uploaded CPython 2.7m

pendulum-0.7.0-cp27-cp27m-manylinux1_i686.whl (106.0 kB view details)

Uploaded CPython 2.7m

pendulum-0.7.0-cp27-cp27m-macosx_10_11_x86_64.whl (97.7 kB view details)

Uploaded CPython 2.7mmacOS 10.11+ x86-64

File details

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

File metadata

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

File hashes

Hashes for pendulum-0.7.0.tar.gz
Algorithm Hash digest
SHA256 3cfbfb9c62b05a012f55cc363ce449a9d17388cd3bfbf823f82f4093699e112c
MD5 2f099b1e94b6158d275475aaa3002fb2
BLAKE2b-256 afb6aea118b901b37aceeebb80afbe43ae2db94d605cdd766eee98765ba7f220

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pendulum-0.7.0-cp36-cp36m-macosx_10_11_x86_64.whl
Algorithm Hash digest
SHA256 81455883f225abd38ef06eb02eb898330f656e9d55106575f9a5b0832b34537c
MD5 2e39351134cc186d6e666b3334d6b8d4
BLAKE2b-256 c15dfd536df5c477ddc911f8106f2885719642641bfb05a29aa170ee71924c7b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pendulum-0.7.0-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 439b811b555ff7b8ad006d181f2b90d202ecdf2ba732922641f7457322215b47
MD5 edaabae517287eb0244b5e8590e954df
BLAKE2b-256 052a8d4beae63894818ee03952399b84a03fa1038f5cf14c8f3dd51183f8ed6e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pendulum-0.7.0-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 c07576443b25d84fe239050e3fac256d799d886cd5903e6510f328b61b739fe1
MD5 eb9fe95e3110dfb29c93ffb810571f2b
BLAKE2b-256 d4830ef7de93077564621f03cdab41b750bacc3ce049823a18caa85e71d58b79

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pendulum-0.7.0-cp35-cp35m-macosx_10_11_x86_64.whl
Algorithm Hash digest
SHA256 99aa5dd7a288f59c8f6cd7d66df70c62d861d4be2ca0f7f24f8b8ccb3d1133e7
MD5 6c82da71348fd283f1c22cf27f75ef8d
BLAKE2b-256 2eba32f32e68c634979bf1a84e1f754b77539168b7d491799c6c2dcd31c552c2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pendulum-0.7.0-cp27-cp27m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 c5d178026ec7e43c77a1e2554f511486bf48920c6b938a1c8acede65651e1ec0
MD5 27a17ebc2d1846a9c3316080bc425c8a
BLAKE2b-256 1a33bf5744a583c4105259aff628610b4dec1cabc445c817822cbe0649999f4d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pendulum-0.7.0-cp27-cp27m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 0eeb8f82f21e47feecba00b2a5773614eae170f762d7db5a376134bb0d1bb517
MD5 cbc0d750fa0b396e12701ff5391387e6
BLAKE2b-256 4011ed1da7f1550501b3fa83d703f747c2fd6817210c555594bd206b00a7a1de

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pendulum-0.7.0-cp27-cp27m-macosx_10_11_x86_64.whl
Algorithm Hash digest
SHA256 ff5947915fbaa60657f1175866744318dc474a3010b449a2d33a7fa565428d79
MD5 a5ac9a702b68ca1149742c716140acce
BLAKE2b-256 49e690165de336ace0f0e696c6cb51f666d6c03e4d39a867902c745103fce72f

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