如何使用os.environ在python中设置时区

2024-05-15 08:43:23 发布

您现在位置:Python中文网/ 问答频道 /正文

示例:

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

我能理解CST,06(与UTC的偏移量??或GMT),CDT==中央夏令时 但是

M4.1.0和M10.5.0是什么意思?

以下是我的发现:

出口TZ=EST05EDT

EST取自下表。要获得UTC,必须添加5个小时,因此是05。目前,美国东部时间,东部日光,是有效的。

TZ=。

DST时区

  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.                         

有没有更简单的方法让我回到正确的时间?是的,只需取消设置TZ环境变量。

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

注意,即使TZ被调整为夏令时,5个月后你会得到正确的时间吗?夏令时什么时候生效?下面显示的TZ值仅在正确的日期内根据dst进行调整。例如,此条目将于4月第一周的凌晨2点生效,并于10月第五周的凌晨2点结束。注10.5.0代表10月的第5周,而不是第5天。

  $ export TZ=EST+5EDT,M4.1.0/2,M10.5.0/2

Tags: middlefor时间tzestutceuropeansummer
1条回答
网友
1楼 · 发布于 2024-05-15 08:43:23

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.

相关问题 更多 >