Converting between Timezones in Python

I had a hard time when I first dealt with conversions between timezones, mostly because I was not aware of PST, PDT, UTC and other basic stuff. Understanding Pacific Standard Time[PST], Pacific DaylightTime[PDT] and Universal Time Coordinated[UTC] was a pre-requisite for me to start playing with timezones.

Just a brief about PST and PDT from Wikipedia

  • The Pacific Time Zone (PT) is a time zone encompassing parts of western Canada, the western United States, and western Mexico. Places in this zone observe standard time by subtracting eight hours from Coordinated Universal Time (UTC−08:00). During daylight saving time, a time offset of UTC−07:00 is used
  • In the United States and Canada, this time zone is generically called the "Pacific Time Zone". Specifically, time in this zone is referred to as "Pacific Standard Time" (PST) when standard time is being observed (early November to mid-March)
  • "Pacific Daylight Time" (PDT) when daylight saving time (mid-March to early November) is being observed.

This is a general approach for converting between any two timezones using pytz which I am putting here.

Program:

import pytz
import datetime

given_date = datetime.datetime.now()

#this is the timezone where the date/time was recorded
source_ianatimezone = 'Asia/Kolkata'
tz_from = pytz.timezone(source_ianatimezone)

#this is the timezone to which I want to convert the above date/time
target_ianatimezone = 'America/Los_Angeles' 
to_tz = pytz.timezone(target_ianatimezone)

print("Converting given date: ",given_date," from: ",source_ianatimezone," to: ",target_ianatimezone)

#Localizing the given_date with the source timezone
localize_with_from_tz = tz_from.localize(given_date, is_dst=True)

#Localizing and converting the given_date to the target timezone
convert_with_to_tz = tz_from.localize(given_date, is_dst=True).astimezone(to_tz)

print("Localized value: ",localize_with_from_tz)
print("After converting: ",convert_with_to_tz)

This gives us the below output:

Converting given date:  2021-01-08 01:07:20.924953  from:  Asia/Kolkata  to:  America/Los_Angeles
Localized value:  2021-01-08 01:07:20.924953+05:30
After converting:  2021-01-07 11:37:20.924953-08:00

There are some other things which I found helpful when dealing with timezone:

  • Localized date/time value. For example:
    localize_with_from_tz = tz_from.localize(given_date, is_dst=True)
    print("Localized value in: ",localize_with_from_tz)
    
    gives us Localized value: 2021-01-08 01:07:20.924953+05:30 this clearly tells us which is timezone in which the date is present. So if you look at the above main program's output, you will see the localized value of the given date and as mentioned, since it is present in IST, we see an offset of +5:30 Its a very basic and a very well known thing but sometimes we miss out on such things!

Just to be clear:

  • Localized value: 2021-01-08 01:07:20.924953+05:30 this is interpreted as the date/time value 2021-01-08 01:07:20.924953 is 5:30 hours ahead of UTC
  • If its minus i.e., something like 2021-01-07 11:37:20.924953-08:00 this is interpreted as it is 8 hours behind UTC.
  • Don't want that offset to be shown ? Just add replace(tzinfo=None) (tz_info = timezone information) and you are good to go. For example:
    localize_with_from_tz = tz_from.localize(given_date, is_dst=True).replace(tzinfo=None)
    print("Localized value in: ",localize_with_from_tz)
    
    gives Localized value: 2021-01-08 01:07:20.924953 as the output.

Now about that is_dst=True , this is for the Daylight Saving Time(dst) something which you will see in America in form PST and PDT. Again a small thing but can cause chaos if missed out!

So is_dst=True is here to make life simplest! Put this and you need not bother about if the given date falls in PST or PDT, pytz does it for you!

This is just the very basic about timezone conversions in python. Though it seems simple and easy, I had a very hard time when I first worked with timezones!

Some useful links: