-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jimmy Bjorklund
committed
Oct 4, 2018
1 parent
3380fc8
commit 30040ae
Showing
9 changed files
with
645 additions
and
643 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,76 @@ | ||
#include <math.h> | ||
#include "Date.h" | ||
|
||
Date::Date() : year(0), month(0), day(0) | ||
{ | ||
} | ||
|
||
Date::Date(int y, int m, int d) : year(y), month(m), day(d) | ||
{ | ||
} | ||
|
||
Date::Date(const time_t& time) | ||
{ | ||
struct tm t; | ||
gmtime_r(&time, &t); | ||
year = t.tm_year + 1900; | ||
month = t.tm_mon + 1; | ||
day = t.tm_mday; | ||
} | ||
|
||
Date::Date(double juliandate) | ||
{ | ||
double z = floor(juliandate + 0.5); | ||
double f = (juliandate + 0.5) - z; | ||
double A; | ||
if (z < 2299161) | ||
{ | ||
A = z; | ||
} | ||
else | ||
{ | ||
double alpha = floor((z - 1867216.25) / 36524.25); | ||
A = z + 1 + alpha - floor(alpha / 4); | ||
} | ||
double B = A + 1524; | ||
double C = floor((B - 122.1) / 365.25); | ||
double D = floor(365.25 * C); | ||
double E = floor((B - D) / 30.6001); | ||
day = B - D - floor(30.6001 * E) + f; | ||
month = (E < 14) ? E - 1 : E - 13; | ||
year = ((month > 2) ? C - 4716 : C - 4715); | ||
} | ||
|
||
double Date::toJulianDate() const | ||
{ | ||
double y = year; | ||
double m = month; | ||
double d = day; | ||
double A = floor(y / 100); | ||
double B = 2 - A + floor(A / 4); | ||
double JD = floor(365.25 * (y + 4716)) + floor(30.6001 * (m + 1)) + d + B - 1524.5; | ||
return JD; | ||
#include <math.h> | ||
#include <string.h> | ||
#include "Date.h" | ||
|
||
Date::Date() : year(0), month(0), day(0) | ||
{ | ||
} | ||
|
||
Date::Date(int y, int m, int d) : year(y), month(m), day(d) | ||
{ | ||
} | ||
|
||
Date::Date(const time_t& time) | ||
{ | ||
struct tm t; | ||
gmtime_r(&time, &t); | ||
year = t.tm_year + 1900; | ||
month = t.tm_mon + 1; | ||
day = t.tm_mday; | ||
} | ||
|
||
Date::Date(double juliandate) | ||
{ | ||
double z = floor(juliandate + 0.5); | ||
double f = (juliandate + 0.5) - z; | ||
double A; | ||
if (z < 2299161) | ||
{ | ||
A = z; | ||
} | ||
else | ||
{ | ||
double alpha = floor((z - 1867216.25) / 36524.25); | ||
A = z + 1 + alpha - floor(alpha / 4); | ||
} | ||
double B = A + 1524; | ||
double C = floor((B - 122.1) / 365.25); | ||
double D = floor(365.25 * C); | ||
double E = floor((B - D) / 30.6001); | ||
day = B - D - floor(30.6001 * E) + f; | ||
month = (E < 14) ? E - 1 : E - 13; | ||
year = ((month > 2) ? C - 4716 : C - 4715); | ||
} | ||
|
||
double Date::toJulianDate() const | ||
{ | ||
double y = year; | ||
double m = month; | ||
double d = day; | ||
double A = floor(y / 100); | ||
double B = 2 - A + floor(A / 4); | ||
double JD = floor(365.25 * (y + 4716)) + floor(30.6001 * (m + 1)) + d + B - 1524.5; | ||
return JD; | ||
} | ||
|
||
time_t Date::julianDateToTimeT(double jd) | ||
{ | ||
return (jd - 2440587.5) * 86400.0; | ||
} | ||
|
||
time_t Date::ToTimeT(const Date& d, double time) | ||
{ | ||
struct tm tm; | ||
memset(&tm, 0, sizeof(struct tm)); | ||
tm.tm_year = d.year - 1900; | ||
tm.tm_mon = d.month - 1; | ||
tm.tm_mday = d.day; | ||
time_t date = mktime(&tm); // Make time creates the time assuming the current time, remove gmtoff later! | ||
|
||
int min = floor(time); | ||
double sec = time - floor(time); | ||
date += (min * 60) + (sec * 60.0); | ||
|
||
date += tm.tm_gmtoff - (tm.tm_isdst * 3600); | ||
return date; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,20 @@ | ||
#ifndef __SUN_CALC_DATE_H__ | ||
#define __SUN_CALC_DATE_H__ | ||
#include <time.h> | ||
|
||
class Date | ||
{ | ||
public: | ||
int year; //!< Year in format YYYY e.g 2000 | ||
int month; //!< Month of the year 1-12. | ||
int day; //!< Day of the month 1-32. | ||
Date(); | ||
Date(int year, int month, int day); | ||
Date(double); //!< Juliandate | ||
Date(const time_t& time); | ||
double toJulianDate() const; | ||
}; | ||
|
||
#ifndef __SUN_CALC_DATE_H__ | ||
#define __SUN_CALC_DATE_H__ | ||
#include <time.h> | ||
|
||
class Date | ||
{ | ||
public: | ||
int year; //!< Year in format YYYY e.g 2000 | ||
int month; //!< Month of the year 1-12. | ||
int day; //!< Day of the month 1-32. | ||
Date(); | ||
Date(int year, int month, int day); | ||
Date(double); //!< Juliandate | ||
Date(const time_t& time); | ||
double toJulianDate() const; | ||
static time_t julianDateToTimeT(double jd); //!< Converst julian date to time_t | ||
static time_t ToTimeT(const Date& d, double time); //!< Combine julian timestamp and date to create a new time_t | ||
}; | ||
|
||
#endif // __SUN_CALC_DATE_H__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,29 @@ | ||
# SunLight | ||
|
||
This class library gives you varios sunrise and sunset values depening on your date and longiture and latitude. | ||
It bases its implementation on the NOAA Solar Calculator. | ||
|
||
|
||
### Credits: | ||
This is strongly influenced by the javascript library solar-calc | ||
https://github.com/jonhester/solar-calc | ||
|
||
### Reference: | ||
https://www.esrl.noaa.gov/gmd/grad/solcalc/index.html | ||
|
||
|
||
### Sample: | ||
|
||
```cpp | ||
double latitude = 59.370272; | ||
double longitude = 18.000767; | ||
time_t now = time(NULL); | ||
|
||
SunLight sunLight(Date(now), latitude, longitude); | ||
|
||
time_t sunrise = sunLight.sunrise(); | ||
time_t sunset = sunLight.sunset(); | ||
|
||
std::cout << "sunrise: " << asctime( gmtime(&sunrise)) << std::endl; | ||
std::cout << "sunset: " << asctime( gmtime(&sunrise)) << std::endl; | ||
# SunLight | ||
|
||
This class library gives you varios sunrise and sunset values depening on your date and longiture and latitude. | ||
It bases its implementation on the NOAA Solar Calculator. | ||
|
||
|
||
### Credits: | ||
This is strongly influenced by the javascript library solar-calc | ||
https://github.com/jonhester/solar-calc | ||
|
||
### Reference: | ||
https://www.esrl.noaa.gov/gmd/grad/solcalc/index.html | ||
|
||
|
||
### Sample: | ||
|
||
```cpp | ||
double latitude = 59.370272; | ||
double longitude = 18.000767; | ||
time_t now = time(NULL); | ||
|
||
SunLight sunLight(Date(now), latitude, longitude); | ||
|
||
time_t sunrise = sunLight.sunrise(); | ||
time_t sunset = sunLight.sunset(); | ||
|
||
std::cout << "sunrise: " << asctime( gmtime(&sunrise)) << std::endl; | ||
std::cout << "sunset: " << asctime( gmtime(&sunrise)) << std::endl; | ||
``` |
Oops, something went wrong.