I'd like to set up a Python program to be able to pull sunrise/sunset from various locations to trigger lights in the local location to symbolize the remote sunrise as it would be -- if you were actually there. What I mean by this, is if you live in Berlin, and YOUR sunrise/sunset in the middle of December is 7:45am/4:15pm, you could have a little LED that would light when some tropical sunrise is happening (say Hawaii). But this is happening in reaction to your own local time.
So, using Python's ephem, and pytz, and localtime, pull the information for sunrise/sunset for various locations, and trigger events based on each location.
I've set up a test program using Vancouver, BC and Georgetown, French Guyana as a test case, and it mostly works -- but the sunrise/sunset for Georgetown is completely wrong.
You can cut and paste this entire thing into a Python window to test, and please forgive the extraneous time calls, but I find it interesting to see what each of the time calls pull.
Nonetheless, what you'll see is that the Guyana.date is absolutely correct, but the sunrise/sunset is something like 1:53 AM / 13:57 PM, which is completely whacked. Any ideas on how this could have gone so horribly, horribly wrong?
EDITED TO REMOVE UNNECESSARY CODE
import ephem
from datetime import datetime, timedelta
from pytz import timezone
import pytz
import time
Guyana = ephem.Observer()
Guyana.lat = '5'
Guyana.lon = '58.3'
Guyana.horizon = 0
Guyana.elevation = 80
Guyana.date = datetime.utcnow()
sun = ephem.Sun()
print("Guyana.date is ",Guyana.date)
print("Guyana sunrise is at",Guyana.next_rising(sun))
print("Guyana sunset is going to be at ",Guyana.next_setting(sun))
The results of this are as follows:
Guyana.date is 2014/10/4 16:47:36
Guyana sunrise is at 2014/10/5 01:53:26
Guyana sunset is going to be at 2014/10/5 13:57:05
What is so wrong about this, is that the actual sunrise in Guyana today is 5:40am, so the 1:53:26 is off by not just hours, but in many ways off.
To answer your updated version: positive longitudes refer to East but Guyana (America) is to the west from Greenwich therefore you should use minus: Guyana.lon = '-58.3'
then the time of the sunrise becomes:
Guyana sunrise is at 2014/10/5 09:39:47
The time is in UTC, you could convert it to the local (Guyana) time:
>>> utc_dt = Guyana.next_rising(sun).datetime().replace(tzinfo=pytz.utc)
>>> print(utc_dt.astimezone(pytz.timezone('America/Guyana')))
2014-10-05 05:39:46.673263-04:00
5:40am local time seems reasonable for sunrise.
From ephem
documentation:
Dates always use Universal Time, never your local time zone.
As I said in my answer to your previous question:
You should pass datetime.utcnow() to the observer instead of your local time.
i.e., Vancouver.date = now
is wrong because you use datetime.now()
that returns a naive local time (pass datetime.utcnow()
(or ephem.now()
) instead), Guyana.date = utc_dt.astimezone(FrenchGuyanaTZ)
is wrong because FrenchGuyanaTZ
timezone is probably has a non-zero UTC offset (pass just utc_dt
instead).
Unrelated: a correct way to find the timestamp for the current time is time.time()
i.e., gmNow
should be equal to timetimeNow
(always). As I said:
you should use
time.time()
instead oftime.mktime(time.localtime())
the later might return wrong result during DST transitions.
The correct way to find the current time in UTC is:
utc_dt_naive = datetime.utcnow()
Or if you need an aware datetime object:
utc_dt = datetime.now(utc_timezone)