Introduction to Python Timezone
In this article, we will discuss timezones in Python. The timezone, in general, is used to display the conversion of date and time between timezones, which means it is used to display the timezone it is using to print the time and date. In Python, the timezone is related to time concepts where two modules display timezones are time module and datetime module. In Python, using the time module with timezone is very easy and error-free, but the datetime module with timezone is error-prone. Therefore Python does not support datetime timezone properly. To use timezone work properly we need to import different modules and we will see in the section.
How does Python Timezone work?
In this section, we will see timezone which is used to display the timezone it is using to display the correct date and time using two modules in Python. They are the time module and the datetime module. Now in this section, we will see how to demonstrate the timezone using both these modules.
Let us see how timezone can be displayed using a time module with an example. To display timezone we use format code as “%z”.
from time import gmtime, strftime
print("Program to demonstrate to display timezone using time module")
s = strftime("%r, %z, %Z")
print("This will dispay the name of the time zones with time")
In the above program, we can see we have imported time module and to display timezone in readable string then we have used strftime() function and we have specified the format code “%Z” to display the timezone and we can see it will result in “UTC”.
How to Demonstrate Timezone?
Now let us see how to demonstrate timezone using the datetime module with example. In Python, it does not properly support the datetime and timezone as its error-prone than using the time module.
from datetime import datetime as dt
print("Python program to demonstrate strftime() function using datetime module")
now = dt.now()
print("The current date and time without formatting is as follows:")
s = now.strftime("%Z %z")
print("This will display the timezone:")
In this above program, we can see using the datetime module we cannot display the timezone. Therefore to do this we need to import “pytz” module or dateutil module to display the timezone.
In Python, the datetime module has two types one is offset-naïve which does not provide timezone information and offset aware. As Python has o built-in timezone support we need to import the “pytz” module.
Firstly we need to install “pytz” module and then import to get timezone information and this can be done as below
- Pip install pytz
Now to use this “pytz” module we need to import it in our python program.
- import datetime
- import pytz
To get the right timezone we need to use local module along with pytz module. Let us see an example below to demonstrate this.
print("Program to display timezone using pytz module:")
dt = datetime.datetime(2020, 6, 12, 18, 30)
print("The time and date is displayed for the given time and date in datetime object:")
local_tz = pytz.timezone('Indian/Mahe')
dt = local_tz.localize(dt)
print("This will display the time of India:")
target_tz = pytz.timezone('UTC')
dt = target_tz.normalize(dt)
print("To convert the localize to normalize to UTC the time is")
In the above program, we can see we are using pytz module to display the timezonewith time for India.
Examples to Implement Python Timezone
Below are the examples mentioned:
from dateutil.tz import *
from datetime import datetime
print("To demonstrate timezone using dateutil module and also used for parsing:")
now = datetime.now()
local = tzlocal()
now = datetime.now()
now = now.replace(tzinfo = local)
print("The local timezone is as follows which is timezone awarew datetime")
utc = tzutc()
utc_now = now.astimezone(utc)
print("The below is converted utc datetime of the above given local timezone")
In the above program, we can have dateutil module with tz as its object to print the timezone of the local using tzlocal() module. And to convert the timezone aware datetime to utc then we use astimezone() function.
This dateutil module is also very easy as a pytz module with displaying timezone. The dateutil module is also used for parsing, such as if the date and time are given in the string format then the dateutil can be used to convert the string format to date-time object. Let us see a simple example below to demonstrate the paring of string to date-time format.
from dateutil.parser import *
print("Program to demonstrate parsing technique using dateutil module:")
r = "5 P.M. 21st January"
print("The given date time in the string format is as follows")
p = parse(r)
print("The parsed date time of the above given date and time")
In the above program, we can see we have used dateutil for parsing the date-time in string format to date-time object.
In this article, we conclude that the timezone is displayed using the time and datetime module. We also saw the time module supports timezone but the datetime module will not support. To do this we have seen in the article we have used the pytz module and dateutil module. In this article, we also saw how the dateutil module is used for parsing the string format to the date-time object.
This is a guide to Python Timezone. Here we discuss an introduction to Python Timezone, syntax, how does it work with programming examples. You can also go through our other related articles to learn more –