Click here to Skip to main content
13,626,902 members
Click here to Skip to main content
Add your own
alternative version

Stats

107.9K views
3.7K downloads
40 bookmarked
Posted 13 Sep 2008
Licenced Public Domain

C# Class for Calculating Sunrise and Sunset Times

, 13 Sep 2008
Rate this:
Please Sign up or sign in to vote.
A class for calculating sunrise and sunset times, implemented as a thread-safe Singleton

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

Share

About the Author

Zacky Pickholz
CEO Homee
Israel Israel
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).

You may also be interested in...

Comments and Discussions

 
Questioni ported NAA code to a working project. Pin
uaichiuu20-Jun-14 11:05
memberuaichiuu20-Jun-14 11:05 
QuestionI think there is a mistake here.. Pin
Nirosh2-Dec-13 18:47
professionalNirosh2-Dec-13 18:47 
AnswerRe: I think there is a mistake here.. Pin
Ryanshane3-May-17 2:18
memberRyanshane3-May-17 2:18 
QuestionLong, Lat and time offset Pin
shaydr11-Feb-13 22:51
membershaydr11-Feb-13 22:51 
QuestionTimezones Pin
amiller8810-Jan-13 4:50
memberamiller8810-Jan-13 4:50 
QuestionisSunset and isSunrise Pin
DNZ_at28-Dec-11 22:50
memberDNZ_at28-Dec-11 22:50 
GeneralEast/West Bug Pin
sorgal3-Mar-11 0:49
membersorgal3-Mar-11 0:49 
GeneralRe: East/West Bug Pin
Member 1045270025-Feb-14 2:19
memberMember 1045270025-Feb-14 2:19 
QuestionWhat about elevation and twilight? Pin
Mark Kestenbaum24-Feb-10 2:19
memberMark Kestenbaum24-Feb-10 2:19 
GeneralKILLER BUG [modified] Pin
Jools55726-Apr-09 13:09
memberJools55726-Apr-09 13:09 
GeneralRe: KILLER BUG Pin
Zacky Pickholz5-May-09 22:54
memberZacky Pickholz5-May-09 22:54 
GeneralNice but one question [modified] Pin
Rafone16-Mar-09 19:50
memberRafone16-Mar-09 19:50 
GeneralRe: Nice but one question Pin
Zacky Pickholz21-Apr-09 22:51
memberZacky Pickholz21-Apr-09 22:51 
GeneralRe: Nice but one question Pin
PIEBALDconsult22-Apr-09 4:13
memberPIEBALDconsult22-Apr-09 4:13 
GeneralRe: Nice but one question Pin
Rafone22-Apr-09 7:05
memberRafone22-Apr-09 7:05 
GeneralRe: Nice but one question Pin
Zacky Pickholz5-May-09 22:49
memberZacky Pickholz5-May-09 22:49 
GeneralRe: Nice but one question Pin
Rafone6-May-09 2:46
memberRafone6-May-09 2:46 
GeneralRe: Nice but one question Pin
Zacky Pickholz6-May-09 10:29
memberZacky Pickholz6-May-09 10:29 
GeneralRe: Nice but one question Pin
Rafone6-May-09 11:26
memberRafone6-May-09 11:26 
GeneralRe: Nice but one question Pin
Deulis18-Dec-12 7:14
memberDeulis18-Dec-12 7:14 
QuestionWhy not static? Pin
PIEBALDconsult9-Jan-09 17:08
memberPIEBALDconsult9-Jan-09 17:08 
AnswerRe: Why not static? Pin
Zacky Pickholz21-Apr-09 22:41
memberZacky Pickholz21-Apr-09 22:41 
GeneralRe: Why not static? Pin
PIEBALDconsult22-Apr-09 4:02
memberPIEBALDconsult22-Apr-09 4:02 
QuestionHow do I reference this in VB.Net? Pin
pmannino8-Jan-09 23:52
memberpmannino8-Jan-09 23:52 
AnswerRe: How do I reference this in VB.Net? Pin
Zacky Pickholz21-Apr-09 22:43
memberZacky Pickholz21-Apr-09 22:43 

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 | Cookies | Terms of Use | Mobile
Web03 | 2.8.180712.1 | Last Updated 13 Sep 2008
Article Copyright 2008 by Zacky Pickholz
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid