
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Pendulum Module in Python
The Pendulum Module is used in Python for datetime manipulation and time zone management. Using this module, you can manage date, time, and other related things over time.
The Pendulum Module's ability to manage time zones easily is one of its main advantages. It makes it simple to deal with dates and times from all around the world since it can instantly convert between various time zones. The Pendulum Module is a powerful tool for anyone who is dealing with time-sensitive data. You can install this module with following command ?
pip install pendulum
We have some methods in Pendulum Module
add
subtract
format string
period
parse string
in_timezone
add() Method
This method increments a Pendulum instance by one or more units (year, month, day, hour, minute, second, etc.).
Example
add(days=5) will add 5 days to a Pendulum instance and return the new date.
import pendulum now = pendulum.now() result = now.add(days=3) print(result)
Output
2023-06-15T12:59:34.170914+05:30
This output indicates that it is June 15, 2023, at 12:59 PM and 34.170914 seconds in the UTC+05:30 timezone.
subtract() Method
This method subtracts one or more units (year, month, day, hour, minute, second, etc.) from a Pendulum instance.
Example
subtract(days=3, hours=2) will subtract 3 days and 2 hours from a Pendulum instance and return the new time.
import pendulum now = pendulum.now() result = now.subtract(days=3, hours=2) print(result)
Output
2023-06-09T11:21:58.461996+05:30
This output indicates that it is June 9, 2023, at 11:21 PM and 58.461996 seconds in the UTC+05:30 timezone.
format() Method
The format() method is available in the Pendulum module and is used to display the date and time in a specified format. It receives a format string and uses that to parse the Pendulum instance into a specified format.
Example
In this example we are creating different-different format string in the Pendulum module.
import pendulum # Creating a Pendulum instance dt = pendulum.datetime(2023, 6, 12, 2, 19, 35, 123456, tz='UTC') # Format the Pendulum instance using format() form_date = dt.format('YYYY-MM-DD') form_time = dt.format('HH:mm:ss.SSSSSS') form_datetime = dt.format('YYYY-MM-DD HH:mm:ss') # Print the formatted results print("Date formatted:",form_date) print("Time formatted:",form_time) print("DateTime Formatted:",form_datetime)
Output
Date formatted: 2023-06-12 Time formatted: 02:19:35.123456 DateTime Formatted: 2023-06-12 02:19:35
Period() Method
A Period object will be returned by the diff() function or when you subtract one DateTime instance from another.
Example
we can create period instance with the help of period() method.
import pendulum start_date = pendulum.datetime(2022, 6, 12) end_date = pendulum.datetime(2023, 6, 12) period = pendulum.period(start_date, end_date) period.days
Output
365
In this program we are finding the difference between two datetime with the use of period() function.
parse() Method
The parse() method of the pendulum module is used to create a pendulum instance by parsing the given date or time string. It automatically recognizes various date and time formats and is useful for parsing them into pendulum objects.
Example
Here is an example in which we use the parse() method in Pendulum.
import pendulum result = pendulum.parse("2023-06-12T03:30:48", tz="UTC") print(result)
Output
2023-06-12T03:30:48+00:00
In this example, we have created a pendulum DateTime instance result using the parse() method. We use the date and time string "2023-06-12T03:30:48" as an argument to parse() . The tz parameter is used so that the parsed DateTime can be checked. In this example we set it to the "UTC" timezone. The parse() method automatically detects the format of the input string and creates the Pendulum instance accordingly.
in_timezone() Method
The in_timezones() function is available in the Pendulum module and is used to convert a given instance into one or more timezones. Returns a new Pendulum instance containing the date and time according to the given timezone.
Example
Here is an example that shows how to use the in_timezones() method.
import pendulum res = pendulum.now("UTC") final_res = res.in_timezone("Asia/Kolkata") print("Current datetime in the UTC timezone: ",res) print("Converted datetime in the 'Asia/Kolkata' timezone: ",final_res)
Output
Current datetime in the UTC timezone: 2023-06-14T03:53:50.575082+00:00 Converted datetime in the 'Asia/Kolkata' timezone: 2023-06-14T09:23:50.575082+05:30
In this program first we print the current time of "UTC" timezones then we convert into "Asia/Kolkata" timezone with the help of in_timezone method.
Conclusion
The Pendulum Module is an important asset for Python users who deal with dates and times. Its useful features, user-friendly interface, and effortless timezone compatibility can streamline your tasks and enhance your efficiency.