我几乎每分钟都会检查一次时间,但没有一个好的方法来检查我是否处于“开启”操作模式。我想在日出前 2 小时一直“开启”,直到日落前 1 小时。如果我不断使用 next_rising()
和 next_setting()
进行检查,太阳升起的那一刻,我的逻辑似乎失败了,因为在那之后它开始计算明天的日出。我的 is_daytime()
坏了。
def is_daytime():
"""
Returns whether we should operate in the 'daytime' mode. Note that this
mode is shifted earlier to begin before sunrise and end before sunset.
"""
# Get the localized hour of the day
now = datetime.datetime.now()
# Get the next sunrise/sunset
here.date = now
sunrise = ephem.localtime(here.next_rising(ephem.Sun))
sunset = ephem.localtime(here.next_setting(ephem.Sun))
sunrise_shift = datetime.timedelta(hours=START_BEFORE_SUNSRISE_HR)
sunset_shift = datetime.timedelta(hours=END_BEFORE_SUNSET_HR)
# Return whether it is some amount of time before sunrise AND sunset
return ((sunrise - now) < sunrise_shift) and ((sunset - now) < sunset_shift)
编辑:阅读解决方案后更新
# Dependencies
import time
import datetime
import pytz
import ephem
# Choose your location for sunrise/sunset calculations
MY_TIMEZONE = "America/Los_Angeles"
MY_LONGITUDE = '37.7833' # +N
MY_LATITUDE = '-122.4167' # +E
MY_ELEVATION = 0 # meters
# Choose when to start and stop relative to sunrise and sunset
START_BEFORE_SUNSRISE_HR = 1
END_BEFORE_SUNSET_HR = 1
here = ephem.Observer()
def is_daytime():
"""
Returns whether we should operate in the 'daytime' mode. Note that this
mode is shifted earlier to begin before sunrise and end before sunset.
Assumes sunset NEVER comes after midnight
"""
# Get the localized hour of the day
now = datetime.datetime.now()
# Get the next sunrise/sunset
here.date = now
next_sunrise = ephem.localtime(here.next_rising(ephem.Sun()))
next_sunset = ephem.localtime(here.next_setting(ephem.Sun()))
sunrise_shift = datetime.timedelta(hours=START_BEFORE_SUNSRISE_HR)
sunset_shift = datetime.timedelta(hours=END_BEFORE_SUNSET_HR)
# If it's daytime
if (next_sunset < next_sunrise):
return (now < (next_sunset - sunset_shift))
# Otherwise it's nighttime
else:
return ((next_sunrise - sunrise_shift) < now)
def main():
# Configure the timezone
pytz.timezone(MY_TIMEZONE)
# Configure the ephem object
here.lat = MY_LATITUDE
here.lon = MY_LONGITUDE
here.elevation = MY_ELEVATION
while True:
if is_daytime():
print "It's daytime!"
time.sleep(60)
if __name__ == '__main__':
main()
最佳答案
the moment the sun rises, my logic seems to fail because after that point it starts computing the sunrise for tomorrow.
仔细思考逻辑。有哪些案例?
- 日出和日落之前。在这种情况下,您只需检查
<= sunrise-2H
;检查sunset-1H
无关紧要,但无害。 - 日出和日落之间。在这种情况下,您只需检查
<= sunset-1H
;检查sunrise-2H
不仅无关紧要,而且有害。 - 日出和日落之后。这其实和第一种情况是一样的。 (在旧金山,日落永远不会在午夜之后到来,日出也不会在午夜之前到来,但如果你希望你的代码在 Oulo 等地方工作,这可能是一个问题。)
那么,你怎么知道你属于哪种情况呢?简单的。如果sunset > sunrise
,您属于情况 1 或 3;只需检查sunrise-2H
;否则,您属于情况 2,只需检查 sunset-1H
.
如果你去比奥洛更北的地方怎么办?例如,在六月初的罗瓦涅米,距离下一次日落还有一个月的时间。这是否意味着您希望您的计划持续整个月?或者您想选择任意时间来启动和关闭(例如,在“午夜”之前 2 小时开始并在“午夜”之后 1 小时结束)?无论你想出什么规则,我认为ephem
有足够的数据供您编写。或者,如果您不知道,至少测试代码并查看它应用的规则,以便您可以记录下来。 (我猜住在那里的人习惯于查看此类与时间相关的程序的文档......)
关于python - 如何在日出前2小时开始进程并在日落前1小时停止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26090494/