gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 20:09:49 35 4
gpt4 key购买 nike

我有一个 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/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com