显然有一个时间模块可以与这个问题结合使用,但我还没有找到它。 我只是想在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小时但是。所以它似乎是 ephem.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的命令行中键入“date”即可返回以下内容:
pi@raspberrypi ~ $ date
Fri Sep 19 18:41:42 PDT 2014
这是准确的。
答案 0 :(得分:1)
您应该将datetime.utcnow()
传递给观察者,而不是当地时间。
ephem
预计latitude
和longitude
以弧度为单位,请使用字符串:
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