I'm writing a Python program that needs to determine whether it's post-sunrise or not, given only the current UTC time and the target latitude and longitude. I see apps do this sort of thing all the time, but I have no idea how it's done. Any ideas?
-
3http://mathforum.org/library/drmath/view/56478.html – Andrey Agibalov Sep 12 '11 at 08:55
-
That was the first link I found, but it wasn't enough for someone with limited calculus background like me to work with. – Daniel Quinn Sep 12 '11 at 10:04
-
Depends on the date too, of course. – Tom Zych Nov 06 '15 at 23:48
-
related: [Calculating dawn and sunset times using PyEphem](http://stackoverflow.com/q/2637293/4279) – jfs Nov 07 '15 at 06:46
-
related: [Sunrise / set calculations](http://stackoverflow.com/q/2538190/4279) – jfs Nov 07 '15 at 06:47
4 Answers
Here's someone's implementation of sunrise/sunset calculation in Python: http://michelanders.blogspot.com/2010/12/calulating-sunrise-and-sunset-in-python.html

- 49,061
- 27
- 133
- 172
Just doing a quick google search I found this. The calculations in the NOAA Sunrise/Sunset and Solar Position Calculators are based on equations from Astronomical Algorithms, by Jean Meeus. The sunrise and sunset results have been verified to be accurate to within a minute for locations between +/- 72° latitude, and within 10 minutes outside of those latitudes.
A detailed explanation of the calculation details can be found from that page and also here.

- 1,143
- 2
- 14
- 28
-
Single-link answers can **easily** bit-rot. You should *at least* provide a short summary of the link in your answer. – Joachim Sauer Sep 12 '11 at 08:57
-
Posters are also supposed to have at least run their questions through a search engine. I was trying to relay that requirement in as helpful a manner as I could. – smulholland2 Sep 12 '11 at 09:01
-
If you think that the question is bad, then a bad answer is *not* the appropriate response. – Joachim Sauer Sep 12 '11 at 09:01
-
Actually, I'd run into that link in my own search, and you're right it's exactly what I needed, just in Javascript. I was hoping that there was a Python module out there for something like this rather than having to rewrite it based on that. – Daniel Quinn Sep 12 '11 at 10:03
You need something that just works™? Use the superior language. And you know it'll always work. How can you trust 122 lines of advanced astrophysics? You can't. But you can always trust PHP.
>>> def is_day(lat, lon): # optinally adjust zenith (currently 96) at the end of the line
... return subprocess.check_output(["php","-r","""date_default_timezone_set("GMT");\n$a=date_sunrise(time(),SUNFUNCS_RET_DOUBLE,{0},{1},{2},0);\n$b=date_sunset(time(),SUNFUNCS_RET_DOUBLE,{0},{1},{2},0);\n$t=date("H")+date("i")/60+date("s")/3600;\necho($a<$b?($t>$a&&$t<$b):($t>$a||$t<$b))?"day":"night";""".format(lat,lon,96)])=="day"
... # ^^ optionally adjust zenith here
...
>>> is_day(-33,151) # Sydney
True
>>> is_day(0,0) # Some hundred km off the coast of Africa
False
>>> __import__("datetime").datetime.utcnow()
datetime.datetime(2012, 2, 10, 22, 29, 30, 365019)
>>>

- 20,267
- 14
- 135
- 196