Pendulum: Python datetimes made easy

Posted by Afsal on 31-Mar-2023

Hi Pythonistas!

Today we will learn about the pendulum, which makes DateTime handling easier.Let us learn this with examples.

Installation

pip install pendulum

Printing current time in the user’s timezone 

now = pendulum.now()
print("Now: ", now)

Output

Now:  2023-03-27T09:31:12.330871+05:30

Print now on specific timezone

now_utc = pendulum.now('UTC')

print("UTC Now", now_utc)

Output

UTC Now 2023-03-27T04:01:12.330930+00:00

Changing to another timezone

now = pendulum.now()

now_in_paris = now.in_timezone("Europe/Paris")

print("Now in Paris: ", now_in_paris)

Output

Now in Paris:  2023-03-27T06:01:12.330871+02:00

Creating DateTime object

in_utc = pendulum.datetime(2013, 3, 31, 0, 59, 59)

in_paris = pendulum.datetime(2013, 3, 31, 0, 59, 59, tz="Europe/Paris")

print(in_utc)

print(in_paris)

Output

In UTC:  2013-03-31T00:59:59+00:00

In Paris:  2013-03-31T00:59:59+01:00

By default time zone will be UTC

Shifting time

now = pendulum.now()
print("Now: ", now)

after_1_hours = now.add(hours=1)
print("After 1 hours: ", after_1_hours)

before_1_hours = now.subtract(hours=1)
print("Before 1 hours: ", before_1_hours)

Output

Now:  2023-03-27T09:51:43.201506+05:30

After 1 hours:  2023-03-27T10:51:43.201506+05:30

Before 1 hours:  2023-03-27T08:51:43.201506+05:30

Period

Period is the difference between two date-times. The period is iterable

now = pendulum.now()
period = now - now.subtract(days=5)
print("Remaining days: ", period.remaining_days)

for dt in period:
    print("dt: ", dt)

Output

Remaining days:  5
dt:  2023-03-22T10:21:21.694834+05:30
dt:  2023-03-23T10:21:21.694834+05:30
dt:  2023-03-24T10:21:21.694834+05:30
dt:  2023-03-25T10:21:21.694834+05:30
dt:  2023-03-26T10:21:21.694834+05:30
dt:  2023-03-27T10:21:21.694834+05:30

Duration

duration = pendulum.duration(days=30)

print(duration.in_hours())

print(duration.in_words())

Output

Duration in hours:  720

Duration in words:  4 weeks 2 days

Generate datetime from a string

dt = pendulum.from_format('2023-03-31', 'YYYY-MM-DD')

print(dt)

dt = pendulum.from_format('2023-03-31', 'YYYY-MM-DD', tz="Asia/Kolkata")

print(dt)

Output

2023-03-31T00:00:00+00:00

2023-03-31T00:00:00+05:30

Parsing datetime from string

The library natively supports the RFC 3339 format, most ISO 8601 formats and some other common formats.

dt = pendulum.parse('2023-03-31T22:00:00')

print(dt)

dt = pendulum.parse('2023-03-31T22:00:00', tz="Asia/Kolkata")

print(dt)

Output

2023-03-31T22:00:00+00:00

2023-03-31T22:00:00+05:30

Formatting

print(dt.format('dddd DD MMMM YYYY'))

Output

Friday 31 March 2023

This is similar to our datetime module. For the extra information check documentation

Difference for human

print(pendulum.now().add(years=5, days=2).diff_for_humans())

print(pendulum.now().add(seconds=10).diff_for_humans())

Output

in 5 years

in a few seconds

These are things I find interesting in this module you can check the documentation for more details.

I hope you have learned something from this post. Please share valuable suggestions with afsal@parseltongue.co.in