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:
public SunTime()
public SunTime(double latitude, double longitude,
double utcOffset, DaylightTime daylightChanges)
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.
public double Longitude
public double Latitude
public DateTime Date
public double UtcOffset
public ZenithValue Zenith
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