Python datetimes made easy.
Project description
Python datetimes made easy.
Supports Python 2.7+ and 3.2+.
>>> 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.5.3-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1e86814d6dbfc19866effe3707632556e93bae29b8bbfca25ec32ea7761a0e9a |
|
MD5 | 677d556566fb47dba529c9cb90096dd3 |
|
BLAKE2b-256 | 53dcac9c228f4a4d5615cd91293d2b3ae925c15b8d4bf9468d2f0c756baf6ff7 |
Hashes for pendulum-0.5.3-cp35-cp35m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8191414f06d9082982cad4e31f5d5b5e2cf8cb09e2796d428e230db9d40332c2 |
|
MD5 | efba9191039e745f6765c812ed1f654d |
|
BLAKE2b-256 | 23363c2e3fcb7a99240a749dee8b6fa775055cc2676bb2ec328258797f91f553 |
Hashes for pendulum-0.5.3-cp35-cp35m-macosx_10_11_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ca007fda0e4ee176ceda4a2b53f37440b495ecdf3e206854098e938a187246b7 |
|
MD5 | fb61279dc9c2e2fb86f16595010c1cb8 |
|
BLAKE2b-256 | 0a0d9b3fb18e788d6dcb6672780798595832c022150ef47ccbbfdc6f0127ecca |
Hashes for pendulum-0.5.3-cp27-cp27m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bd50f1c7d7a41ad01de37de7a8e0dad002b709e2c104cc4cf88a7ebd654cfa52 |
|
MD5 | 8790a56550558c52f0247dde177a1e7e |
|
BLAKE2b-256 | e8064bfb51d8a24a2185664476fb622022de08bc12b89a59b8312b9a652665e0 |
Hashes for pendulum-0.5.3-cp27-cp27m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a41ddcf3829c04fd0f3a8a965d6c121d2470e49f60dcbfd3a85e02fbfd213184 |
|
MD5 | 43971de190b2ad242fcc4b50436afa23 |
|
BLAKE2b-256 | 1c42bc7a49bd89bc4d746623af8bc33411d65a3f4a07a6a3882f23dcc06f7941 |
Hashes for pendulum-0.5.3-cp27-cp27m-macosx_10_11_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 93f8c43e121edbb5edef0815a07d05fd2577b0a6045a8c991381bb5278d25743 |
|
MD5 | 6d0dde7ef64e22bfec6f74fa6a918c75 |
|
BLAKE2b-256 | 7744d35698f1029fea2e9347059948b26647ea43e6d2616650ea351c45711c23 |