How to handle Time zones in Python

How to handle Time zones in Python

In this tutorial, we will define how we can work with the different timezones in Python. We will also learn about the local time zone of Python. Timezone can be described as the geographical area where standard time is observed. Let’s have a brief introduction of the timezone. We will use Python’s pytz library to work with the timezone.

What is the Timezone?

Timezone is defined as the standard time depending on the geographical representation of that place. In other words, timezone refers to the local time of a particular region based on the earth’s rotation. It is defined in UTC (Coordinated Universal Time), the standard against which the world’s region-based time is coordinated.

Note – It is recommended to use the UTC as your base timezone.

For Example – UTC in Singapore is 2.5 hours ahead of India or timezone in the North and is either 5 or 6 hours behind and represented as UTC-5 or UTC-6 based on the day light. Let’s see the following table of different timezones across the world.

Python pytz Library

Python pytz the Olson tz database into Python and supports almost all timezones. This module allows us to with the date-time conversion functionalities and serve international base of the client. We can also perform the calculations as per project requirement and we can also create the timezone aware datetime instances. It can also solve the problem of ambiguous times at the end of the day light.

Installation

We need to install it in our system to work with the timezone. We can install it using the below command.

The above command will install the pytz on your machine. It is recommended to install in the virtual environment.

Python comes with the DateTime.tzinfo() abstract base class. This class allows us to handle the timezone. But it is not good practice to instantiate directly. To get the information about the particular time zone, we need to create the subclass of the DateTime.tzinfo().

The pytz library overcomes this demerits, it has implanted a timzone class for handling arbitrary fixed offset from timezones and UTC.

For Example – To get the current time in Python, we use the datetime.now() functions. However, this method doesn’t return the any timzone. Using timezone, we can pass the timezone in this function and it will return current datetime in the given timezone.

The pytz Attributes

The pytz provides the three attributes and methods to work with the timezone in Python.

  • pytz.utc
  • pytz.timezone(‘region’)
  • pytz.astimezone()

How to get the current time?

To get the current time, we use the time module. We can use the following functions of the time module.

  • localtime()– It allows to get the current local time.
  • strftime(“%H:%M:%S”, t)– it allows to define the format of the time to be used to display the time.

Example –

Output:

The current timezone is: 21:22:41

We get the current time zone of the region and standard UTC.

Create Timezone Naïve Datetime Object

A data object can be defined with or without timezones in Python. This object is also known as Aware or Naïve. If it holds the timezone value, it is an aware

A data object can be defined with or without timezones in Python. This object is also known as Aware or Naïve. If it holds the timezone value, it is an aware Datetime object; in others, it is a naïve object by default.

Let’s understand the following example of creating a timezone object.

Example –

Output:

Timezone naive: 2022-05-08 11:38:01.362134
Timezone Aware: 2022-05-08 06:08:01.363137+00:00
US Central DateTime 2022-05-08 01:08:11.323668-05:00

Explanation –

In the above code, we have used the pytz.utc as the parameter to the datetime.now() function. The offset at the end is +0.00 which is the standard UTC offset. In another line, we used the ‘US/Central’ region to create the timezone. The offset at the end -05:00 is the UTC offset of the CDT region.

The pytz library has the large lists of the timezone, we can print the timezones using the below code.

Output:

