Sunset
Public Member Functions | Static Public Attributes | List of all members
SunSet Class Reference

#include <sunset.h>

Public Member Functions

 SunSet ()
 
 SunSet (double, double, int)
 
 SunSet (double, double, double)
 
 ~SunSet ()
 
void setPosition (double, double, int)
 
void setPosition (double, double, double)
 
void setTZOffset (int)
 
void setTZOffset (double)
 
double setCurrentDate (int, int, int)
 
double calcNauticalSunrise () const
 
double calcNauticalSunset () const
 
double calcCivilSunrise () const
 
double calcCivilSunset () const
 
double calcAstronomicalSunrise () const
 
double calcAstronomicalSunset () const
 
double calcCustomSunrise (double) const
 
double calcCustomSunset (double) const
 
double calcSunriseUTC ()
 
double calcSunsetUTC ()
 
double calcSunrise () const
 
double calcSunset () const
 
int moonPhase (int) const
 
int moonPhase () const
 

Static Public Attributes

static constexpr double SUNSET_OFFICIAL = 90.833
 
static constexpr double SUNSET_NAUTICAL = 102.0
 
static constexpr double SUNSET_CIVIL = 96.0
 
static constexpr double SUNSET_ASTONOMICAL = 108.0
 

Detailed Description

This class controls all aspects of the operations. The math is done in private functions, and the public API's allow for returning a sunrise/sunset value for the given coordinates and timezone.

The resulting calculations are relative to midnight of the day you set in the setCurrentDate() function. It does not return a time_t value for delta from the current epoch as that would not make sense as the sunrise/sunset can be calculated thousands of years in the past. The library acts on a day timeframe, and doesn't try to assume anything about any other unit of measurement other than the current set day.

You can instantiate this class a few different ways, depending on your needs. It's possible to set your location one time and forget about doing that again if you don't plan to move. Then you only need to change the date and timezone to get correct data. Or, you can simply create an object with no location or time data and then do that later. This is a good mechanism for the setup()/loop() construct.

The most important thing to remember is to make sure the library knows the exact date and timezone for the sunrise/sunset you are trying to calculate. Not doing so means you are going to have very odd results. It's reasonably easy to know when you've forgotten one or the other by looking at the time the sun would rise and noticing that it is X hours earlier or later. That is, if you get a return of 128 minutes (2:08 AM) past midnight when the sun should rise at 308 (6:08 AM), then you probably forgot to set your EST timezone.

The library also has no idea about daylight savings time. If your timezone changes during the year to account for savings time, you must update your timezone accordingly.

Constructor & Destructor Documentation

◆ SunSet() [1/3]

SunSet::SunSet ( )

Default constructor taking no arguments. It will default all values to zero. It is possible to call calcSunrise() on this type of initialization and it will not fail, but it is unlikely you are at 0,0, TZ=0. This also will not include an initialized date to work from.

◆ SunSet() [2/3]

SunSet::SunSet ( double  lat,
double  lon,
int  tz 
)
Parameters
latDouble Latitude for this object
lonDouble Longitude for this object
tzInteger based timezone for this object

This will create an object for a location with an integer based timezone value. This constructor is a relic of the original design. It is not deprecated, as this is a valid construction, but the double is preferred for correctness.

◆ SunSet() [3/3]

SunSet::SunSet ( double  lat,
double  lon,
double  tz 
)
Parameters
latDouble Latitude for this object
lonDouble Longitude for this object
tzDouble based timezone for this object

This will create an object for a location with a double based timezone value.

◆ ~SunSet()

SunSet::~SunSet ( )

The constructor has no value and does nothing.

Member Function Documentation

◆ calcAstronomicalSunrise()

double SunSet::calcAstronomicalSunrise ( ) const
Returns
Returns the Astronomical sunrise in fractional minutes past midnight

This function will return the Astronomical sunrise in local time for your location

◆ calcAstronomicalSunset()

double SunSet::calcAstronomicalSunset ( ) const
Returns
Returns the Astronomical sunset in fractional minutes past midnight

This function will return the Astronomical sunset in local time for your location

◆ calcCivilSunrise()

double SunSet::calcCivilSunrise ( ) const
Returns
Returns the Civil sunrise in fractional minutes past midnight

This function will return the Civil sunrise in local time for your location

◆ calcCivilSunset()

double SunSet::calcCivilSunset ( ) const
Returns
Returns the Civil sunset in fractional minutes past midnight

This function will return the Civil sunset in local time for your location

◆ calcCustomSunrise()

double SunSet::calcCustomSunrise ( double  angle) const
Parameters
angleThe angle in degrees over the horizon at which to calculate the sunset time
Returns
Returns sunrise at angle degrees in minutes past midnight.

This function will return the sunrise in local time for your location for any angle over the horizon, where < 90 would be above the horizon, and > 90 would be at or below.

◆ calcCustomSunset()

double SunSet::calcCustomSunset ( double  angle) const
Parameters
angleThe angle in degrees over the horizon at which to calculate the sunset time
Returns
Returns sunset at angle degrees in minutes past midnight.

