Click here to Skip to main content
13,342,925 members (56,963 online)
Click here to Skip to main content
Add your own
alternative version


31 bookmarked
Posted 18 Jul 2000

Sunrise/Sunset Calculations

, 14 Dec 2000
Rate this:
Please Sign up or sign in to vote.
Code to help calculate sunrise and sunset times
<!-- Download Links -->
  • Download source files - 4 Kb
  • Download demo project - 12 Kb
  • <!-- Main HTML starts here -->


    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 a 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 Main three functions are:

    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....


    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


    About the Author

    Brian Heilman
    Software Developer Birdsoft
    United States United States
    Software Engineer with years of Visual C++ and Objective-C

    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.

    You may also be interested in...

    Comments and Discussions

    SuggestionSome cleanup suggestions Pin
    J Haycox21-May-17 7:28
    memberJ Haycox21-May-17 7:28 
    Bugyou have a bug in minus location Pin
    Member 769785810-Sep-11 23:00
    memberMember 769785810-Sep-11 23:00 
    QuestionGreat stuff is there a fix for DST? Pin
    Robert Valentino6-Apr-11 4:45
    memberRobert Valentino6-Apr-11 4:45 
    Generalhelp Pin
    Member 76555218-Feb-11 6:10
    memberMember 76555218-Feb-11 6:10 
    GeneralMy vote of 2 Pin
    craig.miller26-May-10 14:01
    membercraig.miller26-May-10 14:01 
    GeneralRe: My vote of 2 Pin
    Pat Kujawa24-Mar-11 6:46
    memberPat Kujawa24-Mar-11 6:46 
    QuestionCan i have C# version of this code Pin
    toashokin16-Sep-09 0:01
    membertoashokin16-Sep-09 0:01 
    Please provide c# versoin of the code also.
    AnswerRe: Can i have C# version of this code Pin
    Pat Kujawa24-Mar-11 6:46
    memberPat Kujawa24-Mar-11 6:46 
    QuestionDoes anyone know how to get the Noon Rise / set times from this? Pin
    peterdrozd20-Aug-08 4:52
    memberpeterdrozd20-Aug-08 4:52 
    QuestionDid anyone do a clean up? Pin
    JustASoftwareGuy6623-Dec-06 9:50
    memberJustASoftwareGuy6623-Dec-06 9:50 
    Generalwrong result? help Pin
    anqincmt5-Dec-05 14:50
    memberanqincmt5-Dec-05 14:50 
    GeneralCorrect UTC Offset Pin
    jheddings1-Dec-05 16:22
    memberjheddings1-Dec-05 16:22 
    Questionconverting to Pin
    michal_s_w1-Nov-05 9:31
    membermichal_s_w1-Nov-05 9:31 
    GeneralComparison didn't work Pin
    JWood3-May-05 17:25
    memberJWood3-May-05 17:25 
    GeneralNorth/South &amp; West/East Pin
    Stanislav Khatsko24-Oct-04 4:27
    sussStanislav Khatsko24-Oct-04 4:27 
    GeneralInput Pin
    Piccinano25-Feb-03 4:36
    memberPiccinano25-Feb-03 4:36 
    GeneralRe: Input Pin
    Anonymous21-Jun-03 15:03
    sussAnonymous21-Jun-03 15:03 
    GeneralRe: Input Pin
    Stanislav Khatsko24-Oct-04 4:32
    sussStanislav Khatsko24-Oct-04 4:32 
    GeneralDaylight Savings Pin
    Darroll Walsh9-Apr-02 5:42
    memberDarroll Walsh9-Apr-02 5:42 
    GeneralRe: Daylight Savings Pin
    Darroll Walsh9-Apr-02 6:20
    memberDarroll Walsh9-Apr-02 6:20 
    GeneralRe: Daylight Savings Pin
    Ezel324-Nov-03 3:24
    sussEzel324-Nov-03 3:24 
    GeneralRe: Daylight Savings Pin
    KSharenkov11-Jan-05 19:02
    sussKSharenkov11-Jan-05 19:02 
    GeneralDosen't work ;( Pin
    Tutankhamen2-Jul-01 6:14
    memberTutankhamen2-Jul-01 6:14 
    GeneralRe: Dosen't work ;( Pin
    craig.miller26-May-10 14:02
    membercraig.miller26-May-10 14:02 
    QuestionWhat about height ? Pin
    Miron Ophir30-Dec-00 4:35
    memberMiron Ophir30-Dec-00 4: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.

    Permalink | Advertise | Privacy | Terms of Use | Mobile
    Web04 | 2.8.180111.1 | Last Updated 15 Dec 2000
    Article Copyright 2000 by Brian Heilman
    Everything else Copyright © CodeProject, 1999-2018
    Layout: fixed | fluid