When I try and work out the next sunset time using pyephem it gives me sunset time today, even if I am calling after sunset today. The behavior i would expect is to return sunset tomorrow - i.e. the next sunset. Perhaps I am doing something simple and wrong, but i can't work it out.
In posting this question it appears that the problem might be with using localtime
.
Here is the code:
#import modules
import datetime
import ephem
now = datetime.datetime(2020, 3, 4, 21, 00, 00) #set a relevant time after sunset today
wa = ephem.Observer() #create observer object
wa.lat = '47' #set observer parameters
wa.lon = '-122'
wa.date = now
s = ephem.Sun() #identify observed object (the sun)
s.compute(wa) #compute parameters
next_sunrise = ephem.localtime(wa.next_rising(s)) #return sunrise and sunset
next_sunset = ephem.localtime(wa.next_setting(s))
print('Its currently {}, next sunrise will be {}.'.format(now, next_sunrise))
print('Its currently {}, next sunset will be {}.'.format(now, next_sunset))
Gives:
Its currently 2020-03-04 21:00:00, next sunrise will be 2020-03-05 06:38:46.186438.
Its currently 2020-03-04 21:00:00, next sunset will be 2020-03-04 17:59:11.109622.
[Finished in 0.065s]
This is clearly wrong: next sunset is on 2020-03-05, if its after sunset on 2020-03-04.
Notably it appears to work if I don't use localtime
(and use Greenwich - to make checking the results simple):
#import modules
import datetime
import ephem
now = datetime.datetime(2020, 3, 4, 21, 00, 00) #set a relevant time after sunset today
greenw = ephem.Observer() #create observer object
greenw.lat = '50' #set observer parameters
greenw.lon = '0'
greenw.date = now
s = ephem.Sun() #identify observed object (the sun)
s.compute(greenw) #compute parameters
next_sunrise = greenw.next_rising(s) #return sunrise and sunset
next_sunset = greenw.next_setting(s)
print('Its currently {}, next sunrise will be {}.'.format(now, next_sunrise))
print('Its currently {}, next sunset will be {}.'.format(now, next_sunset))
Gives
Its currently 2020-03-04 21:00:00, next sunrise will be 2020/3/5 06:33:54.
Its currently 2020-03-04 21:00:00, next sunset will be 2020/3/5 17:49:44.
[Finished in 0.07s]
I am not sure what the problem is here. I have looked through the documentation but cannot work it out. Perhaps there is a problem with localtime
, or perhaps I am missing something simple - apologies I am new to both python and this module.