One of the most significant built-in modules in Python is the Datetime module. By offering numerous unique answers to actual programming issues, it is incredibly versatile and strong. One of my favorite tools, as an example, is the time delta.
The Datetime module does have some restrictions, though. For instance, dealing with time zones typically highlights the lack. We occasionally need to include some third-party libraries as supplements. The Datetime module has various features that are also uncommonly used in other programming languages. This post will introduce Pendulum, a third-party library that will fix all of the problems with the built-in Datetime module.
One well-liked Python Datetime library for simple Datetime manipulation is the pendulum. It offers a more streamlined and user-friendly API. It makes the issue of date operations including time zones that are complicated by native Datetime instances easier to understand. Although it derives from the common Datetime library, it offers superior features. In projects that already use built-in Date Time classes, you can therefore introduce Pendulum’s Datetime instances.
To install this module run this command into your terminal:
pip install pendulum
Why should a developer choose Pendulum over other libraries or conventional datetime libraries?
1. Drop-In Alternative to datetime
Pendulum just inherits the Python Datetime object, after all. We don’t have to worry about using some of the Date time module’s original features. Python Datetime objects are known as Pendulum Datetime objects.
2. Time zones
The time zones shall be the Pendulum library’s most outstanding feature. This is another important problem with the built-in Datetime module. Prior to Python 3.9, if we wanted to use the time zones, we had to use pytz.
We can easily generate a Datetime object with a time zone using the Pendulum library.
In the preceding example, we simultaneously created two objects. The time zones are different, though. Pendulum also makes it simple to compare the times.
3. Datetime Parsing
Probably the most frequent use case in programming is parsing a Date time. That’s a task that the Python Datetime module excels at. Python utilizes a different format %Y %m %d than the majority of other programming languages, though. The following common format codes are used with Pendulum. Additionally, along with a few other widely used formats, it fully supports the RFC 3339 and ISO 8601 formats. This means that when parsing a string into a Datetime object, we don’t need to specify the format codes. Numerous popular Datetime extensions, such date utile, are also integrated into the pendulum. We can pass the flag strict=False to instruct the library to use the date utile parser as a backup.
Pendulum supports a lot more formats on the fly in addition to these. Take the Datetime object, which just contains numbers. The right Datetime is provided by Pendulum when you enter the year, the week number, and the day of that week. It is considerably simpler to use exact=True than the Python Datetime module if we specifically want a date object or a time object.
4. String Formatting
Pendulum inherits from Python Datetime, thus we can utilize all the available methods (). The format code is easier to understand and can be used to modify the output string. Adding arbitrary strings to the format string and having them escape from the format is another amazing feature.
5. Human Readability
The time delta tool works exceptionally well for comparison tasks in the built-in Python Datetime module. Pendulum can even make it better by giving output that is more suited to humans when comparing two Datetime objects. As an illustration, the method diff_for_humans() compares a Datetime object with the current time and produces very readable results.
6. Find Relative Datetime
Finding the relative Datetime based on a given one is one of the areas where the built-in Python Datetime may be improved. For instance, we must utilize the relative delta from the date utile module to get the final day of the current month. Additionally, the code is difficult to understand because day=31 is used as the argument, even if this works when the month has fewer than 31 days. It cannot be simpler than it is in Pendulum.
Finding a day of the week using the built-in Datetime module is another drawback. This is likely the simplest method, for instance, to determine the date of next Monday. While it accomplishes the job, it is difficult to read. It will take some time for a developer to comprehend the logic behind this line of code. Pendulum makes it as simple as that.
Pendulum uses enumeration for that, so we don’t even have to consider if Monday should be indicated by 0 or 1. Similar to that, the previous() method can be used to determine the Tuesday before. By giving the option keep time=True, we can also maintain the time component.
7. Few more comforts
This library contains additional features that are concealed. Just a couple more, like yesterday or tomorrow. Additionally, it is quite simple to output Datetime with locale for many linguistic and cultural contexts. One more illustration: What year was a person born on January 1st, 1988, and how old is this person?
The pendulum library in Python is useful for handling and converting dates and times with ease. It offers a simple and intuitive interface for working with time zones, and provides various methods for performing arithmetic with dates and times. The library also has features for formatting and parsing dates and times, making it an efficient tool for working with time-related data. Overall, the pendulum library can greatly simplify time-related tasks in Python, making it a valuable tool for developers.