How to set timezone in python using os.environ

12,214

From the python manual:

The standard format of the TZ environment variable is (whitespace added for clarity):

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

Mm.n.d The d‘th day (0 <= d <= 6) or 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 Sunday.

Share:
12,214
kamal
Author by

kamal

Updated on June 04, 2022

Comments

  • kamal
    kamal almost 2 years

    example:

    os.environ['TZ'] = "CST+06CDT,M4.1.0,M10.5.0"
    

    I can understand CST, 06 (offset from UTC ?? or GMT) , CDT == Central Daylight Saving Time but

    What do M4.1.0 and M10.5.0 mean ?

    Here is what i found:

    export TZ=EST05EDT

    EST was taken from the chart below. Five hours has to be added to get UTC, hence the 05. And currently EDT, Eastern Daylight, is in effect.

    TZ=.

    DST timezones

      0      BST for British Summer.                                
      +400   ADT for Atlantic Daylight.                             
      +500   EDT for Eastern Daylight.                              
      +600   CDT for Central Daylight.                              
      +700   MDT for Mountain Daylight.                             
      +800   PDT for Pacific Daylight.                              
      +900   YDT for Yukon Daylight.                                
      +1000  HDT for Hawaii Daylight.                               
      -100   MEST for Middle European Summer,                       
                 MESZ for Middle European Summer,                  
                 SST for Swedish Summer and FST for French Summer. 
      -700   WADT for West Australian Daylight.                     
      -1000  EADT for Eastern Australian Daylight.                  
      -1200  NZDT for New Zealand Daylight.                         
    

    Was there an easier way to get back to my correct time? Yes, just unset the TZ environment variable.

      $ unset TZ
      $ date
      Sun Aug 22 10:17:35 EDT 2004
    

    Note, even though TZ was adjusted for daylight saving time, will you get the correct time 5 months from now? When does daylight saving time go into effect? The TZ value shown below adjust for dst, only during the correct dates. For instance, this entry goes into effect April, the first week, at 2am, and ends October the 5th week, at 2am. Note 10.5.0 stands for the 5th week in October, and not the 5th day.

      $ export TZ=EST+5EDT,M4.1.0/2,M10.5.0/2
    
  • kamal
    kamal about 13 years
    i did look that up in the python docs, but what does start time and end time relate to M4.1.0,M10.5.0
  • kamal
    kamal about 13 years
    i was looking for ... something like , but thanks export TZ=EST05EDT Note, even though TZ was adjusted for daylight saving time, will you get the correct time 5 months from now? When does daylight saving time go into effect? The TZ value shown below adjust for dst, only during the correct dates. For instance, this entry goes into effect April, the first week, at 2am, and ends October the 5th week, at 2am. Note 10.5.0 stands for the 5th week in October, and not the 5th day. $ export TZ=EST+5EDT,M4.1.0/2,M10.5.0/2