Sunrise, Sunset
November 24, 2009
Astronomy has fascinated mankind since its earliest days; science and math were developed first to study the heavens. Even today, your local newspaper tells you such astronomical information as the time of local sunrise and sunset and the phase of the moon.
Your task is to write functions that calculate the time of sunrise and sunset for any spot on earth for any day of the year; you will have to do your own research on the internet to find a suitable set of formulas. When you are finished, you are welcome to read or run a suggested solution, or to post your solution or discuss the exercise in the comments below.
Note that there are places, like central Australia and the whole of India, where the timezone offset from Universal Time is not an integral number of hours. The solution should specify the offset in minutes instead.
The GMT offset need not be given as an integer. Here is the sunrise/sunset calculation for Calcutta, India for today:
> (call-with-values
(lambda () (solar 22.6 -88.4 2009 11 24 -6.5))
(lambda (rise set)
(display "Sunrise: ") (display rise) (newline)
(display "Sunset: ") (display set) (newline)))
Sunrise: 5:57
Sunset: 16:53
The times agree with Weather Underground within three minutes.
Fair enough, since this is a float API. I was making a general point that was malapropos to this particular piece of code.
What about polar circles? How should a program behave when given a point close to a pole?
See the article from the U. S. Naval Observatory referenced in the solution. It works even very close to the poles.
There are public domain APIs to do this. Check the Acknowledgements section on http://sunrisehour.com!