2

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.

jww
  • 97,681
  • 90
  • 411
  • 885
Octoth0rpe
  • 2,267
  • 4
  • 19
  • 21
  • you should use `time.time()` instead of `time.mktime(time.localtime())` the later might return wrong result during DST transitions. – jfs Sep 19 '14 at 15:07
  • see [Sunrise / set calculations](http://stackoverflow.com/a/2539597/4279) – jfs Sep 19 '14 at 15:09
  • Python relies on your operating system settings to determine what local time means. So we should begin by leaving Python out of the equation: if you type the command `date` on your Raspberry Pi, what is its output — could you paste it into your question for us? – Brandon Rhodes Sep 19 '14 at 16:35
  • yup, you can see from the "now is .." comment second from the bottom that datetime.datetime.now() is returning the accurate localtime. Also, the date or time command from the command line returns accurate to localtime. – Octoth0rpe Sep 20 '14 at 01:41
  • also, the difference between the result of time.mktime(time.localtime()) and time.time() is gmNow is 1411178199.0 timetimeNow is 1411178199.284297 – Octoth0rpe Sep 20 '14 at 01:57
  • some cities (not many) are already know to `ephem` therefore instead of setting coordinates, in your specific case you can create an observer with `ephem.city('Vancouver')` – ccpizza Nov 28 '21 at 17:41

1 Answers1

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
jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • I added this, and the result of datetime.utcnow() is an error: timeUTC = datetime.utcnow() AttributeError: 'module' object has no attribute 'utcnow' – Octoth0rpe Sep 20 '14 at 01:49
  • use `from datetime import datetime` – jfs Sep 20 '14 at 01:55
  • dang, nope. sunrise is at 2014-09-20 12:53:17.000005 sunset is going to be at 2014-09-20 00:54:27.000005 now is 2014-09-20 02:01:28.962811 it switched the 'now' to UTC time, but still fails – Octoth0rpe Sep 20 '14 at 02:02
  • @Octoth0rpe: you should also fix longitude, latitude. I've updated the answer with complete code example – jfs Sep 20 '14 at 02:16
  • YES!! Thank you. See? Something simple. Argh. 'forest for the trees'. I never even thought of that and went all rambo looking for other datetime, date, timedate, etc packages. Thanks again! – Octoth0rpe Sep 20 '14 at 12:38