Changing Timezone in Python

FeaImg ChangeTimezone

Sometimes product or infrastructure engineers are required to work on infrastructures that are located all over the world. They must collaborate with machines located in the United States, Asia, Europe, and the United Kingdom, among other places. As a result, time zones are much more significant for Python.

With the constant progress of today’s programming languages, several modules are maintained in nearly all programming languages. Python has a time zone package called pytz that enables cross-platform time zone computations in real-time.


Installing pytz module

To begin, we will import the pytz module’s timezone library. This pip command can be used to install this module.

pip install pytz

Importing necessary modules

In addition, we’ll need to import DateTime from the DateTime module. We can specify the format in which we want our date and time output to be for consistency’s sake.

from pytz import timezone
from datetime import datetime

Getting current Date and Time

In this program, we will specify the format as YY-MM-DD HH:MM: SS.

We will call the now() method on the DateTime library to obtain the current time in the specified format when the code is executed. The output timezone format, on the other hand, will be in its DateTime object format.

So, to make it more readable, we convert it to string time format by calling the strftime() method on it.

time_format = '%Y-%m%d %H:%M:%S %Z%z'
default_now = datetime.now()
formatted_now = datetime.now().strftime(time_format)

print("Date Time in defaut format: ", default_now, '\n')
print("Date Time in string format: ", formatted_now)
Date Time in defaut format:  2021-11-22 09:26:40.054185 

Date Time in string format:  2021-1122 09:26:40 

Converting current date and time to multiple timezones

Now we’ll make a list of time zones and loop through it, converting the current time to that time zone. We will include time zones from the United States, Europe, Asia, and the standard UTC.

timezones = ['US/Central', 'Europe/London', 
             'Asia/Kolkata', 'Australia/Melbourne', 'UTC']
for tz in timezones:
  dateTime = datetime.now(timezone(tz)).strftime(time_format)
  print(f"Date Time in {tz} is {dateTime}")
Date Time in US/Central is 2021-1122 03:27:58 CST-0600
Date Time in Europe/London is 2021-1122 09:27:58 GMT+0000
Date Time in Asia/Kolkata is 2021-1122 14:57:58 IST+0530
Date Time in Australia/Melbourne is 2021-1122 20:27:58 AEDT+1100
Date Time in UTC is 2021-1122 09:27:58 UTC+0000

Following that, we will loop through all of the timezones in the list we created as a parameter of the DateTime library’s now() method to get all of the time zones and the current time in each time zone. We’ll also convert it to string format to make it easier to read.


Conclusion

Congratulations! You just learned how to change timezones in Python. Hope you enjoyed it! 😇

Liked the tutorial? In any case, I would recommend you to have a look at the tutorials mentioned below:

  1. Python datetime module – An Ultimate Guide
  2. How to Work with Python TimeDelta?
  3. How to Wait for a Specific Time in Python?
  4. Convert string to datetime using Python strptime()

Thank you for taking your time out! Hope you learned something new!! 😄