我通常使用此代码获取本地时区:
import platform
import time
import datetime
import pytz
#LOCAL_TIMEZONE = datetime.datetime.now(datetime.timezone.utc).astimezone().tzinfo # "CEST", etc on Linux, but 'Romance Standard Time' on Windows
if "win" in platform.system().lower(): # SO:1387222
# SO:16156597 - try tzlocal.win32; MINGW64: `pip3.8.exe install tzlocal`; RPi: `sudo apt install python3-tzlocal`
from tzlocal.win32 import get_localzone_name
LOCAL_TIMEZONE = pytz.timezone(get_localzone_name())
else:
from tzlocal import get_localzone
LOCAL_TIMEZONE = get_localzone()
# Above results with LOCAL_TIMEZONE: Europe/Copenhagen in Linux, Europe/Paris in Windows
如何获得在Windows和Linux中都能使用的CET或CEST(字母代码)本地时区
^{} 包应该为您提供所需的内容-但是您还需要一个
datetime
对象,可以用于strftime('%Z')
:另外,我在Windows上;现在无法在Linux上测试
相关问题 更多 >
编程相关推荐