Date, Time and the Sun?
The Sun is the basis of our Time!
My simpleLib sDatTimeSun.h library. All possibilities at a glance
(see the red
dots).
Time (interval) synchronization from NTP time servers (use my
sWiFiFull.h) or user/RTC input.
In
addition to the standard usage options for date, time and sun, you can also
control a Sun Tracker with SunRise/Set and SolPos. For example, a solar parabool,
solar panel, sun following tubular daylighting or, go crazy, a sundial with a
pointer instead of shadow.
Use my
sDateTimeSun.h library examples as a guide!
HINT: Combine it with my sWiFiFull.h
library examples with these selectable options:
(STA, AP, STA-AP, STA-IP, AP-IP, STA-DNS, LONG-RANGE, ESP-NOW, OTA, NTP-TIME,
WEATHER, WEB-SOCKET)
sDateTimeSun.h library examples:
sTime.ino (included: UTC, Local, Zone, DST and Sun Rise/Set)
sTimeCalc.ino (some of the many possibilities)
sTimeM5Stick.ino (see M5 image above, also usable with other modules and displays)
sEquiSol.ino (Equinox: March, September / Solstice: June, December)
sSolPos.ino (lat: 52.09, lon: 5.13, 6 dec 2018 12:00: azi: 172.71°, alt: 15.16°)
User date
names and calendar options:Standard English but you can set your own language and calendar options in void setup().
Calendar options: first week of the year and first day of the week.
void sSetWeek(const char _wk = _week); // change strftime week codeDate, Time and Sun functions:
void sSetWday(const char _wd = _wday); // change strftime weekday code
void sSetMonthDayStr(const char _mL[12][21], const char _mS[12][11], const char _dL[7][21], const char _dS[7][11]); // change long and short month and day names
(Structures after the functions)
void sSetTz(char* tz); // set Time ZoneThe used Structures:
bool sGetNTP(char * _timeZone); // get time from server
void sSetEpoch(sEpoch _ep); // set time manual, use sMakeTime() uint64_t sTimestampInt(sEpoch _ep = sEp); // timestamp in milliseconds // if _ep NULL then the current time is used
String sTimestampStr(sEpoch _ep = sEp); // timestampString in milliseconds
// if _ep NULL then the current time is used
sEpoch sMakeTime(int64_t _msec); // time from a timestamp in milliseconds
sEpoch sMakeTime(int8_t h, int8_t m, int8_t s, int16_t ms, int8_t D, int8_t M, int16_t Y); // calc a timesEpoch sNow(sEpoch _ep = sEp); // current time or current time +/- t-offset in seconds
// if _ep NULL then the current time is used
sEpoch sAbsDiff(sEpoch _epX, sEpoch _epY = sEp); // absolute difference between _X and _Y
// if _epY NULL then the current time is used
sSMHD sTimeSplit(time_t t); // split a seconds time in days, hours, minutes and seconds
sDateTime sTimeDate(bool _gmt, sEpoch _ep = sEp); // get a set of local/gmt time and date values
// if _ep NULL then the current time is used
time_t now(time_t t = 0); // as ep.sec from sEpoch ep = sNow();
// if t NOT NULL then then t is a +/- offset to the current time // time_t t = now(12*60*24); // t = tomorrow at the same time
/** get GMT + zone + dst **/ // if t NULL then the current time is used else t is used
uint16_t year(time_t t = now());
uint8_t month(time_t t = now());
uint8_t day(time_t t = now());
uint8_t hour(time_t t = now());
uint8_t minute(time_t t = now());
uint8_t second(time_t t = now());
uint8_t weekday(time_t t = now());
uint8_t yweek(time_t t = now());
uint16_t yday(time_t t = now());
/** get GMT **/
// if t NULL then the current time is used else t is used
uint16_t yearGmt(time_t t = now());
uint8_t monthGmt(time_t t = now());
uint8_t dayGmt(time_t t = now());
uint8_t hourGmt(time_t t = now());
uint8_t minuteGmt(time_t t = now());
uint8_t secondGmt(time_t t = now());
uint8_t weekdayGmt(time_t t = now());
uint8_t yweekGmt(time_t t = now());
uint16_t ydayGmt(time_t t = now());
/*************/ sRiseSet sSun(time_t t, double lat, double lon, bool gmt = false); // calc rise and set times bool sDaylight(time_t t, sRiseSet rs); // is it sun down time sEquiSol sCalcEquiSol(uint16_t Y); // Equinox and Solstice calculation: // spring, summer, autumn, winter (as UTC/GMT time) sSolPosOut sSolPos(sSolPosIn _sp); // Calculates the True North location of the Sun in the sky // for any given location, day, and time. // Valid from the year 1950 to 2050
struct sEpoch { // timestamp and milliseconds time_t sec; // UNIX timestamp int16_t msec; // milliseconds 000-999 }; struct sRiseSet { // output: SunRise and SunSet local or UTC time_t rise, // SunRise local or UTC set; // SunSet local or UTC }; struct sEquiSol { // output: Equinox and Solstice (as UTC/GMT time) sEpoch spring, // Equinox, March summer, // Solstice, June autumn, // Equinox, September winter; // Solstice, December }; /************ position of the Sun (Solar tracker) ************/ // use it in combination with SunRise and SunSet from "sRiseSet sSun(...)" struct sSolPosIn { // input: True North location of the Sun in the sky time_t time; // local time double temp, // you can get it with my sOpenweathermap.h lib.(free key on // "http://api.openweathermap.org/data/2.5/weather?") pres, // you can get it with my sOpenweathermap.h lib. zone, // seconds: use "sDateTime.offset" or a "local-UTC" time lat, // location North/South parallel to the Equator lon; // location East/West of the 0°Prime Meridian // (The meridian of the British Royal Observatory in Greenwich) }; struct sSolPosOut { // output: True North location of the Sun in the sky double azimuth, // the angle of the sun around the horizon, usually measured from // true north and increasing eastward altitude; // the angle between the sun and the observer's local horizon }; /*************************************************************/ struct sDateTime { // local/UTC time and date // set by function: sDateTime sTimeDate(bool _gmt, sEpoch _ep) time_t sec; // UNIX timestamp (it's a int32_t) // GMT < 0, MIN: 13 December 1901 20:45:52 // GMT == 0, : 1 January 1970 00:00:00 // GMT > 0, MAX: 19 January 2038 03:14:07 int8_t second, // seconds 00-59 minute, // minutes 00-59 hour, // hours 00-23 day, // day 01-31 month, // month 01-12 week, // week 01-53 wday; // weekday 1-7 int16_t msec, // milliseconds 000-999 year, // year 1901-2038 yday; // yearday 001-366 // timezone and DST time_t zone, // seconds dst, // seconds offset; // zone + dst (sec - offset = UTC/GMT) // Long/Short month/day names char monthL[21], // month Long name monthS[11], // month Short name dayL[21], // day Long name dayS[11]; // day Short name }; struct sSMHD { // split timestamp to time and days // set by function: sSMHD sTimeSplit(time_t _t) int8_t seconds, // seconds 00-59 minutes, // seconds 00-59 hours; // hours 00-23 int16_t days; // days 0-n };