['Africa/Abidjan', 'Africa/Accra', 'Africa/Addis_Ababa', 'Africa/Algiers', 'Africa/Asmara', 'Africa/Asmera', 'Africa/Bamako', 'Africa/Bangui', 'Africa/Banjul', 'Africa/Bissau', 'Africa/Blantyre', 'Africa/Brazzaville', 'Africa/Bujumbura', 'Africa/Cairo', 'Africa/Casablanca', 'Africa/Ceuta', 'Africa/Conakry', 'Africa/Dakar', 'Africa/Dar_es_Salaam', 'Africa/Djibouti', 'Africa/Douala', 'Africa/El_Aaiun', 'Africa/Freetown', 'Africa/Gaborone', 'Africa/Harare', 'Africa/Johannesburg', 'Africa/Juba', 'Africa/Kampala', 'Africa/Khartoum', 'Africa/Kigali', 'Africa/Kinshasa', 
'Canada/Pacific', 'Canada/Saskatchewan', 'Canada/Yukon', 'Chile/Continental', 'Chile/EasterIsland', 'Cuba', 'EET', 'EST', 'EST5EDT', 'Egypt', 'Eire', 'Etc/GMT', 'Etc/GMT+0', 'Etc/GMT+1', 'Etc/GMT+10', 'Etc/GMT+11', 'Etc/GMT+12', 'Etc/GMT+2', 'Etc/GMT+3', 'Etc/GMT+4', 'Etc/GMT+5', 'Etc/GMT+6', 'Etc/GMT+7', 'Etc/GMT+8', 'Etc/GMT+9', 'Etc/GMT-0', 'Etc/GMT-1', 'Etc/GMT-10', 'Etc/GMT-11', 'Etc/GMT-12', 'Etc/GMT-13', 'Etc/GMT-14', 'Etc/GMT-2', 'Etc/GMT-3', 'Etc/GMT-4', 'Etc/GMT-5', 'Etc/GMT-6', 'Etc/GMT-7', 'Etc/GMT-8', 'Etc/GMT-9', 'Etc/GMT0', 'Etc/Greenwich', 'Etc/UCT', 'Etc/UTC', 'Etc/Universal', 'Etc/Zulu', 'Europe/Amsterdam', 'Europe/Andorra', 'Europe/Astrakhan', 'Europe/Athens', 'Europe/Belfast', 'Europe/Belgrade', 'Europe/Berlin', 'Europe/Bratislava', 'Europe/Brussels', 'Europe/Bucharest', 'Europe/Budapest', 'Europe/Busingen', 'Europe/Chisinau', 'Europe/Copenhagen', 'Europe/Dublin', 'Europe/Gibraltar', 'Europe/Guernsey', 'Europe/Helsinki', 'Europe/Isle_of_Man', 'Europe/Istanbul', 'Europe/Jersey', 'Europe/Kaliningrad', 'Europe/Kiev', 'Europe/Kirov', 'Europe/Lisbon', 'Europe/Ljubljana', 'Europe/London', 'Europe/Luxembourg', 'Europe/Madrid', 'Europe/Malta', 'Europe/Mariehamn', 'Europe/Minsk', 'Europe/Monaco', 'Europe/Moscow', 'Europe/Nicosia', 'Europe/Oslo', 'Europe/Paris', 'Europe/Podgorica', 'Europe/Prague', 'Europe/Riga', 'Europe/Rome', 'Europe/Samara', 'Europe/San_Marino', 'Europe/Sarajevo', 'Europe/Saratov', 'Europe/Simferopol', 'Europe/Skopje', 'Europe/Sofia', 'Europe/Stockholm', 'Europe/Tallinn', 'Europe/Tirane', 'Europe/Tiraspol', 'Europe/Ulyanovsk', 'Europe/Uzhgorod', 'Europe/Vaduz', 'Europe/Vatican', 'Europe/Vienna', 'Europe/Vilnius', 'Europe/Volgograd', 'Europe/Warsaw', 'Europe/Zagreb', 'Europe/Zaporozhye', 'Europe/Zurich', 'GB', 'GB-Eire', 'GMT', 'GMT+0', 'GMT-0', 'GMT0', 'Greenwich', 'HST', 'Hongkong', 'Iceland', 'Indian/Antananarivo', 'Indian/Chagos', 'Indian/Christmas', 'Indian/Cocos', 'Indian/Comoro', 'Indian/Kerguelen', 'Indian/Mahe', 'Indian/Maldives', 'Indian/Mauritius', 'Indian/Mayotte', 'Indian/Reunion', 'Iran', 'Israel', 'Jamaica', 'Japan', 'Kwajalein', 'Libya', 'MET', 'MST', 'MST7MDT', 'Mexico/BajaNorte', 'Mexico/BajaSur', 'Mexico/General', 'NZ', 'NZ-CHAT', 'Navajo', 'PRC',..............
'Pacific/Efate', 'Pacific/Enderbury', 'Pacific/Fakaofo', 'Pacific/Fiji', 'Pacific/Funafuti', 'Pacific/Galapagos', 'Pacific/Gambier', 'Pacific/Guadalcanal', 'Pacific/Guam', 'Pacific/Honolulu', 'Pacific/Johnston', 'Pacific/Kiritimati', 'Pacific/Kosrae', 'Pacific/Kwajalein', 'Pacific/Majuro', 'Pacific/Marquesas', 'Pacific/Midway', 'Pacific/Nauru', 'Pacific/Niue', 'Pacific/Norfolk', 'Pacific/Noumea', 'Pacific/Pago_Pago', 'Pacific/Palau', 'Pacific/Pitcairn', 'Pacific/Pohnpei', 'Pacific/Ponape', 'Pacific/Port_Moresby', 'Pacific/Rarotonga', 'Pacific/Saipan', 'Pacific/Samoa', 'Pacific/Tahiti', 'Pacific/Tarawa', 'Pacific/Tongatapu', 'Pacific/Truk', 'Pacific/Wake', 'Pacific/Wallis', 'Pacific/Yap', 'Poland', 'Portugal', 'ROC', 'ROK', 'Singapore', 'Turkey', 'UCT', 'US/Alaska', 'US/Aleutian', 'US/Arizona', 'US/Central', 'US/East-Indiana', 
'US/Eastern', 'US/Hawaii', 'US/Indiana-Starke', 'US/Michigan', 'US/Mountain', 'US/Pacific', 'US/Samoa', 'UTC', 'Universal', 'W-SU', 'WET', 'Zulu']

