Open In App

Python time.tzset() Function

In Python, the tzset() function of the time module is based on the re-initialization settings using the environment variable TZ. tzset() method of time module in python resets the time transformation protocol.  this timezone means non-DST seconds west of UTC time and altzone means DST seconds west of UTC time. The TZ environment variable’s conventional form is: 

std offset [dst [offset [,start[/time], end[/time]]]]

Where the components are:



Syntax:

time.tzset()

Parameters:



NA

Return Value:

Does not return any value.

Note: Although in many cases, if we change the TZ environment variable it may affect the output of functions like localtime() without calling tzset(), this behavior should not be relied on. The TZ environment variable should contain no whitespace. 

Example 1:




# time.tzset() Function in python
 
# importing time and os module
import time
import os
 
# Define TZ environment variable
os.environ['TZ'] = 'EST+05EDT,M4.1.0,M10.5.0'
 
# reset the time conversion rules
time.tzset()
 
# print time
print(time.strftime('%X %x %Z'))
 
# Define TZ environment variable again
os.environ['TZ'] = 'AEST-10AEDT-11,M10.5.0,M3.5.0'
 
# reset the time conversion rules
time.tzset()
 
# print time
print(time.strftime('%X %x %Z'))

Output
08:47:24 11/19/21 EST
00:47:24 11/20/21 AEDT

Example 2:




# time.tzset() Function in python
 
# importing time and os module
import time
import os
 
# Define TZ environment variable
os.environ['TZ'] = 'UTC'
 
# reset the time conversion rules
time.tzset()
 
# print time
print(time.strftime('%X %x %Z'))
 
# Define TZ environment variable again
os.environ['TZ'] = 'Europe/Amsterdam'
 
# reset the time conversion rules
time.tzset()
 
# print time
print(time.strftime('%X %x %Z'))

Output
12:14:00 11/23/21 UTC
13:14:00 11/23/21 CET

Example 3:




# time.tzset() Function in python
 
# importing time and os module
import time
import os
 
# Define TZ environment variable
os.environ['TZ'] = 'Australia/Melbourne'
 
# reset the time conversion rules
time.tzset()
 
# print time
print(time.strftime('%X %x %Z'))
 
 
# Define TZ environment variable again
os.environ['TZ'] = 'Egypt'
 
# reset the time conversion rules
time.tzset()
 
# print time
print(time.strftime('%X %x %Z'))

Output
23:14:00 11/23/21 AEDT
14:14:00 11/23/21 EET

Article Tags :