2

显然有一个时间模块可以与这个问题结合使用,但我还没有找到它。我只是想在 Raspberry Pi 上使用 Pyephem 来找出我的纬度经度坐标的日出和日落时间。代码很简单:

import ephem
import datetime 
import time
now = datetime.datetime.now()
gmNow = time.mktime(time.localtime()) 
Vancouver = ephem.Observer()
Vancouver.lat = 49.2878
Vancouver.horizon = 0
Vancouver.lon = -123.0502
Vancouver.elevation = 80
Vancouver.date = now
# Vancouver.date = time.localtime()

sun = ephem.Sun()

print("sunrise is at",ephem.localtime(Vancouver.next_rising(sun)))
print("sunset is going to be at ",ephem.localtime(Vancouver.next_setting(sun)))
print("now is ",now)
print("gmNow is",gmNow)

什么出口,什么时候运行是错误的 8 小时。所以看起来 ehem.localtime() 实际上并没有运行。

pi@raspberrypi ~ $ sudo python3 vivarium_sun.py 
sunrise is at 2014-09-19 12:55:56.000004
sunset is going to be at  2014-09-19 00:52:30.000004
now is  2014-09-19 06:22:24.014859
gmNow is 1411132944.0

这让我发疯了,一旦弄清楚,这显然是那些简单的事情之一,所以我要在这里讨论蜂巢思维。

编辑**只需在 Raspberry Pi 的命令行中输入“日期”即可返回以下内容:

pi@raspberrypi ~ $ date
Fri Sep 19 18:41:42 PDT 2014

这是准确的。

4

1 回答 1

1

您应该传递datetime.utcnow()给观察者而不是您的当地时间。

ephem如果作为浮点数传递,期望latitudelongitude弧度,请改用字符串:

from datetime import datetime, timezone

import ephem

now = datetime.now(timezone.utc)
Vancouver = ephem.Observer()
Vancouver.lat = '49.2878'
Vancouver.horizon = 0
Vancouver.lon = '-123.0502'
Vancouver.elevation = 80
Vancouver.date = now
sun = ephem.Sun(Vancouver)

print("sunrise is at", ephem.localtime(Vancouver.next_rising(sun)))
print("sunset is going to be at ", 
      ephem.localtime(Vancouver.next_setting(sun)))
print("now is ",now.astimezone())

输出

sunrise is at 2014-09-20 06:55:38.000005
sunset is going to be at  2014-09-19 19:16:38.000004
now is  2014-09-19 19:15:04.171486-07:00
于 2014-09-19T14:58:30.820 回答