2

毎分時間をチェックしていますが、この「オン」モードの操作にあるかどうかを確認する良い方法がありません。日の出の 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()
4

1 に答える 1

3

太陽が昇った瞬間、私のロジックは失敗したように見えます。その時点以降、明日の日の出の計算が開始されるからです。

ロジックを考えてみてください。ケースは何ですか?

  • 日の出前、日の入り前。その場合、チェックするだけで済みます<= sunrise-2H。のチェックsunset-1Hは無関係ですが、無害です。
  • 日の出と日没の間。その場合、チェックするだけで済みます<= sunset-1H。のチェックsunrise-2Hは無関係であるだけでなく、有害です。
  • 日の出と日没の両方の後。これは実際には最初のケースと同じです。(サンフランシスコでは、真夜中の後に日没が来ることはなく、日の出が前に来ることもありませんが、コードをたとえば Oulo で機能させたい場合は、問題になる可能性があります。)

では、自分がどのケースに属しているかをどのように知ることができますか? 単純。の場合sunset > sunriseは、ケース 1 または 3 です。確認してくださいsunrise-2H。それ以外の場合は、ケース 2 です。チェックしてくださいsunset-1H

オウロよりさらに北に行ったら?たとえば、ロヴァニエミでは 6 月初旬に次の日没まで 1 か月かかります。それはあなたのプログラムを一ヶ月中続けたいということですか?または、任意の開始時間と終了時間を選択したいですか (たとえば、「真夜中」の 2 時間前に開始し、1 時間後に終了します)。どんなルールを思いついたephemとしても、それを書くのに十分なデータがあると思います。または、わからない場合は、少なくともコードをテストして、適用されているルールを確認、それを文書化してください。(そこに住んでいる人は、このような時間関連のプログラムのドキュメントを見ることに慣れていると思います…)

于 2014-09-28T23:26:55.263 に答える