Click here to Skip to main content
15,867,568 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.1K   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

 
SuggestionSome cleanup suggestions Pin
J Haycox21-May-17 6:28
J Haycox21-May-17 6:28 
The code works pretty good as is for US cities provided you pass West longitude as a positive value (an unexpected convention). You could modify the code to invert longitude if the more common convention of negative for west longitude is desired.

The trig to compute "double timeGMT" seems OK too (based on tests for a few cities around the world). However the code to convert to local time has some flaws.

Update 5/24/14: After posting a suggested modification a few days ago I realized it was a bit quirky. Replacing the commented out lines with the code shown below in the three public functions seems to return correct local time as the author intended:

//	double dHour = timeGMT / 60;
//	int iHour = (int)dHour;
//	double dMinute = 60 * (dHour - iHour);
//	int iMinute = (int)dMinute;
//	double dSecond = 60 * (dMinute - iMinute);
//	int iSecond = (int)dSecond;
	
//	_tzset();
//	int iHrToTimeZone = _timezone / 3600; //returns a difference value in seconds
//	iHour -= iHrToTimeZone;
//	CTime NewTime(time.GetYear(),time.GetMonth(),time.GetDay(),iHour,iMinute,iSecond);

	// Convert timeGMT to integer seconds (may be negative - e.g. Ho Chi Minh City)
	__time64_t tSeconds = (__time64_t)(timeGMT * 60.);

	__time64_t tt64;
	_time64(&tt64);  // Start with arbitrary __time64_t (current time)
	tt64 /= 60 * 60 * 24; // Convert to whole days
	tt64 *= 60 * 60 * 24; // Convert to seconds rounded to whole days
	tt64 += tSeconds; // Add computed time of day
	CTime tog(tt64);  // CTime with arbitray date and the computed time of day

	CTime NewTime(time.GetYear(),time.GetMonth(),time.GetDay(), tog.GetHour(), tog.GetMinute(), tog.GetSecond());
	return NewTime;


In testing remember to pass West longitude as positive and East as negative (or change the code to follow the more common convention). Also remember that times are local times for the timezone set for the computer running the code (not the target location).

The locations I tested are:

TestLocations testLocations[] = {
	{39.7684, 86.1581, -4, _T("Indianapolis, Indiana")},	// Note: West longitudes are positive
	{10.8231, -106.6297, +7, _T("Ho Chi Minh City")},
	{64.8378, 147.7164, -8, _T("Fairbanks Alaska")},
	{48.8566, -2.3522, +2, _T("Paris France")},
	{51.4769, -0.0005, +1, _T("Greenwich, London, UK")},
	{-33.8688, -151.2093, +10, _T("Sydney, Australia")},
	{-34.6037, 58.3816, -3, _T("Buenos Aires, Argentina")},
};

CSunRiseSet agrees with Google sunrise / sunset for these locations within a couple of minutes except Fairbanks which is around 4 minutes different.



modified 24-May-17 14:45pm.

Bugyou have a bug in minus location Pin
Member 769785810-Sep-11 22:00
Member 769785810-Sep-11 22:00 
QuestionGreat stuff is there a fix for DST? Pin
Robert Valentino6-Apr-11 3:45
Robert Valentino6-Apr-11 3:45 
Generalhelp Pin
Member 76555218-Feb-11 5:10
Member 76555218-Feb-11 5:10 
GeneralMy vote of 2 Pin
craig.miller26-May-10 13:01
craig.miller26-May-10 13:01 
GeneralRe: My vote of 2 Pin
Pat Kujawa24-Mar-11 5:46
Pat Kujawa24-Mar-11 5:46 
QuestionCan i have C# version of this code Pin
toashokin15-Sep-09 23:01
toashokin15-Sep-09 23:01 
AnswerRe: Can i have C# version of this code Pin
Pat Kujawa24-Mar-11 5:46
Pat Kujawa24-Mar-11 5:46 
QuestionDoes anyone know how to get the Noon Rise / set times from this? Pin
peterdrozd20-Aug-08 3:52
peterdrozd20-Aug-08 3:52 
QuestionDid anyone do a clean up? Pin
H.SmithMi23-Dec-06 8:50
H.SmithMi23-Dec-06 8:50 
Generalwrong result? help Pin
anqincmt5-Dec-05 13:50
anqincmt5-Dec-05 13:50 
GeneralCorrect UTC Offset Pin
jheddings1-Dec-05 15:22
jheddings1-Dec-05 15:22 
Questionconverting to vb.net? Pin
michal_s_w1-Nov-05 8:31
michal_s_w1-Nov-05 8:31 
GeneralComparison didn't work Pin
JWood3-May-05 16:25
JWood3-May-05 16:25 
GeneralNorth/South & West/East Pin
Stanislav Khatsko24-Oct-04 3:27
sussStanislav Khatsko24-Oct-04 3:27 
GeneralInput Pin
Piccinano25-Feb-03 3:36
Piccinano25-Feb-03 3:36 
GeneralRe: Input Pin
Anonymous21-Jun-03 14:03
Anonymous21-Jun-03 14:03 
GeneralRe: Input Pin
Stanislav Khatsko24-Oct-04 3:32
sussStanislav Khatsko24-Oct-04 3:32 
GeneralDaylight Savings Pin
9-Apr-02 4:42
suss9-Apr-02 4:42 
GeneralRe: Daylight Savings Pin
9-Apr-02 5:20
suss9-Apr-02 5:20 
GeneralRe: Daylight Savings Pin
Ezel324-Nov-03 2:24
sussEzel324-Nov-03 2:24 
GeneralRe: Daylight Savings Pin
KSharenkov11-Jan-05 18:02
sussKSharenkov11-Jan-05 18:02 
GeneralDosen't work ;( Pin
Tutankhamen2-Jul-01 5:14
Tutankhamen2-Jul-01 5:14 
GeneralRe: Dosen't work ;( Pin
craig.miller26-May-10 13:02
craig.miller26-May-10 13:02 
QuestionWhat about height ? Pin
30-Dec-00 3:35
suss30-Dec-00 3:35 

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.