Python time tzset() method

Python Time tzset() Method

Description

The tzset() method resets the time conversion rules used by the library functions. The environment variable TZ specifies how to reset.

The standard format of the TZ environment variable is as follows (spaces added for clarity): −

std offset [dst [offset [,start[/time], end[/time]]]]
  • std and dst − Three or more alphanumeric characters representing the time zone abbreviation. These are propagated to time.tzname.
  • offset − The offset is in the format: .hh[:mm[:ss]]. This represents the value to add the local time to UTC. If it begins with a ‘-‘, the time zone is east of the prime meridian; otherwise, it is west. If there is no offset after dst, daylight saving time is assumed to be one hour ahead of standard time.

  • start[/time], end[/time] − Indicates when to switch to and from daylight saving time. The start and end dates are in one of the following formats −

    • Jn − The nth Julian day (1 <= n <= 365). Leap days are not counted, so in all years, February 28 is the 59th day and March 1 is the 60th day.
    • n − The zero-based Julian day (0 <= n <= 365). Leap days are counted, possibly up to February 29.

    • Mm.n.d − Day of the dth day of the nth week of the mth month of the year (0 <= d <= 6) (1 <= n <= 5, 1 <= m <= 12, where week 5 means "the last dth day of the mth month" and can occur in the fourth or fifth week). Week 1 is the first week in which day d occurs. Day 0 is Sunday.

    • time − Time in the same format as offset, but without leading signs (‘-‘ or ‘+’). If time is not specified, the default is 02:00:00.

Syntax

The syntax of the tzset() method is as follows −

time.tzset()

Parameters

None

Return Value

This method does not return any value.

Example

The following example demonstrates the use of the tzset() method.

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')

When we run the above program, it produces the following output.

14:53:54 04/19/23 EDT
04:56:34 04/20/23 AEST

Leave a Reply

Your email address will not be published. Required fields are marked *