This function will return the sunset in local time for your location for any angle over the horizon, where < 90 would be above the horizon, and > 90 would be at or below.

◆ calcNauticalSunrise()

double SunSet::calcNauticalSunrise ( ) const
Returns
Returns the Nautical sunrise in fractional minutes past midnight

This function will return the Nautical sunrise in local time for your location

◆ calcNauticalSunset()

double SunSet::calcNauticalSunset ( ) const
Returns
Returns the Nautical sunset in fractional minutes past midnight

This function will return the Nautical sunset in local time for your location

◆ calcSunrise()

double SunSet::calcSunrise ( ) const
Returns
Returns local sunrise in minutes past midnight.

This function will return the Official sunrise in local time for your location

◆ calcSunriseUTC()

double SunSet::calcSunriseUTC ( )
Returns
Returns the UTC time when sunrise occurs in the location provided

This is a holdover from the original implementation and to me doesn't seem to be very useful, it's just confusing. This function is deprecated but won't be removed unless that becomes necessary.

◆ calcSunset()

double SunSet::calcSunset ( ) const
Returns
Returns local sunset in minutes past midnight.

This function will return the Official sunset in local time for your location

◆ calcSunsetUTC()

double SunSet::calcSunsetUTC ( )
Returns
Returns the UTC time when sunset occurs in the location provided

This is a holdover from the original implementation and to me doesn't seem to be very useful, it's just confusing. This function is deprecated but won't be removed unless that becomes necessary.

◆ moonPhase() [1/2]

int SunSet::moonPhase ( ) const

Overload to set the moonphase for right now

◆ moonPhase() [2/2]

int SunSet::moonPhase ( int  fromepoch) const
Parameters
fromepochtime_t seconds from epoch to calculate the moonphase for

This is a simple calculation to tell us roughly what the moon phase is locally. It does not give position. It's roughly accurate, but not great.

The return value is 0 to 29, with 0 and 29 being hidden and 14 being full.

◆ setCurrentDate()

double SunSet::setCurrentDate ( int  y,
int  m,
int  d 
)

double SunSet::setCurrentDate(int y, int m, int d)

Parameters
yInteger year, must be 4 digits
mInteger month, not zero based (Jan = 1)
dInteger day of month, not zero based (month starts on day 1)
Returns
Returns the result of the Julian Date conversion if you want to save it

Since these calculations are done based on the Julian Calendar, we must convert our year month day into Julian before we use it. You get the Julian value for free if you want it.

◆ setPosition() [1/2]

void SunSet::setPosition ( double  lat,
double  lon,
double  tz 
)
Parameters
latDouble Latitude value
lonDouble Longitude value
tzDouble Timezone offset

This will set the location the library uses for it's math. The timezone is included in this as it's not valid to call any of the calc functions until you have set a timezone. It is possible to simply call setPosition one time, with a timezone and not use the setTZOffset() function ever, if you never change timezone values.

◆ setPosition() [2/2]

void SunSet::setPosition ( double  lat,
double  lon,
int  tz 
)
Parameters
latDouble Latitude value
lonDouble Longitude value
tzInteger Timezone offset

This will set the location the library uses for it's math. The timezone is included in this as it's not valid to call any of the calc functions until you have set a timezone. It is possible to simply call setPosition one time, with a timezone and not use the setTZOffset() function ever, if you never change timezone values.

This is the old version of the setPosition using an integer timezone, and will not be deprecated. However, it is preferred to use the double version going forward.

◆ setTZOffset() [1/2]

void SunSet::setTZOffset ( double  tz)
Parameters
tzDouble timezone, may be positive or negative

Critical to set your timezone so results are accurate for your time and date. This function is critical to make sure the system works correctly. If you do not set the timezone correctly, the return value will not be correct for your location. Forgetting this will result in return values that may actually be negative in some cases.

◆ setTZOffset() [2/2]

void SunSet::setTZOffset ( int  tz)
Parameters
tzInteger timezone, may be positive or negative

Critical to set your timezone so results are accurate for your time and date. This function is critical to make sure the system works correctly. If you do not set the timezone correctly, the return value will not be correct for your location. Forgetting this will result in return values that may actually be negative in some cases.

This function is a holdover from the previous design using an integer timezone and will not be deprecated. It is preferred to use the setTZOffset(doubble).

Member Data Documentation

◆ SUNSET_ASTONOMICAL

constexpr double SunSet::SUNSET_ASTONOMICAL = 108.0
staticconstexpr

Astronomical sun angle for sunset

◆ SUNSET_CIVIL

constexpr double SunSet::SUNSET_CIVIL = 96.0
staticconstexpr

Civil sun angle for sunset

◆ SUNSET_NAUTICAL

constexpr double SunSet::SUNSET_NAUTICAL = 102.0
staticconstexpr

Nautical sun angle for sunset

◆ SUNSET_OFFICIAL

constexpr double SunSet::SUNSET_OFFICIAL = 90.833
staticconstexpr

Standard sun angle for sunset


The documentation for this class was generated from the following files: