Python datetimes made easy.
Project description
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for pendulum-0.6.1-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7c8721db252c87a1ab787586626d95bd8caea268de01a53c44865f24610c83d1 |
|
MD5 | 1b518c922f90f0de3b92fa9f849dc40f |
|
BLAKE2b-256 | 3fd58ab96ad7e3d1022d713d7cfd25023c6cf6d486c3a3ea69016d3b61b7e561 |
Hashes for pendulum-0.6.1-cp35-cp35m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d78905e7ca59897b247380c154cb3b79e0ceb31f53f3e649e782f3346464efb6 |
|
MD5 | b96788e1f68f9ae841ae531c0e9493f5 |
|
BLAKE2b-256 | f1511d90f25b82011ca8c2148020ba9ce67a021ed81d57839ee5ded96efbf0db |
Hashes for pendulum-0.6.1-cp35-cp35m-macosx_10_11_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 500271c27102f71a6f0080766c38fa223ebefeff5f78cc9a0f39f7fba179105d |
|
MD5 | de16e1d88c10e86fbcd1740bb6b80196 |
|
BLAKE2b-256 | 09ac17fdb73480be1a9463bf339c11c52f2ba111ddb0c1b555df609166df4b4b |
Hashes for pendulum-0.6.1-cp27-cp27m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d8f52e343604ebe729e6e8f3ac36172efe27dde4f239283f16eef353727b3691 |
|
MD5 | 0019fe6b506ead44a66eaf1bda8fce6d |
|
BLAKE2b-256 | 05e8e9aec8e214705033b1d49abe8ad2076e7641d8c3abbafa8dea3478e3f56d |
Hashes for pendulum-0.6.1-cp27-cp27m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 120072a924cdc6d9c37b7890837da8813a560656f49a2de71f693c30cf4ee0d8 |
|
MD5 | 85d0b1441f335e8a644a5b3dfbef07c7 |
|
BLAKE2b-256 | e47ae706df0644d1536f690ea4525814dfe9f35f6431a1be01574440b4fbf2b7 |
Hashes for pendulum-0.6.1-cp27-cp27m-macosx_10_11_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5a1ef8bf74a9409e8c31ab900b3d781a953d1c963a243c3bea88c8fc6fcd76a2 |
|
MD5 | cc039a34d440b39696fd26dc7d30fd5b |
|
BLAKE2b-256 | fe4b0cd8376ef9fad7d7251dd3294f2422b8025b5a7e61c9f33584a409c7b8f4 |