localtime not actually giving localtime

瘦欲@ 提交于 2020-01-05 07:31:29

问题


there's obviously a time module that works in combination with this problem, but I have not found it yet. I'm simply trying to use Pyephem on a Raspberry Pi to find out what time sunrise and sunset is for my latitude longitude coordinates. the code is quite simply this:

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)

what exports, when that runs is wrong by 8 hours though. so it appears that the ephem.localtime() is not actually running.

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

It's driving me nuts, and it's obviously one of those simple things once it's figured out, so I'm going to the hive mind here.

EDIT** Just typing 'date' into the command line of the Raspberry Pi returns the following:

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

which is accurate.


回答1:


You should pass datetime.utcnow() to the observer instead of your local time.

ephem expects latitude and longitude in radians if passed as floats, use strings instead:

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())

Output

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


来源:https://stackoverflow.com/questions/25935077/localtime-not-actually-giving-localtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!