本地时间实际上并不给本地时间(localtime not actually giving local

2019-10-20 23:53发布

有很明显的是结合这一问题工作的时间模块,但我还没有找到它。 我只是试图用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

它的驾驶我坚果,这是明显的那些简单的事情,一旦它找到了一个,所以我要去蜂群思维在这里。

编辑**只需输入“日期”为树莓派的命令行返回以下内容:

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

这是准确的。

Answer 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


文章来源: localtime not actually giving localtime