Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to compare time in different time zones in Python?
In this article, we will show you how to compare time in different timezones in Python using the below methods ?
Comparing the given Timezone with the local TimeZone
Comparing the Current Datetime of Two Timezones
Comparing Two Times with different Timezone
Method 1: Comparing Local Timezone with UTC
This method compares a local timezone (CET) with UTC timezone to determine the time difference ?
# importing datetime, pytz modules
from datetime import datetime
import pytz
# Getting the local timezone
localTimeZone = pytz.timezone('CET')
# Getting the UTC timeZone
utcTimeZone = datetime.now(pytz.utc)
# format string
time_format = '%Y:%m:%d %H:%M:%S %Z %z'
# Convert the time to the local timezone
local = utcTimeZone.astimezone(localTimeZone)
# Getting formatted time using strftime() function
print("Formatted DateTime in Local Timezone : ", local.strftime(time_format))
print("Formatted DateTime in UTC Timezone : ", utcTimeZone.strftime(time_format))
difference = int(local.strftime('%z'))
difference2 = int(utcTimeZone.strftime('%z'))
# Comparing Time Zones
if (difference > difference2):
print('UTC TimeZone is behind the Local Timezone by', local.strftime('%z'), 'Hours')
if (difference < difference2):
print('UTC TimeZone is ahead of the Local TimeZone by', utcTimeZone.strftime('%z'), 'Hours')
Formatted DateTime in Local Timezone : 2022:09:14 12:05:05 CEST +0200 Formatted DateTime in UTC Timezone : 2022:09:14 10:05:05 UTC +0000 UTC TimeZone is behind the Local Timezone by +0200 Hours
Method 2: Comparing Current Datetime of Two Timezones
Time zones can make things more complicated, but we can use the same logic for comparison. The only difference is that we're working with aware dates, which include additional information about the time zone ?
Understanding localize() Function
localize() function When creating datetime-aware objects with an initial fixed datetime value, the correct function to use is localize(). The original datetime value will be retained in the resulting datetime aware object.
Example
# importing datetime, pytz modules
from datetime import datetime
import pytz
# Getting the time in America/New_York timezone
timezone_newyork = pytz.timezone('America/New_York')
# Getting the time in Europe/London timezone
timezone_london = pytz.timezone("Europe/London")
# current datetime
inputDatetime = datetime.now()
# Localize the given date, according to the timezone objects
# This step converts the given two timezones to local time zones using localize() function
datewith_tz_newyork = timezone_newyork.localize(inputDatetime)
datewith_tz_london = timezone_london.localize(inputDatetime)
# These are now, effectively no longer the same date after being localized
print("The date and time with timezone newyork:", datewith_tz_newyork)
print("The date and time with timezone london:", datewith_tz_london)
difference = int(datewith_tz_newyork.strftime('%z'))
difference2 = int(datewith_tz_london.strftime('%z'))
# checking whether the date with different timezones are equal or NOT after they are in the same timezone
if (datewith_tz_newyork > datewith_tz_london):
print('Current Time in Newyork time is older than London by', (difference2 - difference) / 100, 'hours')
else:
print('Current Time in London time is older than Newyork by', (difference - difference2) / 100, 'hours')
The date and time with timezone newyork: 2022-09-14 10:13:27.727111-04:00 The date and time with timezone london: 2022-09-14 10:13:27.727111+01:00 Current Time in Newyork time is older than London by 5.0 hours
Method 3: Comparing Specific Times in Different Timezones
This method is similar to the previous one, except we provide specific DateTime values for both timezones ?
from datetime import datetime
import pytz
# Getting the time in America/New_York timezone
timezone_newyork = pytz.timezone('America/New_York')
# Getting the time in Europe/London timezone
timezone_london = pytz.timezone("Europe/London")
# input the date time of the newyork in the format Year, Month, Day, Hour, Minute, Second
newyorkDateTime = datetime(2013, 3, 15, 20, 5, 10)
# input date time of the london
londonDateTime = datetime(2013, 3, 15, 20, 5, 10)
# Localize the given date, according to the timezone objects
datewith_tz_newyork = timezone_newyork.localize(newyorkDateTime)
datewith_tz_london = timezone_london.localize(londonDateTime)
# These are now, effectively no longer the same date after being localized
print("The date and time with timezone newyork:", datewith_tz_newyork)
print("The date and time with timezone london:", datewith_tz_london)
difference = int(datewith_tz_newyork.strftime('%z'))
difference2 = int(datewith_tz_london.strftime('%z'))
# comparing the date with different timezones after they are in the same timezone
if (difference > difference2):
print('Given Two Times of two different Time Zones differ by', (difference - difference2) / 100, 'hours')
else:
print('Given Two Times of two different Time Zones differ by', (difference2 - difference) / 100, 'hours')
The date and time with timezone newyork: 2013-03-15 20:05:10-04:00 The date and time with timezone london: 2013-03-15 20:05:10+00:00 Given Two Times of two different Time Zones differ by 4.0 hours
Key Concepts
pytz.timezone() − Creates timezone objects for specific regions
localize() − Attaches timezone information to naive datetime objects
astimezone() − Converts datetime from one timezone to another
strftime('%z') − Extracts timezone offset information
Conclusion
We learned how to compare time in different timezones using three different methods. Use localize() for timezone-aware objects and astimezone() for timezone conversions.
