2020-03-28 13:45:20 +01:00
|
|
|
#include "src/dependencies/timezone/Timezone.h"
|
2020-03-25 09:00:55 +01:00
|
|
|
#include "wled.h"
|
2020-03-31 02:38:08 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Acquires time from NTP server
|
|
|
|
*/
|
2020-03-28 13:45:20 +01:00
|
|
|
|
2020-03-26 09:12:55 +01:00
|
|
|
TimeChangeRule UTCr = {Last, Sun, Mar, 1, 0}; // UTC
|
|
|
|
Timezone tzUTC(UTCr, UTCr);
|
2016-12-31 00:38:51 +01:00
|
|
|
|
2020-03-26 09:12:55 +01:00
|
|
|
TimeChangeRule BST = {Last, Sun, Mar, 1, 60}; // British Summer Time
|
|
|
|
TimeChangeRule GMT = {Last, Sun, Oct, 2, 0}; // Standard Time
|
|
|
|
Timezone tzUK(BST, GMT);
|
|
|
|
|
|
|
|
TimeChangeRule CEST = {Last, Sun, Mar, 2, 120}; //Central European Summer Time
|
|
|
|
TimeChangeRule CET = {Last, Sun, Oct, 3, 60}; //Central European Standard Time
|
|
|
|
Timezone tzEUCentral(CEST, CET);
|
|
|
|
|
|
|
|
TimeChangeRule EEST = {Last, Sun, Mar, 3, 180}; //Central European Summer Time
|
|
|
|
TimeChangeRule EET = {Last, Sun, Oct, 4, 120}; //Central European Standard Time
|
|
|
|
Timezone tzEUEastern(EEST, EET);
|
|
|
|
|
|
|
|
TimeChangeRule EDT = {Second, Sun, Mar, 2, -240 }; //Daylight time = UTC - 4 hours
|
|
|
|
TimeChangeRule EST = {First, Sun, Nov, 2, -300 }; //Standard time = UTC - 5 hours
|
|
|
|
Timezone tzUSEastern(EDT, EST);
|
|
|
|
|
|
|
|
TimeChangeRule CDT = {Second, Sun, Mar, 2, -300 }; //Daylight time = UTC - 5 hours
|
|
|
|
TimeChangeRule CST = {First, Sun, Nov, 2, -360 }; //Standard time = UTC - 6 hours
|
|
|
|
Timezone tzUSCentral(CDT, CST);
|
|
|
|
|
|
|
|
Timezone tzCASaskatchewan(CST, CST); //Central without DST
|
|
|
|
|
|
|
|
TimeChangeRule MDT = {Second, Sun, Mar, 2, -360 }; //Daylight time = UTC - 6 hours
|
|
|
|
TimeChangeRule MST = {First, Sun, Nov, 2, -420 }; //Standard time = UTC - 7 hours
|
|
|
|
Timezone tzUSMountain(MDT, MST);
|
|
|
|
|
|
|
|
Timezone tzUSArizona(MST, MST); //Mountain without DST
|
|
|
|
|
|
|
|
TimeChangeRule PDT = {Second, Sun, Mar, 2, -420 }; //Daylight time = UTC - 7 hours
|
|
|
|
TimeChangeRule PST = {First, Sun, Nov, 2, -480 }; //Standard time = UTC - 8 hours
|
|
|
|
Timezone tzUSPacific(PDT, PST);
|
|
|
|
|
|
|
|
TimeChangeRule ChST = {Last, Sun, Mar, 1, 480}; // China Standard Time = UTC + 8 hours
|
|
|
|
Timezone tzChina(ChST, ChST);
|
|
|
|
|
|
|
|
TimeChangeRule JST = {Last, Sun, Mar, 1, 540}; // Japan Standard Time = UTC + 9 hours
|
|
|
|
Timezone tzJapan(JST, JST);
|
|
|
|
|
|
|
|
TimeChangeRule AEDT = {Second, Sun, Oct, 2, 660 }; //Daylight time = UTC + 11 hours
|
|
|
|
TimeChangeRule AEST = {First, Sun, Apr, 3, 600 }; //Standard time = UTC + 10 hours
|
|
|
|
Timezone tzAUEastern(AEDT, AEST);
|
|
|
|
|
|
|
|
TimeChangeRule NZDT = {Second, Sun, Sep, 2, 780 }; //Daylight time = UTC + 13 hours
|
|
|
|
TimeChangeRule NZST = {First, Sun, Apr, 3, 720 }; //Standard time = UTC + 12 hours
|
|
|
|
Timezone tzNZ(NZDT, NZST);
|
|
|
|
|
|
|
|
TimeChangeRule NKST = {Last, Sun, Mar, 1, 510}; //Pyongyang Time = UTC + 8.5 hours
|
|
|
|
Timezone tzNK(NKST, NKST);
|
|
|
|
|
|
|
|
TimeChangeRule IST = {Last, Sun, Mar, 1, 330}; // India Standard Time = UTC + 5.5 hours
|
|
|
|
Timezone tzIndia(IST, IST);
|
|
|
|
|
2020-05-03 01:48:23 +02:00
|
|
|
TimeChangeRule ACST = {First, Sun, Apr, 3, 570}; //Australian Central Standard = UTC + 9.5 hours
|
|
|
|
TimeChangeRule ACDT = {First, Sun, Oct, 2, 630}; //Australian Central Daylight = UTC + 10.5 hours
|
|
|
|
Timezone tzAUNorthern(ACST, ACST);
|
|
|
|
Timezone tzAUSouthern(ACDT, ACST);
|
|
|
|
|
2020-03-26 09:12:55 +01:00
|
|
|
// Pick your timezone from here.
|
2020-05-03 01:48:23 +02:00
|
|
|
Timezone* timezones[] = {&tzUTC, &tzUK, &tzEUCentral, &tzEUEastern, &tzUSEastern, &tzUSCentral, &tzUSMountain, &tzUSArizona, &tzUSPacific, &tzChina, &tzJapan, &tzAUEastern, &tzNZ, &tzNK, &tzIndia, &tzCASaskatchewan, &tzAUNorthern, &tzAUSouthern};
|
2018-03-06 23:47:08 +01:00
|
|
|
|
2016-12-29 00:03:58 +01:00
|
|
|
void handleNetworkTime()
|
|
|
|
{
|
2019-10-18 13:26:39 +02:00
|
|
|
if (ntpEnabled && ntpConnected && millis() - ntpLastSyncTime > 50000000L && WLED_CONNECTED)
|
2017-02-07 16:02:27 +01:00
|
|
|
{
|
|
|
|
if (millis() - ntpPacketSentTime > 10000)
|
|
|
|
{
|
|
|
|
sendNTPPacket();
|
|
|
|
ntpPacketSentTime = millis();
|
|
|
|
}
|
|
|
|
if (checkNTPResponse())
|
|
|
|
{
|
|
|
|
ntpLastSyncTime = millis();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void sendNTPPacket()
|
|
|
|
{
|
2019-12-04 02:01:47 +01:00
|
|
|
if (!ntpServerIP.fromString(ntpServerName)) //see if server is IP or domain
|
|
|
|
{
|
|
|
|
#ifdef ESP8266
|
|
|
|
WiFi.hostByName(ntpServerName, ntpServerIP, 750);
|
|
|
|
#else
|
|
|
|
WiFi.hostByName(ntpServerName, ntpServerIP);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-03-11 17:57:06 +01:00
|
|
|
DEBUG_PRINTLN("send NTP");
|
|
|
|
byte pbuf[NTP_PACKET_SIZE];
|
|
|
|
memset(pbuf, 0, NTP_PACKET_SIZE);
|
|
|
|
|
|
|
|
pbuf[0] = 0b11100011; // LI, Version, Mode
|
|
|
|
pbuf[1] = 0; // Stratum, or type of clock
|
|
|
|
pbuf[2] = 6; // Polling Interval
|
|
|
|
pbuf[3] = 0xEC; // Peer Clock Precision
|
2017-02-07 16:02:27 +01:00
|
|
|
// 8 bytes of zero for Root Delay & Root Dispersion
|
2019-03-11 17:57:06 +01:00
|
|
|
pbuf[12] = 49;
|
|
|
|
pbuf[13] = 0x4E;
|
|
|
|
pbuf[14] = 49;
|
|
|
|
pbuf[15] = 52;
|
2017-02-07 16:02:27 +01:00
|
|
|
|
|
|
|
ntpUdp.beginPacket(ntpServerIP, 123); //NTP requests are to port 123
|
2019-03-11 17:57:06 +01:00
|
|
|
ntpUdp.write(pbuf, NTP_PACKET_SIZE);
|
2017-02-07 16:02:27 +01:00
|
|
|
ntpUdp.endPacket();
|
|
|
|
}
|
|
|
|
|
2018-03-14 13:16:28 +01:00
|
|
|
bool checkNTPResponse()
|
2017-02-07 16:02:27 +01:00
|
|
|
{
|
|
|
|
int cb = ntpUdp.parsePacket();
|
|
|
|
if (cb) {
|
2019-03-11 17:57:06 +01:00
|
|
|
DEBUG_PRINT("NTP recv, l=");
|
2017-02-07 16:02:27 +01:00
|
|
|
DEBUG_PRINTLN(cb);
|
2019-03-11 17:57:06 +01:00
|
|
|
byte pbuf[NTP_PACKET_SIZE];
|
|
|
|
ntpUdp.read(pbuf, NTP_PACKET_SIZE); // read the packet into the buffer
|
2017-02-07 16:02:27 +01:00
|
|
|
|
2019-03-11 17:57:06 +01:00
|
|
|
unsigned long highWord = word(pbuf[40], pbuf[41]);
|
|
|
|
unsigned long lowWord = word(pbuf[42], pbuf[43]);
|
2017-02-07 16:02:27 +01:00
|
|
|
if (highWord == 0 && lowWord == 0) return false;
|
|
|
|
|
|
|
|
unsigned long secsSince1900 = highWord << 16 | lowWord;
|
|
|
|
|
|
|
|
DEBUG_PRINT("Unix time = ");
|
2018-04-11 23:50:35 +02:00
|
|
|
unsigned long epoch = secsSince1900 - 2208988799UL; //subtract 70 years -1sec (on avg. more precision)
|
2017-02-07 16:02:27 +01:00
|
|
|
setTime(epoch);
|
|
|
|
DEBUG_PRINTLN(epoch);
|
2018-01-14 00:53:16 +01:00
|
|
|
if (countdownTime - now() > 0) countdownOverTriggered = false;
|
2017-02-07 16:02:27 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2016-12-29 00:03:58 +01:00
|
|
|
}
|
|
|
|
|
2018-03-06 23:47:08 +01:00
|
|
|
void updateLocalTime()
|
|
|
|
{
|
2018-03-14 00:25:54 +01:00
|
|
|
unsigned long tmc = now()+ utcOffsetSecs;
|
2020-08-04 15:32:54 +02:00
|
|
|
localTime = timezones[currentTimezone]->toLocal(tmc);
|
2018-03-06 23:47:08 +01:00
|
|
|
}
|
|
|
|
|
2019-03-16 02:09:37 +01:00
|
|
|
void getTimeString(char* out)
|
2016-12-29 00:03:58 +01:00
|
|
|
{
|
2018-03-06 23:47:08 +01:00
|
|
|
updateLocalTime();
|
2020-08-04 15:32:54 +02:00
|
|
|
byte hr = hour(localTime);
|
2019-03-25 22:51:38 +01:00
|
|
|
if (useAMPM)
|
|
|
|
{
|
|
|
|
if (hr > 11) hr -= 12;
|
|
|
|
if (hr == 0) hr = 12;
|
|
|
|
}
|
2020-08-04 15:32:54 +02:00
|
|
|
sprintf(out,"%i-%i-%i, %i:%s%i:%s%i",year(localTime), month(localTime), day(localTime),
|
|
|
|
hr,(minute(localTime)<10)?"0":"",minute(localTime),
|
|
|
|
(second(localTime)<10)?"0":"",second(localTime));
|
2019-03-11 17:57:06 +01:00
|
|
|
if (useAMPM)
|
|
|
|
{
|
2020-08-04 15:32:54 +02:00
|
|
|
strcat(out,(hour(localTime) > 11)? " PM":" AM");
|
2019-03-11 17:57:06 +01:00
|
|
|
}
|
2016-12-29 00:03:58 +01:00
|
|
|
}
|
|
|
|
|
2018-03-06 23:47:08 +01:00
|
|
|
void setCountdown()
|
|
|
|
{
|
|
|
|
countdownTime = timezones[currentTimezone]->toUTC(getUnixTime(countdownHour, countdownMin, countdownSec, countdownDay, countdownMonth, countdownYear));
|
|
|
|
if (countdownTime - now() > 0) countdownOverTriggered = false;
|
|
|
|
}
|
|
|
|
|
2018-02-20 22:29:48 +01:00
|
|
|
//returns true if countdown just over
|
|
|
|
bool checkCountdown()
|
|
|
|
{
|
2020-01-01 01:04:54 +01:00
|
|
|
unsigned long n = now();
|
2020-08-04 15:32:54 +02:00
|
|
|
if (countdownMode) localTime = countdownTime - n + utcOffsetSecs;
|
2020-01-01 01:04:54 +01:00
|
|
|
if (n > countdownTime) {
|
2020-08-04 15:32:54 +02:00
|
|
|
if (countdownMode) localTime = n - countdownTime + utcOffsetSecs;
|
2020-01-01 01:04:54 +01:00
|
|
|
if (!countdownOverTriggered)
|
|
|
|
{
|
|
|
|
if (macroCountdown != 0) applyMacro(macroCountdown);
|
|
|
|
countdownOverTriggered = true;
|
|
|
|
return true;
|
|
|
|
}
|
2018-02-20 22:29:48 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-31 00:09:44 +01:00
|
|
|
byte weekdayMondayFirst()
|
|
|
|
{
|
2020-08-04 15:32:54 +02:00
|
|
|
byte wd = weekday(localTime) -1;
|
2019-01-31 00:09:44 +01:00
|
|
|
if (wd == 0) wd = 7;
|
|
|
|
return wd;
|
|
|
|
}
|
|
|
|
|
2018-09-22 22:49:24 +02:00
|
|
|
void checkTimers()
|
|
|
|
{
|
2020-08-04 15:32:54 +02:00
|
|
|
if (lastTimerMinute != minute(localTime)) //only check once a new minute begins
|
2018-09-22 22:49:24 +02:00
|
|
|
{
|
2020-08-04 15:32:54 +02:00
|
|
|
lastTimerMinute = minute(localTime);
|
2018-09-22 22:49:24 +02:00
|
|
|
for (uint8_t i = 0; i < 8; i++)
|
|
|
|
{
|
|
|
|
if (timerMacro[i] != 0
|
2020-08-04 15:32:54 +02:00
|
|
|
&& (timerHours[i] == hour(localTime) || timerHours[i] == 24) //if hour is set to 24, activate every hour
|
|
|
|
&& timerMinutes[i] == minute(localTime)
|
2019-01-31 00:09:44 +01:00
|
|
|
&& (timerWeekday[i] & 0x01) //timer is enabled
|
|
|
|
&& timerWeekday[i] >> weekdayMondayFirst() & 0x01) //timer should activate at current day of week
|
2018-09-22 22:49:24 +02:00
|
|
|
{
|
|
|
|
applyMacro(timerMacro[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|