我大约每分钟检查一次时间,但没有一个好的方法来检查我是否处于这种“开启”操作模式。我想在白天前 2 小时“开”到日落前 1 小时。如果我不断检查使用next_rising()
and 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()