Get Current Time in Different Timezone

The pytz module allows us to get the current date and time of any timezone using the below method.

Syntax –

First, we create the timezone object using the pytz.timezone(‘region name’) function, the pass this object to datetime.now(timezone_obj) to get the current datetime of the given timezone.

Using the above list of timezone, we will get the current the timezone of various timzones.

Example – 1:

Output:

US Buenos DateTime: 2022:05:08 03:52:13 -03 -0300
US Adak timezone DateTime: 2022:05:07 21:52:13 HDT -0900
US Eastern timezone DateTime: 2022:05:07 22:52:13 AKDT -0800
US Michigan timezone DateTime: 2022:05:08 02:52:13 EDT -0400
US Belam timezone DateTime: 2022:05:08 03:52:13 -03 -0300

Explanation –

In the above code, we have gotten the current time of the different regions of America.

Example – 2: Get the Timezone of a Different Country

Output:

Israel DateTime: 2022:05:08 10:16:46 IDT +0300
Rome DateTime: 2022:05:08 09:16:46 CEST +0200
Amsterdam DateTime: 2022:05:08 09:16:46 CEST +0200
Hongkong DateTime: 2022:05:08 15:16:46 HKT +0800
Jamaica DateTime: 2022:05:08 02:16:46 EST -0500
Turkey: DateTime: 2022:05:08 10:16:46 +03 +0300

When we run the code, it returns current timezone of the various countries.

Get Timezone Information Using tzinfo

Using the DateTime.tzinfo class, we can get information about the date or time. The tzinfo generally has the following information.

  • The timezone of the datetime.
  • The Daylight saving
  • Offset from the UTC.

The tzinfo class is abstract class which class provides the following methods to get the timezone information.

  • utcoffset(dt) – It returns the total offset from the UTC, which should be a timedelta object. If the time delta is east of UTC, the time delta value will be positive. If the time delta is west of UTC, the time delta value will be negative. The local timezone has both timezone and DST values. Therefore, the range of the time delta is between timedelta hours=24 to timedelta(hours=24).
  • tzname(dt) – This method returns the time zone name corresponding to datetime object.
  • utcoffset(dt) – This method is used to get the DST offset in the zones where DST is in effect. If the DST is not in effect, it will return only timedelta(0).The DTC information is already part of the UTC offset.

Let’s understand the following example.

Example – 1:

Output:

Europe London DateTime: 2022:05:08 09:12:09 BST +0100
BST
1:00:00
1:00:00

Converting Between Timezones

We can convert the datetime from one timezone to another timezone using the datetime.astimezone() method. This method takes the datetime object as a parameter and returns a new datetime of a given timezone. Let’s understand the following example.

Example –

Output:

UTC DateTime: 2022:05:08 08:23:21 UTC +0000
US Hawaii DateTime: 2022:05:07 22:23:21 HST -1000
US Mountain DateTime: 2022:05:07 22:23:21 HST -1000
India DateTime: 2022:05:08 13:53:21 IST +0530

Working with Local Timezones

In the above section, we have mentioned that the naïve datetime to an aware datetime instance. We can pass the timezone value set to local standardized value.

The pytz module provides the localize() method that is used to convert a naïve to local time. It takes the arguments, namely the datetime object to localize and an optional is_dst flag.

The tzinfo has a method dst() which will return the Daylight Saving Time (DST) information if the flag is set to true.

Let’s understand the following example.

Example –

Output:

Indian Standard Time:: 2022-05-08 14:28:37 IST+0530
Amsterdam with daylight saving time:: 1997-12-03 02:00:00 CET+0100
Daylight saving time in amsterdam on 3/8/83:: 0:00:00

Example – 2: Converting UTC format to IST format

Output:

UTC Format                       			IST Format
2022-05-08 09:05:46 UTC +0000     2022-05-08 14:35:46 IST +0530
2022-05-08 09:05:46 UTC +0000     2022-05-08 14:35:46 IST +0530
2022-05-08 09:05:46 UTC +0000     2022-05-08 14:35:46 IST +0530
2022-05-08 09:05:46 UTC +0000     2022-05-08 14:35:46 IST +0530
2022-05-08 09:05:46 UTC +0000     2022-05-08 14:35:46 IST +0530

Conclusion

This tutorial has covered the essential methods of the pytz library. Timezones play essential roles in software development/web development, and we have discussed how we can get the current time and timezone. This tutorial will help you get familiar with the pytz library and timezones.


原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263143.html

(0)
上一篇 2022年5月30日
下一篇 2022年5月30日

相关推荐

发表回复

登录后才能评论