【发布时间】:2020-03-05 06:06:51
【问题描述】:
当我尝试使用 pyephem 计算下一个日落时间时,它会给我今天的日落时间,即使我是在今天日落之后打电话。我期望的行为是明天返回日落 - 即下一个日落。也许我在做一些简单而错误的事情,但我无法解决。
在发布此问题时,问题似乎与使用 localtime 有关。
代码如下:
#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))
给予:
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]
这显然是错误的:下一个日落是在 2020-03-05,如果它在 2020-03-04 日落之后。
值得注意的是,如果我不使用 localtime(并使用格林威治 - 使检查结果变得简单),它似乎可以工作:
#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))
给予
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]
我不确定这里有什么问题。我查看了文档,但无法解决。也许localtime 有问题,或者我遗漏了一些简单的东西 - 抱歉,我对 python 和这个模块都是新手。
【问题讨论】: