A time zone represents the standardized time depending on which part of the world is being considered. Show In simple terms, timezone refers to the local time of a region. UTC (Coordinated Universal Time) is the astronomical time based on earth’s rotation, is the standard against which the world’s region-based time is coordinated. Note: UTC – Coordinated Universal Time is the common time standard across the world. So, in Python, to work with the timezone without any issues, it is recommended to use the UTC as your base timezone For example, CT(Central Time) in North and South America is either 5 or 6 hours behind and represented as UTC-5 or UTC-6 based on the Day Light Saving. Below are a few examples. UTC OffsetLocationsNameLocationUTC +9Japan, South Korea, and 5 moreJSTTokyoUTC +5:30IndiaISTIndiaUTC +1The United Kingdom and 20 moreBSTLondonUTC -10Hawaii/USA and 2 moreHSTHonoluluTimeZonesPython provides the Timezone naive: 2021-07-09 13:22:02.2569787 abstract base class which provides methods to handle timezone. But this class is an abstract base class and should not be instantiated directly. We need to define a subclass of tzinfo to capture information about a particular time zone. The pytz library has implemented a timezone class for handling arbitrary fixed offsets from UTC and timezones. This library allows accurate and cross-platform timezone calculations and also solves the issue of ambiguous times at the end of daylight saving time. pytz is a concrete implementation of the abstract base class tzinfo and is used to create timezone-aware datetime objects. For example, The Timezone naive: 2021-07-09 13:22:02.2569788 function returns the current local date-time without any timezone information. Using the pytz library, we can pass the timezone name to this function to get the current datetime in the given timezone. We’ll use the following attributes and methods of the pytz module to work with timezone in Python.
Create Timezone Aware Datetime ObjectIn Python, a date object can be mentioned with or without timezones. Based on that, an object is known as Naive or Aware. A date object, by default, is naive. A datetime or time object is aware if it holds the timezone(tz) value. Follow the below steps to create a timezone aware Datetime Object in Python: –
Example:
Output: Timezone naive: 2021-07-09 13:22:02.256978
Refer to list all timezones in Python if you don’t know the exact name of the timezone to create a date and time in the right timezone. To make the old/existing datetime timezone aware, use the following code.
Note: The 8 method return the new 9 instance.Format UTC DateTime to Get the timezone name Extract the timezone name from UTC DateTime using the DateTime formatting in Python. Use the 0 directive to get the timezone name.
Note: IST is the timezone name Create TimeZone Aware Datetime Object Using timezone classLet’s see how create a timezone aware 9 object without pytz.The datetime modules have the 2 class, which in turn is the subclass of the abstract base class 3. Each instance created of the timezone class represents the offset of the timezone from the Coordinated Universal Time (UTC).We can create an UTC-aware 9 object by assigning the 5Syntax:
Here 6 represents the difference between the local time and the UTC (Coordinated Universal Time). It can be a time delta object ranging from hours=-24 to +24.Example:
Note: we are setting the UTC offset using the timedelta class 7Get Current Time in Different TimezoneUsing the pytz module we can get the current date and time of any timezone. Syntax:
Steps:
Note: UTC – Coordinated Universal Time is the common time standard across the world. So, to work with the timezone without any issues, it is recommended to use the UTC as your base timezone. In this example, we’ll see how to get the current datetime in the following timezones USA: Get current Date and Time in the following TimeZones of United States
Output: US Central DateTime: 2021:07:08 08:37:34 CDT -0500 US Pacific timezone DateTime: 2021:07:08 06:37:34 PDT -0700 US Eastern timezone DateTime: 2021:07:08 09:37:34 EDT -0400 US Mountain timezone DateTime: 2021:07:08 07:37:34 MDT -0600 Other TimeZones
Output: Timezone naive: 2021-07-09 13:22:02.2569780 Also, see: Convert between timezones Get TimeZone Information Using tzinfoThe Timezone naive: 2021-07-09 13:22:02.2569787 is an abstract base class containing information about the date or time object passed to them. The tzinfo generally contains the following information: –
The tzinfo class provides the following method to get the timezone information: –
Example: Timezone naive: 2021-07-09 13:22:02.2569781 Our code produced the following information: Timezone naive: 2021-07-09 13:22:02.2569782 The datetime modules have the 2 class, which in turn is the subclass of the abstract base class 3Converting Between TimezonesUse the 0 method to convert the datetime from one timezone to another. This method uses an instance of the datetime object and returns a new datetime of a given timezone.Timezone naive: 2021-07-09 13:22:02.2569783 Output: Timezone naive: 2021-07-09 13:22:02.2569784 Working with Local TimezonesNote: To work with the timezone without any issues, it is recommended to use the UTC as your base timezone not a local time. As already mentioned, we can convert a naive datetime to an aware datetime instance with a timezone value set to a local standardized value. We can do it with one of the 1 methods called 2() .This method is used to convert a naive to local time. It accepts the two arguments, namely the datetime object to localize and an optional 3 flag.This flag is set to true if we want to localize and the daylight saving information and false if we want only the standard offset time and false otherwise. As mentioned above the 3 has a method called 5 which will return the Daylight Saving Time(DST) information if the flag is set to true. |