本地时间实际上没有给出本地时间 [英] localtime not actually giving localtime

查看:156
本文介绍了本地时间实际上没有给出本地时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显然有一个与该问题结合使用的时间模块,但我尚未找到它. 我只是试图在Raspberry Pi上使用Pyephem,以查找日出和日落的时间是我的经度坐标. 代码很简单:

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)

什么出口,但运行8小时仍是错误的.所以看来 ephem.localtime()实际上没有运行.

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.

编辑**仅在Raspberry Pi的命令行中输入'date'即可返回以下内容:

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

这是准确的.

推荐答案

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

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

ephem如果以浮点形式传递,则期望以弧度表示的latitudelongitude,请改用字符串:

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

输出

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

这篇关于本地时间实际上没有给出本地时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