There are some standard libraries we can use for timezones, here we’ll use pytz. This library has a timezone class for handling arbitrary fixed offsets from UTC and timezones. Show Installationpytz is a third-party package that you have to install. To install pytz use the following command –
Getting StartedAfter installation import the pytz package and now let’s see how we can pass in the timezones. Basic syntax in Python to get the date and time information is :
The above syntax returns the current local date-time without any timezone information. But with the use of the pytz package we can handle this date-time information in the various timezones –now() gives us the option to pass in a time zone, so if you leave the time zone empty then it will also return the current local date-time. The output of now() depends upon the machine. The local time and time zone settings of the host machine will determine the output. So in order to work with the timezone smoothly, it is recommended to use the UTC as your base timezone. To get the Universal Time Coordinated i.e. UTC time we just pass in the parameter to now() function. To get the UTC time we can directly use the ‘pytz.utc’ as a parameter to now() function as ‘now(pytz.utc)’. The offset will be shown at the end as (+ or – hours). The below code shows the local time of the machine and the UTC time with offset. Example: Python3
Output:
As you can see now we got the date time info of the local machine and in UTC, the timezone offset at the end is +00:00. If we want to get date time info of specific timezone we just need to add the timezone in the parameter – Syntax:
The parameter pytz.timezone allows us to specify the timezone information as a string. We can pass in any available timezone and will get the current date time of that timezone, and it will also print the offset with respect to the UTC. i.e., the difference between UTC timezone(+00:00) and the specified time zone. Example: Python3
Output:
Here we got the different date time according to different timezones with offset. The offset will also show the (+/-) hours. We can also convert any time zone to a different time zone. Converting between Timezonesastimezone() method is used to manipulate i.e., to convert the datetime objects to the new specified datetime object. It uses an instance of the datetime object and returns new timezone information. Timezone-aware objects are Python DateTime or time objects that include timezone information. An aware object represents a specific moment in time that is not open to interpretation. Checking if an object is timezone aware or not:We can easily check if a datetime object is timezone-aware or not. For this, we will store the current date and time in a new variable using the datetime.now() function of datetime module.
Then we will check the timezone information of the object stored in the tzinfo base class. tzinfo is an abstract base class for time zone information objects. Python3
2021-08-30T09:45:43.291212+00:000 2021-08-30T09:45:43.291212+00:001 2021-08-30T09:45:43.291212+00:002 2021-08-30T09:45:43.291212+00:003
2021-08-30T09:45:43.291212+00:004 2021-08-30T09:45:43.291212+00:005 2021-08-30T09:45:43.291212+00:006 2021-08-30T09:45:43.291212+00:007 2021-08-30T09:45:43.291212+00:002 2021-08-30T09:45:43.291212+00:002 Aware 2021-08-30T09:55:15.111556+00:000 Aware 2021-08-30T09:55:15.111556+00:001 Aware 2021-08-30T09:55:15.111556+00:002 Aware 2021-08-30T09:55:15.111556+00:003 Aware 2021-08-30T09:55:15.111556+00:004 2021-08-30T09:45:43.291212+00:002 2021-08-30T09:45:43.291212+00:002 Aware 2021-08-30T09:55:15.111556+00:000 Aware 2021-08-30T09:55:15.111556+00:008 Aware 2021-08-30T09:55:15.111556+00:009 2021-08-30 04:35:37.036990+00:000 2021-08-30 04:35:37.036990+00:001 2021-08-30 04:35:37.036990+00:000 2021-08-30 04:35:37.036990+00:003 2021-08-30 04:35:37.036990+00:000 2021-08-30 04:35:37.036990+00:005 2021-08-30 04:35:37.036990+00:006 2021-08-30 04:35:37.036990+00:007 2021-08-30 04:35:37.036990+00:008 2021-08-30 04:35:37.036990+00:009 Aware 2021-08-30T09:55:15.111556+00:008 Aware 2021-08-30T09:55:15.111556+00:009 2021-08-30 04:35:37.036990+00:000 Aware 2021-08-30 04:46:40.670455+00:003 2021-08-30 04:35:37.036990+00:000 2021-08-30 04:35:37.036990+00:005 2021-08-30 04:35:37.036990+00:006 Aware 2021-08-30 04:46:40.670455+00:007 2021-08-30 04:35:37.036990+00:008 Output: Unaware Timezone aware object using datetimeFor this, we will store the current time in a new variable using the datetime.now().time() function of datetime module. Then we will replace the value of the timezone in the tzinfo class of the object using the replace() function. After that convert the date value into ISO 8601 format using the isoformat() method.
Code: Python3
2021-08-30T09:45:43.291212+00:000 2021-08-30T09:45:43.291212+00:001 2021-08-30T09:45:43.291212+00:002 2021-08-30T09:45:43.291212+00:003
2021-08-30T09:45:43.291212+00:001 2021-08-30T09:45:43.291212+00:002 import 22021-08-30 04:35:37.036990+00:000 import 42021-08-30T09:45:43.291212+00:002 import 6
2021-08-30T09:45:43.291212+00:001 2021-08-30T09:45:43.291212+00:002 datetime 1
2021-08-30 04:35:37.036990+00:005 datetime 4Output: 2021-08-30T09:45:43.291212+00:00 Now let’s check if the object is timezone aware or not using the method we used in the 1st section of the article. |