首页 > 解决方案 > Python - 使用 UTC 时间戳检查它是否是另一个时区的 DST

问题描述

我有一个 UTC 时间戳,我想检查当时是否是伦敦的夏令时。我将如何在 Python 中做到这一点?

标签: pythonpython-3.xdatetimetimepytz

解决方案


您可以使用 os.environ 设置您的时区。以下是在两个不同时区检查 DST 的示例:

import time, os

os.environ['TZ'] = 'Europe/London'                                                                                                                                                      

timestamp = os.path.getmtime(filename) 
isdst = time.localtime(timestamp).tm_isdst > 0                                                                                                                                          


In [973]: timestamp                                                                                                                                                                               
Out[973]: 1571900789.0347116

In [965]: isdst                                                                                                                                                                                   
Out[965]: True

os.environ['TZ'] = 'USA/Colorado'                                                                                                                                                        

timestamp = os.path.getmtime(filename) 
isdst = time.localtime(timestamp).tm_isdst > 0                                                                                                                                          

In [968]: isdst                                                                                                                                                                                   
Out[968]: False

推荐阅读