Handling timezone in Python
There are some standard libraries we can use for timezones, here we’ll use pytz. This library has a timezone class for handling arbitrary fixed offsets from UTC and timezones.
pytz is a third-party package that you have to install. To install pytz use the following command –
pip install pytz
After installation import the pytz package and now let’s see how we can pass in the timezones. Basic syntax in Python to get the date and time information is :
The above syntax returns the current local date-time without any timezone information. But with the use of the pytz package we can handle this date-time information in the various timezones –now() gives us the option to pass in a time zone, so if you leave the time zone empty then it will also return the current local date-time. The output of now() depends upon the machine. The local time and time zone settings of the host machine will determine the output.
So in order to work with the timezone smoothly, it is recommended to use the UTC as your base timezone. To get the Universal Time Coordinated i.e. UTC time we just pass in the parameter to now() function. To get the UTC time we can directly use the ‘pytz.utc’ as a parameter to now() function as ‘now(pytz.utc)’. The offset will be shown at the end as (+ or – hours).
The below code shows the local time of the machine and the UTC time with offset.
As you can see now we got the date time info of the local machine and in UTC, the timezone offset at the end is +00:00.
If we want to get date time info of specific timezone we just need to add the timezone in the parameter –
.now(pytz.timezone(‘ YOUR_TIMEZONE ‘))
The parameter pytz.timezone allows us to specify the timezone information as a string. We can pass in any available timezone and will get the current date time of that timezone, and it will also print the offset with respect to the UTC. i.e., the difference between UTC timezone(+00:00) and the specified time zone.
Here we got the different date time according to different timezones with offset. The offset will also show the (+/-) hours. We can also convert any time zone to a different time zone.
Converting between Timezones
astimezone() method is used to manipulate i.e., to convert the datetime objects to the new specified datetime object. It uses an instance of the datetime object and returns new timezone information.
astimezone(pytz.timezone(‘ NEW_TIMEZONE ‘))
To get exactly what we need the following syntax will be followed.
dtObj = datetimeObject_instance.astimezone(pytz.timezone(‘ NEW_TIMEZONE ‘))
- dtObj : which stores the returned datetime object with newly created time date
- datetimeObject_instance : currently available datetime object (i.e., reference to the local date time)
- NEW_TIMEZONE : timezone information which should be changed to
pytz has a large list of timezones available you can print them using the following method.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course