Pendulum Library in Python
If you are a Python developer looking for a library to deal with date and time operations, you might have heard of the Pendulum library. Pendulum is a powerful and easy-to-use Python library for working with dates, times, time zones, and durations. It provides an intuitive interface for handling date and time operations without requiring extensive knowledge of the underlying library. In this article, we will be exploring the Pendulum library and its features in detail.
Installing Pendulum
Before we dive into using the Pendulum library, we must install it. Pendulum can be installed using pip, the Python package installer, by running the following command:
$ pip install pendulum
Once Pendulum is installed, we can begin using it in our Python projects.
Creating a Pendulum Object
The main class in the Pendulum library is the Pendulum class, which represents a point in time. To create a Pendulum object, we simply import the library and use the pendulum.datetime()
method. The method takes in values for the year, month, day, hour, minute, and second. If any of these values are not provided, they default to 0.
import pendulum
# create a datetime object with current time
now = pendulum.datetime()
# create a datetime object with specific time
dt = pendulum.datetime(2022, 12, 31, 23, 59, 59)
# create a datetime object from a string
dt_str = pendulum.from_format('2022-12-31 23:59:59', 'YYYY-MM-DD HH:mm:ss')
# create a datetime object in a specific timezone
paris_tz = pendulum.timezone('Europe/Paris')
dt_paris = pendulum.datetime(2022, 12, 31, 23, 59, 59, tz=paris_tz)
Manipulating Pendulum Objects
Once we have created a Pendulum object, we can manipulate it using the many methods available in the Pendulum library. We can add or subtract time to a datetime object, change the timezone, format the output, and much more.
# add 1 day to the datetime object
dt = dt.add(days=1)
# subtract 1 hour from the datetime object
dt = dt.subtract(hours=1)
# change the timezone of the datetime object
dt_paris = dt.in_timezone('Europe/Paris')
# format the datetime object to a string
dt_str = dt.to_datetime_string()
# output the datetime object in a specific format
output_fmt = 'MMMM Do YYYY, h:mm:ss a'
dt_formatted = dt.format(output_fmt)
Working with Timezones
Pendulum makes working with timezones easy. By default, all datetime objects created using Pendulum are in the local timezone. However, we can easily create datetime objects in other timezones; all we need is the timezone string.
# create a datetime object in a specific timezone
paris_tz = pendulum.timezone('Europe/Paris')
dt_paris = pendulum.datetime(2022, 12, 31, 23, 59, 59, tz=paris_tz)
# get the timezone offset of the datetime object
tz_offset = dt_paris.offset_hours
Duration and Interval
Pendulum provides two additional classes to work with time: Duration and Interval. Duration represents a length of time, while Interval represents the time between two datetime objects.
# create a duration object representing 1 day and 2 hours
duration = pendulum.Duration(days=1, hours=2)
# create an interval object representing the time between two datetime objects
start = pendulum.datetime(2022, 1, 1)
end = pendulum.datetime(2022, 1, 2)
interval = pendulum.Interval(start, end)
# output the duration object in a specific format
output_fmt = 'h:mm'
duration_formatted = duration.in_words(locale='en', format=output_fmt)
Conclusion
In this article, we have explored the Pendulum library, a powerful and easy-to-use Python library for working with dates, times, time zones, and durations. We have seen how to create datetime objects, manipulate them, work with timezones, and use the Duration and Interval classes. Pendulum provides an intuitive interface that simplifies date and time operations while still providing a high degree of flexibility and power. It is definitely worth checking out for any Python developer working with date and time operations.