Python Program for Changing Timezone

Product or infrastructure engineers are sometimes required to work on infrastructures located all over the world. They must work with machines in the United States, Asia, Europe, and the United Kingdom, among other locations. As a result, time zones play a significantly larger role in Python.

Several modules are kept in practically all programming languages due to the constant advancement of today’s computer languages. Python provides a time zone library called pytz that allows for real-time cross-platform time zone computations.

Installation of pytz Module:

Before going to the coding part, we will import the timezone library from the pytz module. This module can be installed using the pip command.

pip install pytz

Also, import datetime function from datetime module.

Program for Changing Timezone in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import timezone function from pytz module using the import keyword.
  • Import datetime function from datetime module using the import keyword.
  • Give some random date and time format and store it in a variable (The default format is YY-MM-DD HH:MM: SS).
  • Get the present time using the datetime.now() function.
  • Store it in another variable.
  • Convert the datetime into given string format using the strftime() function by passing the above given time format as an argument to it.
  • Store it in another variable.
  • Print the default date-time format.
  • Print the date-time in the above-given string format.
  • The Exit of the Program.

Below is the implementation:

# Import timezone function from pytz module using the import keyword.
from pytz import timezone
# Import datetime function from datetime module using the import keyword.
from datetime import datetime
# Give some random date and time format and store it in a variable (The default
# format is YY-MM-DD HH:MM:SS).
gvn_timeformt = '%Y-%m%d %H:%M:%S %Z%z'
# Get the present time using the datetime.now() function.
# Store it in another variable.
defalt_date_time = datetime.now()
# Convert the datetime into given string format using the strftime() function
# by passing the above given time format as an argument to it.
# Store it in another variable.
formtd_date_time = datetime.now().strftime(gvn_timeformt)
# Print the default date time format.
print("The default date time format = ", defalt_date_time)
# Print the date time in the above given string format
print("The date time in the above given string format = ", formtd_date_time)

Output:

The default date time format = 2021-12-06 19:33:22.854984 
The date time in the above given string format = 2021-1206 19:33:22
Conversion of current date-time to a different timezones

 

Approach:

  • Import timezone function from pytz module using the import keyword.
  • Import datetime function from datetime module using the import keyword.
  • Give some random date and time format and store it in a variable (The default format is YY-MM-DD HH:MM: SS).
  • Give the list of timezones as static input and store it in a variable.
  • Loop in the above-given timezones list using the for loop.
  • Inside the for loop, get the date-time by passing the place to the timezone() function and apply the strftime() function by passing the time format as an argument to it.
  • Print the date-time of given zones.
  • The Exit of the Program.

Below is the implementation:

# Import timezone function from pytz module using the import keyword.
from pytz import timezone
# Import datetime function from datetime module using the import keyword.
from datetime import datetime
# Give some random date and time format and store it in a variable
# (The default format is YY-MM-DD HH:MM: SS).
timeeformt = '%Y-%m%d %H:%M:%S %Z%z'
# Give the list of timezones as static input and store it in a variable.
t_zones_lst = ['Europe/London', 'US/Central',
               'Asia/Kolkata', 'UTC', 'Australia/Melbourne', ]
# Loop in the above-given timezones list using the for loop.
for t_zone in t_zones_lst:
    # Inside the for loop, get the date-time by passing the place to the
    # timezone() function and apply strftime() function by passing the time
    # format as argument to it.
    rslt_date_time = datetime.now(timezone(t_zone)).strftime(timeeformt)
    # Print the date-time of given zones.
    print(f"{t_zone} Date Time = {rslt_date_time}")

Output:

Europe/London Date Time = 2021-1208 16:36:11 GMT+0000 
US/Central Date Time = 2021-1208 10:36:11 CST-0600 
Asia/Kolkata Date Time = 2021-1208 22:06:11 IST+0530 
UTC Date Time = 2021-1208 16:36:11 UTC+0000 
Australia/Melbourne Date Time = 2021-1209 03:36:11 AEDT+1100