Python - 使用 UTC 时间戳检查是否是另一个时区的 DST

标签 python python-3.x datetime time pytz

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

最佳答案

您可以使用 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

关于Python - 使用 UTC 时间戳检查是否是另一个时区的 DST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58774833/

相关文章:

python - 绘制 Pandas 时间序列数据框的线性回归线的置信区间

python-3.x - 在 FLASK 中运行 pypupeteer 给出 ValueError : signal only works in main thread

python - 如何在 python 中用整数制作网格?

c++ - std::to_string 不是使用 CygWin 的 C++11 中的成员

php Y-m-d 添加 H :i:s

c# - 如何在 .NET 中将此字符串格式化为 DateTime

Java 时间解析具有短日期名称的日期

python - 在python 3.6中从公钥到公共(public)地址生成比特币 key 对

python - 在 ipython 笔记本中更改字体和背景颜色

python - 从外部范围阴影名称 xyz