Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Calculate Sunset and Sunrise Time

0.00/5 (No votes)
23 Dec 2011 1  
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 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