2020-02-23 16:29:39 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include "datetime.h"
|
2020-11-29 09:57:30 +00:00
|
|
|
#include "settings.h"
|
2020-02-23 16:29:39 +00:00
|
|
|
|
2020-02-27 21:08:12 +00:00
|
|
|
RTC_DATA_ATTR struct datetime_struct now;
|
|
|
|
|
2020-02-23 16:29:39 +00:00
|
|
|
const char *ntpServer = "pool.ntp.org";
|
2020-03-17 18:39:21 +00:00
|
|
|
unsigned long lastUpdate = 0;
|
|
|
|
|
2020-02-27 21:08:12 +00:00
|
|
|
int getNumberOfDays(int month, int year)
|
2020-02-23 16:29:39 +00:00
|
|
|
{
|
2020-02-27 21:08:12 +00:00
|
|
|
// leap year condition, if month is 2
|
|
|
|
if (month == 2)
|
2020-02-23 16:29:39 +00:00
|
|
|
{
|
2020-02-27 21:08:12 +00:00
|
|
|
if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
|
|
|
|
return 29;
|
|
|
|
else
|
|
|
|
return 28;
|
2020-02-23 16:29:39 +00:00
|
|
|
}
|
2020-02-27 21:08:12 +00:00
|
|
|
// months which has 31 days
|
|
|
|
else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
|
|
|
|
return 31;
|
|
|
|
else
|
|
|
|
return 30;
|
2020-02-23 16:29:39 +00:00
|
|
|
}
|
|
|
|
|
2020-03-11 19:36:21 +00:00
|
|
|
bool updateDateTime()
|
2020-02-23 16:29:39 +00:00
|
|
|
{
|
2020-02-27 21:08:12 +00:00
|
|
|
if (!getLocalTime(&now))
|
2020-02-23 16:29:39 +00:00
|
|
|
{
|
|
|
|
Serial.println("Failed to obtain time");
|
2020-02-28 21:02:35 +00:00
|
|
|
return false;
|
2020-02-23 16:29:39 +00:00
|
|
|
}
|
|
|
|
|
2020-02-27 21:08:12 +00:00
|
|
|
//
|
|
|
|
now.days_in_month = getNumberOfDays(now.tm_mon + 1, now.tm_year);
|
|
|
|
now.month_num = now.tm_mon + 1;
|
2020-02-23 16:29:39 +00:00
|
|
|
|
|
|
|
// gives offset of first day of the month with respect to Monday
|
2020-02-27 21:08:12 +00:00
|
|
|
// https://www.tondering.dk/claus/cal/chrweek.php#calcdow
|
2020-02-23 16:29:39 +00:00
|
|
|
uint8_t a = (14 - now.month_num) / 12;
|
2020-02-27 21:08:12 +00:00
|
|
|
uint16_t y = (now.tm_year + 1900) - a;
|
2020-02-23 16:29:39 +00:00
|
|
|
uint16_t m = now.month_num + (12 * a) - 2;
|
|
|
|
now.day_offset = (((1 + y + (y / 4) - (y / 100) + (y / 400) + ((31 * m) / 12)) % 7) + 7) % 7;
|
2020-02-28 21:02:35 +00:00
|
|
|
|
|
|
|
return true;
|
2020-03-11 19:36:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void setupDateTime()
|
|
|
|
{
|
2020-11-30 20:53:58 +00:00
|
|
|
// config time
|
2020-11-29 09:57:30 +00:00
|
|
|
int gmtOffset_sec = NVS.getInt("system.utc");
|
|
|
|
int daylightOffset_sec = NVS.getInt("system.dst");
|
2020-03-17 18:39:21 +00:00
|
|
|
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
|
|
|
|
|
2020-11-30 20:53:58 +00:00
|
|
|
// get current time
|
2020-03-11 19:36:21 +00:00
|
|
|
if (!updateDateTime())
|
|
|
|
{
|
|
|
|
// re-try
|
|
|
|
updateDateTime();
|
|
|
|
}
|
2020-03-17 18:39:21 +00:00
|
|
|
|
|
|
|
Serial.println(&now, "%A, %B %d %Y %H:%M:%S");
|
|
|
|
lastUpdate = millis();
|
2020-03-11 19:36:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void loopDateTime()
|
|
|
|
{
|
2020-03-17 18:39:21 +00:00
|
|
|
if ((millis() - lastUpdate) >= 1000)
|
|
|
|
{
|
|
|
|
lastUpdate = millis();
|
|
|
|
updateDateTime();
|
|
|
|
}
|
2020-02-27 21:08:12 +00:00
|
|
|
}
|