Skip to main content

Python datetimes made easy

Project description

https://img.shields.io/pypi/v/pendulum.svg https://img.shields.io/pypi/l/pendulum.svg Pendulum Build status

Python datetimes made easy.

Supports Python 3.8 and newer.

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

Resources

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.

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()

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pendulum-3.0.0b1.tar.gz (83.6 kB view hashes)

Uploaded Source

Built Distributions

pendulum-3.0.0b1-pp310-pypy310_pp73-win_amd64.whl (295.7 kB view hashes)

Uploaded PyPy Windows x86-64

pendulum-3.0.0b1-pp310-pypy310_pp73-musllinux_1_1_x86_64.whl (538.5 kB view hashes)

Uploaded PyPy musllinux: musl 1.1+ x86-64

pendulum-3.0.0b1-pp310-pypy310_pp73-musllinux_1_1_aarch64.whl (540.0 kB view hashes)

Uploaded PyPy musllinux: musl 1.1+ ARM64

pendulum-3.0.0b1-pp310-pypy310_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (387.9 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

pendulum-3.0.0b1-pp310-pypy310_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (338.5 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ARM64

pendulum-3.0.0b1-pp310-pypy310_pp73-macosx_11_0_arm64.whl (352.9 kB view hashes)

Uploaded PyPy macOS 11.0+ ARM64

pendulum-3.0.0b1-pp310-pypy310_pp73-macosx_10_7_x86_64.whl (369.5 kB view hashes)

Uploaded PyPy macOS 10.7+ x86-64

pendulum-3.0.0b1-pp39-pypy39_pp73-win_amd64.whl (295.7 kB view hashes)

Uploaded PyPy Windows x86-64

pendulum-3.0.0b1-pp39-pypy39_pp73-musllinux_1_1_x86_64.whl (538.5 kB view hashes)

Uploaded PyPy musllinux: musl 1.1+ x86-64

pendulum-3.0.0b1-pp39-pypy39_pp73-musllinux_1_1_aarch64.whl (540.0 kB view hashes)

Uploaded PyPy musllinux: musl 1.1+ ARM64

pendulum-3.0.0b1-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (387.9 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

pendulum-3.0.0b1-pp39-pypy39_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (338.5 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ARM64

pendulum-3.0.0b1-pp39-pypy39_pp73-macosx_11_0_arm64.whl (352.8 kB view hashes)

Uploaded PyPy macOS 11.0+ ARM64

pendulum-3.0.0b1-pp39-pypy39_pp73-macosx_10_7_x86_64.whl (369.5 kB view hashes)

Uploaded PyPy macOS 10.7+ x86-64

pendulum-3.0.0b1-pp38-pypy38_pp73-win_amd64.whl (295.4 kB view hashes)

Uploaded PyPy Windows x86-64

pendulum-3.0.0b1-pp38-pypy38_pp73-musllinux_1_1_x86_64.whl (538.5 kB view hashes)

Uploaded PyPy musllinux: musl 1.1+ x86-64

pendulum-3.0.0b1-pp38-pypy38_pp73-musllinux_1_1_aarch64.whl (540.0 kB view hashes)

Uploaded PyPy musllinux: musl 1.1+ ARM64

pendulum-3.0.0b1-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (387.8 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

pendulum-3.0.0b1-pp38-pypy38_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (338.9 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ARM64

pendulum-3.0.0b1-pp38-pypy38_pp73-macosx_11_0_arm64.whl (352.9 kB view hashes)

Uploaded PyPy macOS 11.0+ ARM64

pendulum-3.0.0b1-pp38-pypy38_pp73-macosx_10_7_x86_64.whl (369.6 kB view hashes)

Uploaded PyPy macOS 10.7+ x86-64

pendulum-3.0.0b1-pp37-pypy37_pp73-musllinux_1_1_x86_64.whl (539.6 kB view hashes)

Uploaded PyPy musllinux: musl 1.1+ x86-64

pendulum-3.0.0b1-pp37-pypy37_pp73-musllinux_1_1_aarch64.whl (541.0 kB view hashes)

Uploaded PyPy musllinux: musl 1.1+ ARM64

pendulum-3.0.0b1-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (389.0 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

pendulum-3.0.0b1-pp37-pypy37_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (340.1 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ARM64

pendulum-3.0.0b1-pp37-pypy37_pp73-macosx_10_7_x86_64.whl (371.3 kB view hashes)

Uploaded PyPy macOS 10.7+ x86-64

pendulum-3.0.0b1-cp312-none-win_arm64.whl (289.8 kB view hashes)

Uploaded CPython 3.12 Windows ARM64

pendulum-3.0.0b1-cp312-none-win_amd64.whl (295.3 kB view hashes)

Uploaded CPython 3.12 Windows x86-64

pendulum-3.0.0b1-cp312-cp312-musllinux_1_1_x86_64.whl (538.5 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.1+ x86-64

pendulum-3.0.0b1-cp312-cp312-musllinux_1_1_aarch64.whl (539.6 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.1+ ARM64

pendulum-3.0.0b1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (388.1 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

pendulum-3.0.0b1-cp312-cp312-manylinux_2_17_s390x.manylinux2014_s390x.whl (440.7 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ s390x

pendulum-3.0.0b1-cp312-cp312-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (365.5 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ppc64le

pendulum-3.0.0b1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (338.4 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ARM64

pendulum-3.0.0b1-cp312-cp312-macosx_11_0_arm64.whl (352.7 kB view hashes)

Uploaded CPython 3.12 macOS 11.0+ ARM64

pendulum-3.0.0b1-cp312-cp312-macosx_10_7_x86_64.whl (369.4 kB view hashes)

Uploaded CPython 3.12 macOS 10.7+ x86-64

pendulum-3.0.0b1-cp311-none-win_arm64.whl (289.8 kB view hashes)

Uploaded CPython 3.11 Windows ARM64

pendulum-3.0.0b1-cp311-none-win_amd64.whl (295.4 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

pendulum-3.0.0b1-cp311-cp311-musllinux_1_1_x86_64.whl (538.6 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ x86-64

pendulum-3.0.0b1-cp311-cp311-musllinux_1_1_aarch64.whl (539.5 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ ARM64

pendulum-3.0.0b1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (388.1 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

pendulum-3.0.0b1-cp311-cp311-manylinux_2_17_s390x.manylinux2014_s390x.whl (440.7 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ s390x

pendulum-3.0.0b1-cp311-cp311-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (365.5 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ppc64le

pendulum-3.0.0b1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (338.4 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARM64

pendulum-3.0.0b1-cp311-cp311-macosx_11_0_arm64.whl (352.7 kB view hashes)

Uploaded CPython 3.11 macOS 11.0+ ARM64

pendulum-3.0.0b1-cp311-cp311-macosx_10_7_x86_64.whl (369.4 kB view hashes)

Uploaded CPython 3.11 macOS 10.7+ x86-64

pendulum-3.0.0b1-cp310-none-win_amd64.whl (295.3 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

pendulum-3.0.0b1-cp310-cp310-musllinux_1_1_x86_64.whl (538.6 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

pendulum-3.0.0b1-cp310-cp310-musllinux_1_1_aarch64.whl (539.5 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ ARM64

pendulum-3.0.0b1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (388.1 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

pendulum-3.0.0b1-cp310-cp310-manylinux_2_17_s390x.manylinux2014_s390x.whl (440.7 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ s390x

pendulum-3.0.0b1-cp310-cp310-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (365.5 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ppc64le

pendulum-3.0.0b1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (338.4 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARM64

pendulum-3.0.0b1-cp310-cp310-macosx_11_0_arm64.whl (352.8 kB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

pendulum-3.0.0b1-cp310-cp310-macosx_10_7_x86_64.whl (369.4 kB view hashes)

Uploaded CPython 3.10 macOS 10.7+ x86-64

pendulum-3.0.0b1-cp39-none-win_amd64.whl (295.5 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

pendulum-3.0.0b1-cp39-cp39-musllinux_1_1_x86_64.whl (538.8 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

pendulum-3.0.0b1-cp39-cp39-musllinux_1_1_aarch64.whl (539.9 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ ARM64

pendulum-3.0.0b1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (388.4 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

pendulum-3.0.0b1-cp39-cp39-manylinux_2_17_s390x.manylinux2014_s390x.whl (441.2 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ s390x

pendulum-3.0.0b1-cp39-cp39-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (366.1 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ppc64le

pendulum-3.0.0b1-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (338.9 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARM64

pendulum-3.0.0b1-cp39-cp39-macosx_11_0_arm64.whl (352.9 kB view hashes)

Uploaded CPython 3.9 macOS 11.0+ ARM64

pendulum-3.0.0b1-cp39-cp39-macosx_10_7_x86_64.whl (369.7 kB view hashes)

Uploaded CPython 3.9 macOS 10.7+ x86-64

pendulum-3.0.0b1-cp38-none-win_amd64.whl (295.4 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

pendulum-3.0.0b1-cp38-cp38-musllinux_1_1_x86_64.whl (538.8 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ x86-64

pendulum-3.0.0b1-cp38-cp38-musllinux_1_1_aarch64.whl (540.0 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ ARM64

pendulum-3.0.0b1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (388.3 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

pendulum-3.0.0b1-cp38-cp38-manylinux_2_17_s390x.manylinux2014_s390x.whl (441.3 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ s390x

pendulum-3.0.0b1-cp38-cp38-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (366.2 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ppc64le

pendulum-3.0.0b1-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (338.9 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ARM64

pendulum-3.0.0b1-cp38-cp38-macosx_11_0_arm64.whl (353.0 kB view hashes)

Uploaded CPython 3.8 macOS 11.0+ ARM64

pendulum-3.0.0b1-cp38-cp38-macosx_10_7_x86_64.whl (369.8 kB view hashes)

Uploaded CPython 3.8 macOS 10.7+ x86-64

pendulum-3.0.0b1-cp37-none-win_amd64.whl (295.3 kB view hashes)

Uploaded CPython 3.7 Windows x86-64

pendulum-3.0.0b1-cp37-cp37m-musllinux_1_1_x86_64.whl (538.7 kB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ x86-64

pendulum-3.0.0b1-cp37-cp37m-musllinux_1_1_aarch64.whl (540.0 kB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ ARM64

pendulum-3.0.0b1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (388.3 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

pendulum-3.0.0b1-cp37-cp37m-manylinux_2_17_s390x.manylinux2014_s390x.whl (441.4 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ s390x

pendulum-3.0.0b1-cp37-cp37m-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (366.2 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ ppc64le

pendulum-3.0.0b1-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (338.9 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ ARM64

pendulum-3.0.0b1-cp37-cp37m-macosx_11_0_arm64.whl (353.0 kB view hashes)

Uploaded CPython 3.7m macOS 11.0+ ARM64

pendulum-3.0.0b1-cp37-cp37m-macosx_10_7_x86_64.whl (369.8 kB view hashes)

Uploaded CPython 3.7m macOS 10.7+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page