Working with dates and times in Odoo is very crucial. Sometimes, Odoo’s built-in features are not sufficient for handling complex scenarios and timezone differences. In such cases, we need a more robust solution for date and time management. This is where Pendulum comes in; it’s a modern library that makes date and time handling simpler, more intuitive, and less error-prone.
Let’s look at how Pendulum can simplify date and time calculation in Odoo.
To begin using Pendulum, install it in your environment:
pip install pendulum
Once installed, you’re ready to use it in your Odoo code.
1. Getting Current Time
Fetching the current time:
import pendulum
current_time = pendulum.now()
print(current_time)
Unlike standard datetime, this already includes timezone information.
2. Timezone Handling
Converting to different timezones:
import pendulum
utc_time = pendulum.now('UTC')
local_time = utc_time.in_timezone('Asia/Kolkata')
print(local_time)
3. Using Pendulum with Odoo Fields
Odoo provides datetime values like this:
from odoo import fields
odoo_time = fields.Datetime.now()
To work with Pendulum, you can convert it:
import pendulum
pendulum_time = pendulum.parse(str(odoo_time))
4. Date Calculations
Perform simple calculations:
import pendulum
now = pendulum.now()
after_three_days = now.add(days=3)
previous_week = now.subtract(weeks=1)
5. Formatting Dates
Formatting Date and Time:
formatted_date = pendulum.now().format('YYYY-MM-DD HH:mm:ss')
print(formatted_date)6. Example in Odoo
Check whether a record is older than a week:
import pendulum
record_date = pendulum.parse(str(record.create_date))
if record_date < pendulum.now().subtract(days=7):
print("This record is older than 7 days")
This kind of logic becomes much cleaner with Pendulum. In day-to-day Odoo development, where you’re constantly dealing with timezones and date calculations, it just makes things feel more easier to manage.
To read more about How to Configure and Use Date & Datetime Fields in Odoo 19, refer to our blog How to Configure and Use Date & Datetime Fields in Odoo 19.