Click here to Skip to main content
Licence CPOL
First Posted 26 Feb 2009
Views 38,491
Downloads 1,079
Bookmarked 75 times

Calculate Sunset and Sunrise Time

By | 23 Dec 2011 | Article
This class implements an algorithm found in Almanac for Computers (1990) published by Nautical Almanac Office.

Introduction

The SunTime class calculates the sunrise and sunset time at a given location / global position. This class implements an algorithm found in Almanac for Computers (1990) published by Nautical Almanac Office.

Background

In home control applications, the time of sunrise and sunset are handy for taking certain actions such as closing blinds.

And so began the search for an accurate calculation of these times. The search ended with a piece of pseudo code from the Almanac for Computers that also proved to work very accurately.

Using the Code

The source code contains a simple console application that shows you the basic usage of the SunTime class.

The class contains three constructor overrides:

// Create a new SunTime object with default settings.
public SunTime()

/// <summary>
/// Create a new SunTime object for the current date.
/// </summary>
/// <param name="latitude">Global position latitude in degrees. 
/// Latitude is positive for North and negative for South.</param>
/// <param name="longitude">Global position longitude in degrees. 
/// Longitude is positive for East and negative for West.</param>
/// <param name="utcOffset">The local UTC offset 
/// (f.e. +1 for Brussel, Kopenhagen, Madrid, Paris).</param>
/// <param name="daylightChanges">The daylight saving settings to use.</param>
public SunTime(double latitude, double longitude, 
		double utcOffset, DaylightTime daylightChanges)

/// <summary>
/// Create a new SunTime object for the given date.
/// </summary>
/// <param name="latitude">Global position latitude in degrees. 
/// Latitude is positive for North and negative for South.</param>
/// <param name="longitude">Global position longitude in degrees. 
/// Longitude is positive for East and negative for West.</param>
/// <param name="utcOffset">The local UTC offset 
/// (f.e. +1 for Brussel, Kopenhagen, Madrid, Paris).</param>
/// <param name="daylightChanges">The daylight saving settings to use.</param>
/// <param name="date">The date to calculate the set- and risetime for.</param>
public SunTime(double latitude, double longitude, 
		double utcOffset, DaylightTime daylightChanges, DateTime date)

After creating an instance of the class, you can modify all parameters by setting the Properties. The new RiseTime and SetTime will update immediately.

/// <summary>
/// Gets or sets the global position longitude in degrees.
/// Longitude is positive for East and negative for West.
/// </summary>
public double Longitude

/// <summary>
/// Gets or sets the global position latitude in degrees.
/// Latitude is positive for North and negative for South.
/// </summary>
public double Latitude

/// <summary>
/// Gets or sets the date where the RiseTime and SetTime apply to.
/// </summary>
public DateTime Date

/// <summary>
/// Gets or sets the local UTC offset in hours.
/// F.e.: +1 for Brussel, Kopenhagen, Madrid, Paris.
/// See Windows Time settings for a list of offsets.
/// </summary>
public double UtcOffset

/// <summary>
/// Gets or sets the zenith used in the sunrise / sunset time calculation.
/// </summary>
public ZenithValue Zenith

/// <summary>
/// Gets or sets the daylight saving range to use in sunrise / sunset time calculation.
/// </summary>
public DaylightTime DaylightChanges

Daylight saving

You can pass a daylight saving range in the constructor. Or pass null to ignore the daylight saving.

To get your local daylight saving range, you can use TimeZone.CurrentTimeZone.GetDaylightChanges(DateTime.Now.Year)

Ranges from other timezones can be read from the Windows registry. VBDT has an article on this subject 'TimeZoneInfo' on CodeProject. Or you could use the Win32 API function GetTimeZoneInformation.

History

  • 27-Feb-2009: Uploaded the source code
  • 27-Feb-2009: Added the Zenith property
  • 04-Mar-2009: Changed UtcOffset into a double and added the DaylightChanges property
  • 23-Dec-2011: Corrects the official zenith value

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Wouter Huysentruit



Belgium Belgium

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionTnx! Pinmember10der3:42 27 Dec '11  
Bugone correction PinmemberJeff Sumey12:08 22 Dec '11  
GeneralRe: one correction PinmemberWouter Huysentruit21:48 22 Dec '11  
GeneralThanks ! Pinmemberjyl20026:16 12 Dec '10  
GeneralDifferent UTC Offset Pinmembertoashokin2:08 16 Sep '09  
GeneralRe: Different UTC Offset PinmemberWouter Huysentruit6:03 16 Sep '09  
GeneralProblems with high latitudes PinmemberRussHuffman9:13 22 Jun '09  
GeneralRe: Problems with high latitudes [modified] PinmemberRussHuffman9:15 22 Jun '09  
GeneralATMEGA32 Pinmemberjaht0:40 13 Mar '09  
GeneralRe: ATMEGA32 PinmemberWouter Huysentruit2:07 13 Mar '09  
GeneralPosition Pinmemberviliam11:11 9 Mar '09  
GeneralRe: Position PinmemberWouter Huysentruit7:24 10 Mar '09  
GeneralRe: Position Pinmemberviliam8:14 10 Mar '09  
GeneralGreat [modified] PinmemberHamed Mosavi23:24 2 Mar '09  
GeneralRe: Great PinmemberWouter Huysentruit5:36 3 Mar '09  
You're right. The utcOffset should be changed to a double, together with its property.
 
Thanks for your comment.
GeneralPerfect Pinmemberx89310:03 28 Feb '09  
Generalmy vote of 3 PinmemberAdrian Pasik9:16 27 Feb '09  
GeneralRe: my vote of 3 PinmemberWouter Huysentruit14:24 27 Feb '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web02 | 2.5.120604.1 | Last Updated 23 Dec 2011
Article Copyright 2009 by Wouter Huysentruit
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid