python - Python - 使用 UTC 时间戳检查它是否是另一个时区的 DST
问题描述
我有一个 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 - 如何创建索引从 0 开始的数据帧的子集?
- cpu - 第 1 行出现“预期表达式”错误。如何解决?
- graphviz - 如何在 Graphviz 中使节点居中
- c++ - 如何在 linux 中为 c++ 项目自动增加共享文件的内部版本号
- reactjs - 从 JSON 动态设置图像 src
- java - map of map 根据内图值获取外图
- javascript - 删除字符串中的现有标签。我怎样才能防止这种情况?
- python - 如何使用 c-python-client 模块获取 GET /v2/quota_definitions api
- excel - 如何将数据从一个电子表格粘贴到另一个电子表格的最后一列?
- python-3.x - 在 Python 中使用 Selenium 在 Google 专利中搜索后单击第一个链接/专利