Stuntman And Lover Of Cheese

by Dan Bostonweeks

FESSolarCalculator: Sunrise, Sunset, and Twilight Times for Cocoa

Recently I sat down to think about how I would improve on RadioMaestro. I’ve let it go for far too long without an update. One thing I left out when originally developing RadioMaestro was the ability to change the list of AM stations at night. In AM broadcasting many stations are forced to reduce power or suspend operations at night. It has to do with changes in the ionosphere and the radio waves traveling farther at night.

Rather than just having an arbitrary time to pick between day- and night-time AM stations I also thought about interfaces that should change when it’s dark. Reading apps like Read It Later have a night mode. Camera apps might have saved settings for day and night shots. A desk or night table clock app might have different modes for day and night. It even extends to audio, an app may want to have lower volume at night on startup. There’s no reason those apps can’t use the geolocation of your device, the date, and know within an acceptable precision if the sun has risen or set or is up and shining.

I looked around a bit but I didn’t really find anything that hit the right spot for what I was envisioning. I just want something simple that I pass an NSDate and a CLLocation and it does it’s thing. I can then grab whatever times I need out of it.

I sat down and started to dig on the math behind the calculations and fortunately there are a lot of really great explanations by people far smarter than me. It took me a bit to make sure it was working correctly and now I have written a class, FESSolarCalculator, that does exactly as my requirements listed above.

The FESSolarCalculator project is hosted at GitHub. There are currently two issues that I know about and I’ll get to those in the coming days. Pull requests are welcome as is all manner of criticism. Thanks to Rob Rix for a sanity check and for talking about approaches to a better, more streamlined approach from my initial implementation.

The source is MIT Licensed so go nuts with it. I hope you find it useful. Drop me a note if you do.