Determining the Sunrise and Sunset for a Particular Location
By Mike ShafferIn my last article, Calculating the Distance Between Cities, I showed you how to calculate the distance between two geographical points on the Earth. In it, I jokingly mentioned that while you were scanning your database and performing all of the calculations for distance, you might want to entertain (or distract) the user by letting her know when the sun was going to set in her location (since by then you had her latitude and longitude).
Well, it's just my luck that people took me seriously and began sending emails asking how to perform sunrise and sunset calculations! :)
So, here is a snippet of code that will calculate either sunrise or sunset for a given latitude and longitude. There are all kinds of caveats attached to this code, and to avoid any misunderstandings, please read them carefully:
- This code works for the years 1901 to 2099
- This code will not work for latitudes greater than 63 degrees (North OR South)
- This code calculates visual sunrise and sunset (this is what you normally see on
the evening news). It is NOT intended to be used for other types of sunrise/sunset events
such as civil, nautical or astronomical twilight (although it could be modified to do so)
- This code is not intended to produce results that adhere to any religious definitions
of sunrise/sunset
- This code does NOT take regional time changes (e.g. Daylight Savings time) into account.
Whew! Having now examined all of the disclaimers, let's look at the parameters required to
call the GetSunRiseSet function:
year, month, dayyear/month/day to use for calculations
|
Here is some sample code that shows how to call the GetSunRiseSet function:
|
The result returned from the GetSunRiseSet function will be the time of the
event (in 24-hour or military-time format).
Yes, there are alternatives to using this code. One thing that I have seen many sites do is to perform a 'screen-scrape' from another site that already performs these calculations, such as www.weather.com. While that technique may work in general, it relies on the fact that weather.com is always up when you need it. When it comes to solving programming problems, I believe that 'elegant and reliable' solutions are much better than 'quick-and-dirty' solutions. (As a side note, you can learn how to grab information off of other web servers by reading, Grabbing Information From Other Servers.)
It should be noted that this code is based on the work of several other people, most of whom specialize in the field of celestial mechanics. While it's true that I am an avid amateur astronomer, I cannot claim credit for developing the methods that are encapsulated by these routines. Please see the comments in the routines for the names of the people whose shoulders I was fortunate to be able to stand upon.
For information on how to obtain a database of cities, zip codes, and longitude/latitude measurements, be sure to consult my previous article, or visit GreatData.com.
Happy Programming!
Attachments:



