我正在检查每分钟的时间,但是没有一个好的方法来检查我是否处于这种“开启”操作模式。我想在日光照射前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()

最佳答案

太阳升起的那一刻,我的逻辑似乎失败了,因为在那之后,它开始计算明天的日出。


通过逻辑思考。什么情况


在日出和日落之前。在这种情况下,您只需要检查<= sunrise-2Hsunset-1H的检查无关紧要,但无害。
在日出和日落之间。在这种情况下,您只需要检查<= sunset-1Hsunrise-2H的检查不仅无关紧要,而且有害。
在日出和日落之后。这实际上与第一种情况相同。 (在旧金山,日落永远不会在午夜之后出现,而日出永远不会在午夜之前出现,但是如果您想让代码在Oulo中运行,则可能会出现问题。)


那么,您如何知道自己在哪种情况下?简单。如果sunset > sunrise,则表示情况1或3;只需检查sunrise-2H;否则,您将处于情况2,只需检查sunset-1H

如果您比Oulo更北,该怎么办?例如,在6月初的罗瓦涅米(Rovaniemi),下一个日落是一个月的路程。这是否意味着您希望您的程序持续运行一个月?还是您想选择一个任意的时间来启动和关闭(例如,在“午夜”之前2小时开始,然后在1小时之后结束)?不管您提出什么规则,我都认为ephem具有足够的数据供您编写。或者,如果您不知道,请至少测试代码并查看其适用的规则,以便将其记录下来。 (我猜想住在那里的人习惯于看文档中与时间相关的程序,例如……)

关于python - 如何在日出前2小时开始过程而日落前1小时停止过程?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26090494/

10-12 20:15