Package: pendulum
Python datetimes made easy.Supports Python3.8 and newer.>>>importpendulum>>>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.hours23>>>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'ResourcesOfficial WebsiteDocumentationIssue TrackerWhy Pendulum?Nativedatetimeinstances 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.Pendulumprovides a cleaner and more easy to use API while still relying on the standard library.
So it’s stilldatetimebut better.Unlike other datetime libraries for Python, Pendulum is a drop-in replacement
for the standarddatetimeclass (it inherits from it), so, basically, you can replace all yourdatetimeinstances byDateTimeinstances in your code (exceptions exist for libraries that check
the type of the objects by using thetypefunction likesqlite3orPyMySQLfor instance).It also removes the notion of naive datetimes: eachPenduluminstance is timezone-aware
and by default inUTCfor ease of use.Pendulum also improves the standardtimedeltaclass by providing more intuitive methods and properties.LimitationsEven though theDateTimeclass is a subclass ofdatetimethere 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:sqlite3will use thetype()function to determine the type of the object by default. To work around it you can register a new adapter:frompendulumimportDateTimefromsqlite3importregister_adapterregister_adapter(DateTime,lambdaval:val.isoformat(' '))mysqlclient(formerMySQLdb) andPyMySQLwill use thetype()function to determine the type of the object by default. To work around it you can register a new adapter:importMySQLdb.convertersimportpymysql.convertersfrompendulumimportDateTimeMySQLdb.converters.conversions[DateTime]=MySQLdb.converters.DateTime2literalpymysql.converters.conversions[DateTime]=pymysql.converters.escape_datetimedjangowill use theisoformat()method to store datetimes in the database. However sincependulumis always timezone aware the offset information will always be returned byisoformat()raising an error, at least for MySQL databases. To work around it you can either create your ownDateTimeFieldor use the previous workaround forMySQLdb:fromdjango.db.modelsimportDateTimeFieldasBaseDateTimeFieldfrompendulumimportDateTimeclassDateTimeField(BaseDateTimeField):defvalue_to_string(self,obj):val=self.value_from_object(obj)ifisinstance(value,DateTime):returnvalue.to_datetime_string()return''ifvalisNoneelseval.isoformat()ContributingContributions are welcome, especially with localization.Getting startedTo work on the Pendulum codebase, you’ll want to clone the project locally
and install the required dependencies viapoetry.$gitclonegit@github.com:sdispater/pendulum.git$poetryinstallLocalizationIf 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 theclockutility:./clocklocalecreate<your-locale>It will generate a directory inpendulum/localesnamed after your locale, with the following
structure:<your-locale>/
    - custom.py
    - locale.pyThelocale.pyfile must not be modified. It contains the translations provided by
the CLDR database.Thecustom.pyfile 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 theendata as a reference to see which data is needed.You should also add tests for the created or modified locale.