What is pytz?
pytz is a Python module mostly used for resolving the issues related to time zone differences. This module determines the time of any region using the local timezone name taking UTC time as the reference.
The datetime is a python built-in library. This library can perform almost all the operations on date and time. However, datetime always takes the local machine’s time. This is considerably not the issue when you work at your home or in an offline environment. But it matters a lot for online servers or applications.
Why do we need it?
Let’s assume a website has registered users from different corners of the world. Every user can see the current time of his/her location immediately after login to the dashboard.
Suppose the server sends the time to the user dashboard. In this case, python built-in datetime uses datetime.now() to get the current time. But this may show the wrong time to the user’s location. This because datetime.now() is actually the server’s machines current time. It completely depends on the provider server’s location. So the user location time is different from the server time.
Here comes the pytz module. The pytz module has an object named localize() which takes the user’s timezone name and generates the current time exact current time for the user’s location.
There are many uses of pytz. Such as,
- Converting any datetime(past, present, or future) to any other local time
- Converting time considering the daylight saving time record for any specific region
- Transforming time using timezone offset
- Retrieving any timezone’s current offset from UTC
A simple example of the use of pytz?
As I mentioned before, pytz is not python’s built-in library. So we need to install it to the system python environment. You can simply install python pytz to your system default python environment by using the following command –
pip install pytz
After your installation, we are ready to go to use it.
My local timezone name is Dhaka/Asia. Therefore, I could simply achieve my time now using datetime.now(). But this time I will find time now in Montreal.
Firstly, we need to import the necessary modules. We still need the datetime module to perform operations on date.
>>> from datetime import datetime >>> from pytz import timezone
Secondly, we need to set the timezone using the timezone name abbreviation that pytz uses. For Montreal, it is America/Montreal.
>>> tz = timezone("America/Montreal")
You may ask me how you can find your desired pytz timezone abbreviation name. Well, you have to find your desired timezone name from the list of pytz modules timezone abbreviation.
Here is the code to see it –
import pytz for time_zone in pytz.all_timezones: print(time_zone)
Finally, we will find the current time of Montreal.
>>> datetime.now(tz) datetime.datetime(2020, 9, 24, 17, 29, 2, 944829, tzinfo=<DstTzInfo 'America/Montreal' EDT-1 day, 20:00:00 DST>)
This output shows every detail of the current time of Montreal. However, there are several ways to get the formatted string datetime using the datetime module.
Here is an example for you –
>>> datetime.now(tz).strftime("%Y-%m-%d %H:%M") '2020-09-24 17:29'