Click here to Skip to main content
6,629,885 members and growing! (22,793 online)
Email Password   helpLost your password?
Languages » C# » Date / Time     Beginner License: A Public Domain dedication

C# Class for Calculating Sunrise and Sunset Times

By Zacky Pickholz

A class for calculating sunrise and sunset times, implemented as a thread-safe Singleton
C#, .NET, WinXP, Vista, Win32, Win64, Dev
Posted:13 Sep 2008
Views:12,753
Bookmarked:25 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
8 votes for this article.
Popularity: 3.61 Rating: 4.00 out of 5
1 vote, 12.5%
1

2
1 vote, 12.5%
3
2 votes, 25.0%
4
4 votes, 50.0%
5

Introduction

This simple C# Singleton class calculates the sunrise and sunset times for a given date.

Background

After searching for a simple and decent implementation for calculating sunrise and sunset times for given dates, and trying several implementations that were either too complicated to migrate to C# or simply not working, I found a simple yet working JavaScript implementation here.

I migrated the code to C#, tweaking it a little so that it provides accurate calculations. Also, I wrapped it as a Singleton class (assuming multiple instances would not be required for such a class) and added a lock to the main calculation method, in order to make it thread safe (via blocking).

Using the Code

The singleton class SunTimes can be called from anywhere in your code by calling SunTimes.Instance.

The class contains a single method, with one overload, named CalculateSunRiseSetTimes(). You simply call this method, provide it with three input parameters: latitude and longitude of the desired location, and date for which to calculate. Moreover, you need to pass it four (4) output (ref) parameters: riseTime (sunrise time), setTime (sunset time), isSunrise (does the sun rise that day at all?) and isSunset (does the sun set that day at all?).

The method returns a boolean value if the calculation succeeds (it will fail, if the time zone and longitude are incompatible).

Here is a sample usage of the class:

...

DateTime date = DateTime.Today;
bool isSunrise = false;
bool isSunset = false;
DateTime sunrise = DateTime.Now;
DateTime sunset = DateTime.Now;

// Print out the Sunrise and Sunset times for the next 20 days
for (int i=0; i<20; i++)
{
                                                // Coordinates of Tel-Aviv
     SunTimes.Instance.CalculateSunRiseSetTimes(new SunTimes.LatitudeCoords
                                   (32, 4, 0, SunTimes.LatitudeCoords.Direction.North),
                                                new SunTimes.LongitudeCoords
                                   (34, 46, 0, SunTimes.LongitudeCoords.Direction.East),
                                                date, ref sunrise, ref sunset, 
			                     ref isSunrise, ref isSunset);

     Debug.Print(date + '': Sunrise @'' + sunrise.ToString('HH:mm') + ''  
				Sunset @'' + sunset.ToString(''HH:mm''));

     date = date.AddDays(1); // Move to the next day
}

...

Points of Interest

This implementation is not in particular fancy, not is it the slickest design, but hey - it does the work (at least as far as I've tested it). I will be happy to get any comments (not on its design, please, only if you detect any actual bugs).

History

  • 14-Sep-2008: Uploaded the class implementation

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication

About the Author

Zacky Pickholz


Member
With 15 years of experience in the IT/High-Tech industry, as developer, team-leader and product manager.

Fields of expertise include: Networks, Security, Gaming, Embedded/Real Time and Web applications.

I have written many applications, both in structural languages (C, Pascal, Fortran, VB) and OO languages (C++, C#, J2SE/J2EE) as well as in scripting/interpreted/web languages (HTML, JavaScript, LUA Script, PHP, VBScript).

Today, Co-Founder and CEO of Robo Smart Solutions (www.robo.co.il).
Occupation: CEO
Company: Homee
Location: Israel Israel

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 25 (Total in Forum: 25) (Refresh)FirstPrevNext
GeneralKILLER BUG [modified] PinmemberJools55714:09 26 Apr '09  
GeneralRe: KILLER BUG PinmemberZacky Pickholz23:54 5 May '09  
GeneralNice but one question [modified] PinmemberRafone20:50 16 Mar '09  
GeneralRe: Nice but one question PinmemberZacky Pickholz23:51 21 Apr '09  
GeneralRe: Nice but one question PinmemberPIEBALDconsult5:13 22 Apr '09  
GeneralRe: Nice but one question PinmemberRafone8:05 22 Apr '09  
GeneralRe: Nice but one question PinmemberZacky Pickholz23:49 5 May '09  
GeneralRe: Nice but one question PinmemberRafone3:46 6 May '09  
GeneralRe: Nice but one question PinmemberZacky Pickholz11:29 6 May '09  
GeneralRe: Nice but one question PinmemberRafone12:26 6 May '09  
GeneralWhy not static? PinmemberPIEBALDconsult18:08 9 Jan '09  
GeneralRe: Why not static? PinmemberZacky Pickholz23:41 21 Apr '09  
GeneralRe: Why not static? PinmemberPIEBALDconsult5:02 22 Apr '09  
GeneralHow do I reference this in VB.Net? Pinmemberpmannino0:52 9 Jan '09  
GeneralRe: How do I reference this in VB.Net? PinmemberZacky Pickholz23:43 21 Apr '09  
GeneralProblem getting sunrise and sunset to output. Pinmembernewb2vb6:05 4 Nov '08  
GeneralRe: Problem getting sunrise and sunset to output. PinmemberMedlan6:52 4 Nov '08  
Generalerror Pinmemberharrifer9:19 31 Oct '08  
GeneralRe: error Pinmemberrsam_london16:19 31 Oct '08  
GeneralProblem with rounding the UtcOffset Pinmemberrsam_london18:11 29 Oct '08  
GeneralTwilight times? Pinmemberacasia214:07 9 Oct '08  
GeneralRe: Twilight times? PinmemberKenv7:54 31 Oct '08  
GeneralRe: Twilight times? PinmemberRafone21:26 16 Mar '09  
GeneralGreat code BUT ... need to fix ... Pinmemberon_your_615:33 2 Oct '08  
GeneralRe: Great code BUT ... need to fix ... PinmemberZacky Pickholz2:17 7 Oct '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 13 Sep 2008
Editor: Deeksha Shenoy
Copyright 2008 by Zacky Pickholz
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project