Python datetimes made easy
Project description
Python datetimes made easy.
Supports Python 2.7 and 3.4+.
>>> 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)
>>> 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.datetime(2013, 3, 31, 2, 30, tz='Europe/Paris')
'2013-03-31T03:30:00+02:00' # 2:30 does not exist (Skipped time)
# Proper handling of dst transitions
>>> just_before = pendulum.datetime(2013, 3, 31, 1, 59, 59, 999999, tz='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 DateTime 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.datetime(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 DateTime 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 DateTime
from sqlite3 import register_adapter
register_adapter(DateTime, 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 DateTime
MySQLdb.converters.conversions[DateTime] = MySQLdb.converters.DateTime2literal
pymysql.converters.conversions[DateTime] = 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 DateTime
class DateTimeField(BaseDateTimeField):
def value_to_string(self, obj):
val = self.value_from_object(obj)
if isinstance(value, DateTime):
return value.to_datetime_string()
return '' if val is None else val.isoformat()
Resources
Contributing
Contributions are welcome, especially with localization.
Getting started
To work on the Pendulum codebase, you’ll want to clone the project locally and install the required depedendencies via poetry.
$ git clone git@github.com:sdispater/pendulum.git
$ poetry install
Localization
If you want to help with localization, there are two different cases: the locale already exists or not.
If the locale does not exist you will need to create it by using the clock utility:
./clock locale create <your-locale>
It will generate a directory in pendulum/locales named after your locale, with the following structure:
<your-locale>/
- custom.py
- locale.py
The locale.py file must not be modified. It contains the translations provided by the CLDR database.
The custom.py file is the one you want to modify. It contains the data needed by Pendulum that are not provided by the CLDR database. You can take the en data as a reference to see which data is needed.
You should also add tests for the created or modified locale.
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-2.1.0-cp38-cp38m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0cbbd4f30c69a283690d9ed8e58e44a990e067e59ee05b5ef55d022b38659aeb |
|
MD5 | 148ae1b5fd6c7aad5c316b1601d8e43f |
|
BLAKE2b-256 | 92dfe93d3923a92a56d27ebc14835b066d324153d238c0f421c6723230bf71a2 |
Hashes for pendulum-2.1.0-cp38-cp38-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | aa13ddea12fd871d3191f633f08090b91ea2e80fb0ed50a7a149add7f680b12d |
|
MD5 | 6defeac04177868cb1f18fbc3d98f79d |
|
BLAKE2b-256 | 65dbf63332f0fb369c1d7ccfa38f1bdf1aaf4f6f341007688fcfee444c76b694 |
Hashes for pendulum-2.1.0-cp38-cp38-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6cf0f876cd088ee1578266f4231121376747aa90c3ed3b8e212a8344a9920061 |
|
MD5 | 6218b7c2982db7ae7aaeea11d8e5690c |
|
BLAKE2b-256 | db3c3a6091f4f81b524a70ac85a5b83db9a492eb2533f08a7b8d5a30d936aea9 |
Hashes for pendulum-2.1.0-cp38-cp38-macosx_10_13_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | eb7e349bb2d1b2b418d094e2179d6768561e8242fd8cb640b5aaba735f3e91d1 |
|
MD5 | c9bea68ab50fa804478d3b2015626eda |
|
BLAKE2b-256 | c86543a8395c13e7bf7560939d41abf4c1269d4f44dbcec2e7847a0fc44f9edc |
Hashes for pendulum-2.1.0-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2788945a0111d5325fd27ae3e3b18b741e440d20bdb7d4ea22fce7c9a4fbbf40 |
|
MD5 | 30f6466c72cf718240909eb272243640 |
|
BLAKE2b-256 | 26431b845522bdb549d5e27dbb132d817792e3f3d62f79dbf6f5288eaefecc6a |
Hashes for pendulum-2.1.0-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | aa560bd39d94f3889646422f1e65b8dfd025bf6288d43e5c2e31d4f972aaf2e4 |
|
MD5 | 9bc7e49f508456c4e35e46c7878553bf |
|
BLAKE2b-256 | 3b0878526c01e27e5ea8dfb0114023568015097c4de3216b8b2fc57f7f9c6f63 |
Hashes for pendulum-2.1.0-cp37-cp37m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4420e058110740a8193fb0709350dfc6ac790a99c345fc4e92e24df0f834ddcb |
|
MD5 | b99b33cd3a20c4842c838f985c198bcb |
|
BLAKE2b-256 | db494c1eae0f3bae32e4d2711ee6f813d5f255f84ebfd06804929abc92332b14 |
Hashes for pendulum-2.1.0-cp37-cp37m-macosx_10_13_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | edd00e6b43698762e10bfda508cc9c06bad88c0703a9b37e412aec1189e06e23 |
|
MD5 | 4d074c886a447a3e685c7adb3ffb60de |
|
BLAKE2b-256 | dac7d13cb7c04afe38fe67299d31c89b15914d75d9bb6912fbe438f005ecbc48 |
Hashes for pendulum-2.1.0-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 816e01dcb0ba4ffcf2ceaafe4d644174fea680361e909f6f8ba0a4fdb2ccae24 |
|
MD5 | b4ab03ac8b28ad945d5884f02b98b24b |
|
BLAKE2b-256 | 9df323cfd1541f2f5ea05057b38fa67391fe639fd145dcd3632d7914d9e59d87 |
Hashes for pendulum-2.1.0-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 701127e1f0ff7c253cc0c07f29becc5f9210547914e0bbe59ffd9fa064d7c3c8 |
|
MD5 | f9e3af598e3590c9ecbe851b68e43bd0 |
|
BLAKE2b-256 | fce82eb9f8a5ce6511f2f1d44f621171388765f34fe1d5fa74d50368aa620bbf |
Hashes for pendulum-2.1.0-cp36-cp36m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a79a72a7fd1092a7c69ddd8580a0be5365ded40c9f9c865623c7665742e3b888 |
|
MD5 | b28fa39fd79a73d8a639d1173bb0b161 |
|
BLAKE2b-256 | 89f60b2ab9fd88b1dcc821131886e603d6e9b9b8731692d1fed3779997731bef |
Hashes for pendulum-2.1.0-cp36-cp36m-macosx_10_13_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 75a62e3f98499283fafe8ef4b44f81052e84825b00a0b64609dd8a06985382b9 |
|
MD5 | 46f3984da2ed115c16f14677ba2550af |
|
BLAKE2b-256 | a31a32aaf0a1368219511a83affa70b0af08c7ef4c7edaff69afde7857da70ec |
Hashes for pendulum-2.1.0-cp35-cp35m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ac3c6a992beeb4c9bd90c317a1bb2a6cba159b49a49b6dd3c86b5bacb86f3d50 |
|
MD5 | bdc79cae7a21ffee544e8cde4500ea2a |
|
BLAKE2b-256 | 3e54c5f6205f8a48b826dde534f3c0563fa9ad0fd07e95d606493a3c31c5ddf8 |
Hashes for pendulum-2.1.0-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ff7f3420de0c0cf21c1fc813d581fcfa4a1fb6d87f09485880b3e1204eb9cdd7 |
|
MD5 | 2c7f203a6b500273738579195b85ede1 |
|
BLAKE2b-256 | 29a96097eb53d1d96308050a00ea89200c5e9ec1d6b6675da5a4982c8a7bb0fd |
Hashes for pendulum-2.1.0-cp35-cp35m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d42d1e870541eeaf3fe0500aac0c76a85bd4bd53ebed74f9a7daf8f01ac77374 |
|
MD5 | e14d019a32a23133f53d1a5b7ef9dd1a |
|
BLAKE2b-256 | c46f55ddd7f74e8b920055927cec36ada813a92da5892c3677278c54213d0567 |
Hashes for pendulum-2.1.0-cp35-cp35m-macosx_10_13_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 575934b65b298eeb99c5a5b1673c945fc5c99e2b56caff772a91bc4b1eba7b82 |
|
MD5 | 517aaea183dadba3c894b5486b6dca36 |
|
BLAKE2b-256 | 0fe90c5131738b3017ed42d61f7ccce44d72539579e6cce6bed7aa4a371b051b |
Hashes for pendulum-2.1.0-cp27-cp27m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 70007aebc4494163f8705909a1996ce21ab853801b57fba4c2dd53c3df5c38f0 |
|
MD5 | f351e97bd43b3367c9f248e6c0a91b61 |
|
BLAKE2b-256 | e54288b46ed7d0f3bc721ca6f3597bd67a093610792889864db404731df7702d |
Hashes for pendulum-2.1.0-cp27-cp27m-macosx_10_13_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9eda38ff65b1f297d860d3f562480e048673fb4b81fdd5c8c55decb519b97ed2 |
|
MD5 | 129e9749f1984fe24022028c3d8dd604 |
|
BLAKE2b-256 | 4a69241fb1e304cae65b68ef0de77a8e1051b007ed368f1b4bb3d3a5575fac58 |