Python time.tzset() Function
Last Updated :
05 Aug, 2022
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:
- std and dst: It is time zone contractions that are given by three or more alphanumeric values. These will be scattered into tzname() function of time in python.
- offset: In tzset(), the offset is of the form: ± hh[:mm[:ss]]. This indicates the value-added of the local time to arrive at UTC. The timezone is east of the Prime Meridian if preceded by a ‘-‘ sign. Else, it is west. Summertime is assumed to be one hour before the standard time if no offset follows DST.
- start[/time], end[/time]: Shows when to switch to and back from DST. The start and end dates follow one of the following formats:
- Jn: The Julian day n where n is in the range from 1 to 365 (1 <= n <= 365). In this, we do not count leap days, so in all years February 28 is day 59 and March 1 is day 60.
- n: The zero-based Julian day (0 <= n <= 365) which is in the range of 0 to 365. In this, we count leap days, and it is possible to point to February 29.
- Mm.n.d: The d’th day (0 <= d <= 6) of week n of month m of the year (1 <= n <= 5, 1 <= m <= 12, where week 5 means “the last d day in month m” which may occur in either the fourth or the fifth week). Week 1 is the first week in which the d’th day occurs. Day zero is a Sunday.
- time: This follows the same format as offset except that the leading sign (‘-‘ or ‘+’) is not allowed in this. This takes default time as 02:00:00 if it is not given.
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:
Python3
import time
import os
os.environ[ 'TZ' ] = 'EST+05EDT,M4.1.0,M10.5.0'
time.tzset()
print (time.strftime( '%X %x %Z' ))
os.environ[ 'TZ' ] = 'AEST-10AEDT-11,M10.5.0,M3.5.0'
time.tzset()
print (time.strftime( '%X %x %Z' ))
|
Output
08:47:24 11/19/21 EST
00:47:24 11/20/21 AEDT
Example 2:
Python3
import time
import os
os.environ[ 'TZ' ] = 'UTC'
time.tzset()
print (time.strftime( '%X %x %Z' ))
os.environ[ 'TZ' ] = 'Europe/Amsterdam'
time.tzset()
print (time.strftime( '%X %x %Z' ))
|
Output
12:14:00 11/23/21 UTC
13:14:00 11/23/21 CET
Example 3:
Python3
import time
import os
os.environ[ 'TZ' ] = 'Australia/Melbourne'
time.tzset()
print (time.strftime( '%X %x %Z' ))
os.environ[ 'TZ' ] = 'Egypt'
time.tzset()
print (time.strftime( '%X %x %Z' ))
|
Output
23:14:00 11/23/21 AEDT
14:14:00 11/23/21 EET
Share your thoughts in the comments
Please Login to comment...