【发布时间】:2018-07-13 08:54:11
【问题描述】:
我开始学习 python,我尝试创建一个脚本来在某个时间运行某个命令。这是我目前所拥有的:
import datetime
day_of_week = datetime.date.today().weekday() # 0 is Monday, 6 is Sunday
time = datetime.datetime.now().time()
if day_of_week == 4 and (time >= datetime.time(2,45) and time < datetime.time(9)):
#do something
elif (day_of_week == 3 and time > datetime.time(22)) or (day_of_week == 4 and time < datetime.time(2,45)):
#wait until 2h45
else:
#do nothing
我希望脚本仅在某一天以及大约 19h00 PST/PDT(2h45 GMT/UTC)时继续运行。我写的那个适用于 GMT/UTC,但我确信当夏令时开始或我将本地时区更改为其他时区时它会失败。
如何让脚本检查 PST/PDT 中的当前时间并从那里继续,忽略或转换本地时区?
【问题讨论】:
-
您为什么要尝试在 Python 中执行此操作,而不是使用操作系统内置的功能(*nix 上的 cron,Windows 上的任务计划程序),这两者都会为您完成 DST 调整工作?
-
我不想按计划运行它。我想在需要时打开脚本。我要把这个发给朋友,一天的限制是避免在不应该运行的时候运行脚本。
标签: python python-3.x timezone