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.

Advertisement

Pages: 1 2

6 Responses to “Sunrise, Sunset”

  1. John Cowan said

    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.

  2. programmingpraxis said

    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.

  3. John Cowan said

    Fair enough, since this is a float API. I was making a general point that was malapropos to this particular piece of code.

  4. Kallikanzarid said

    What about polar circles? How should a program behave when given a point close to a pole?

  5. programmingpraxis said

    See the article from the U. S. Naval Observatory referenced in the solution. It works even very close to the poles.

  6. Fredrik said

    There are public domain APIs to do this. Check the Acknowledgements section on http://sunrisehour.com!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: