[time-nuts] Sunrise, solar noon, sunset and the Equation of Time
holrum at hotmail.com
Wed Aug 31 01:07:51 EDT 2016
I recently added code to Lady Heather to precisely calculate the sun position, sunrise/solar noon/sunset times and the Equation of Time... Egad, what a deep, dark rabbit hole that leads one down. And woe be unto thee that trusts any of the online calculators. Results can be over 10 minutes off and nobody gives times down to the second. Oh, and there's a half dozen different types of sunrise/sunset to consider.
The gold standard for those calculation is NREL's Solar Position Algorithm. It's awesome. It's also awesomely complex. Like 1300 lines of code and several thousand double precision operations and 300+ trig functions per result. It also has a rather wonky license agreement. (and if your time zone is not Greenwich is can report the sunrise or sunset time for the previous/next day)
I finally settled on Grena's Algorithm 5 for sun position. It only around 100 lines long uses less than 20 trig function calls to calculate the sun position and produces results comparable to SPA for the years 2010 .. 2110.
Getting sunrise/sunset/solar noon from solar position is a bit more complicated. I do binary searches and interpolations to find when the sun crosses the magic thresholds to within a second... it takes about 300 lines of code and 60 sun position evaluations. Lady Heather can now play sound files at sunrise, solar noon, sunset... your basic GPS disciplined rooster and church bells.
And, for the sundial crowd, tomorrow is the day the Equation of Time crosses 0.0 seconds so get out and set your sundials...
More information about the time-nuts