Updated June 16, 2023
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 the timezone time module and the datetime module. In Python, using the time module with a timezone is very easy and error-free, but the datetime module with a 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 the 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 of these modules.
Let us see how the timezone can be displayed using a time module with an example. To display the timezone, we use the format code “%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") print(s)
In the above program, we can see we have imported the time module to display the timezone in the 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 the timezone using the datetime module with an 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:") print(now) s = now.strftime("%Z %z") print("This will display the timezone:") print(s)
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 must import the “pytz” module.
Firstly we need to install “pytz” module and then import it to get timezone information, and this can be done below
- Pip install pytz
Now to use this “pytz” module, we need to import it into our Python program.
- import datetime
- import pytz
To get the right timezone, we need to use the local module along with pytz module. Let us see an example below to demonstrate this.
import datetime import pytz print("Program to display timezone using pytz module:") print("\n") dt = datetime.datetime(2020, 6, 12, 18, 30) print("The time and date is displayed for the given time and date in datetime object:") print(dt) print("\n") local_tz = pytz.timezone('Indian/Mahe') dt = local_tz.localize(dt) print("This will display the time of India:") print(dt) target_tz = pytz.timezone('UTC') dt = target_tz.normalize(dt) print("To convert the localize to normalize to UTC the time is") print(dt)
In the above program, we can see we are using pytz module to display the timezone with 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:") print("\n") 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") print(now) print(“\n”) utc = tzutc() utc_now = now.astimezone(utc) print("The below is converted utc datetime of the above given local timezone") print(utc_now)
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 a 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:") print("\n") r = "5 P.M. 21st January" print("The given date time in the string format is as follows") print(r) print("\n") p = parse(r) print("The parsed date time of the above given date and time") print(p)
In the above program, we can see we have used dateutil for parsing the date-time in string format to the 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 it. 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 parses the string format to the date-time object.
This is a guide to Python Timezone. Here we discuss an introduction to Python Timezone, syntax, and how it work with programming examples. You can also go through our other related articles to learn more –