Click here to Skip to main content
15,879,239 members
Articles / Desktop Programming / MFC

Sunrise/Sunset Calculations

Rate me:
Please Sign up or sign in to vote.
4.12/5 (12 votes)
14 Dec 2000 171.3K   5.4K   32   28
Code to help calculate sunrise and sunset times

Introduction

I searched the web for a piece of code that calculates the Sunrise/Sunset based on the Date and a latitude and longitude, but could not find one in C++ that was freely available. People have FreeWare out there, but were unwilling to give out the source as they deemed it to "valuable". So I took it upon myself to convert one of the other freely available pieces of code, and convert it for everyone to use.

I have written the following C++ code. It is based heavily on the JavaScript I obtained directly from the NOAA. I find that many applications may have a use for this information, so feel free to use it to make your own FreeWare tray applications, or any other Utility Programs.

The three main functions are:

C++
CTime GetSunset(double dLat,double dLon,CTime time);
CTime GetSunrise(double dLat,double dLon,CTime time);
CTime GetSolarNoon(double dLon, CTime time);

which take in Latitude and Longitude values as doubles and a CTime with the Date that you want to calculate the SR/SS for. It returns a CTime in UTC.

I have included a non-MFC Tray icon class I have written, with the example. It has the basic functionality to display a tray icon, and can be used in MFC, Win32 and ATL applications. Probably not as good as the newest CSystemTray, but I use it with Win32 to make very small footprint applications!!

I have also added more functionality to the demo, as the way I originally submitted it, it had very little actual functionality. I also added the code so it is converted from UTC as well.

Hope someone finds it useful....

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.


Written By
Software Developer Birdsoft
United States United States
Software Engineer with years of Visual C++ and Objective-C
experience.

Developed Windows Mobile applications including All-In Hold 'Em, Extreme Agenda, and The Dog Ate It.

Now mostly iOS development with the popular title Extreme Agenda as a flagship.

Comments and Discussions

 
QuestionGreat stuff is there a fix for DST? Pin
Robert Valentino6-Apr-11 3:45
Robert Valentino6-Apr-11 3:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.