当我尝试用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
(并使用Greenwich-使检查结果变得简单),它似乎可以工作:
#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和这个模块都是新手
我仔细研究了这个问题,最后找到了一个解决办法
似乎以弗所已经/正在被抛弃,取而代之的是天域(https://rhodesmill.org/pyephem/)。我看了那个文档来确定上升、运输和沉降(https://rhodesmill.org/skyfield/almanac.html),但它看起来像是钉子的大锤。在看到我将要运行一个API之后,我决定使用sunrise-sunset API可以更轻松地解决我的小问题:https://sunrise-sunset.org/api
我想写的代码是看太阳是否升起。以下是python3中的解决方案(对于任何草率的代码表示歉意):
给出:
相关问题 更多 >
编程相关推荐