2019-12-28 08:34:50 -05:00
|
|
|
#include "DateTimeController.h"
|
2020-01-18 07:56:25 -05:00
|
|
|
#include <date/date.h>
|
|
|
|
#include <libraries/log/nrf_log.h>
|
2021-04-02 11:33:49 -04:00
|
|
|
#include <systemtask/SystemTask.h>
|
2019-12-28 08:34:50 -05:00
|
|
|
|
|
|
|
using namespace Pinetime::Controllers;
|
|
|
|
|
2021-10-10 10:08:19 -04:00
|
|
|
namespace {
|
|
|
|
char const* DaysStringShort[] = {"--", "MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"};
|
|
|
|
char const* MonthsString[] = {"--", "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"};
|
|
|
|
char const* MonthsStringLow[] = {"--", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
|
|
|
|
}
|
|
|
|
|
2021-08-17 19:53:57 -04:00
|
|
|
void DateTime::SetCurrentTime(std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> t) {
|
|
|
|
this->currentDateTime = t;
|
2021-09-19 11:42:50 -04:00
|
|
|
UpdateTime(previousSystickCounter); // Update internal state without updating the time
|
2021-08-17 19:53:57 -04:00
|
|
|
}
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
void DateTime::SetTime(
|
|
|
|
uint16_t year, uint8_t month, uint8_t day, uint8_t dayOfWeek, uint8_t hour, uint8_t minute, uint8_t second, uint32_t systickCounter) {
|
|
|
|
std::tm tm = {
|
|
|
|
/* .tm_sec = */ second,
|
|
|
|
/* .tm_min = */ minute,
|
|
|
|
/* .tm_hour = */ hour,
|
|
|
|
/* .tm_mday = */ day,
|
|
|
|
/* .tm_mon = */ month - 1,
|
|
|
|
/* .tm_year = */ year - 1900,
|
2020-03-02 14:13:30 -05:00
|
|
|
};
|
|
|
|
tm.tm_isdst = -1; // Use DST value from local time zone
|
2021-04-18 13:28:14 -04:00
|
|
|
currentDateTime = std::chrono::system_clock::from_time_t(std::mktime(&tm));
|
2020-01-18 07:56:25 -05:00
|
|
|
|
|
|
|
NRF_LOG_INFO("%d %d %d ", day, month, year);
|
|
|
|
NRF_LOG_INFO("%d %d %d ", hour, minute, second);
|
|
|
|
previousSystickCounter = systickCounter;
|
2020-03-02 14:13:30 -05:00
|
|
|
|
2020-01-18 07:56:25 -05:00
|
|
|
UpdateTime(systickCounter);
|
|
|
|
NRF_LOG_INFO("* %d %d %d ", this->hour, this->minute, this->second);
|
|
|
|
NRF_LOG_INFO("* %d %d %d ", this->day, this->month, this->year);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DateTime::UpdateTime(uint32_t systickCounter) {
|
2020-01-31 14:01:33 -05:00
|
|
|
// Handle systick counter overflow
|
2020-01-18 07:56:25 -05:00
|
|
|
uint32_t systickDelta = 0;
|
2021-04-18 13:28:14 -04:00
|
|
|
if (systickCounter < previousSystickCounter) {
|
2020-01-18 07:56:25 -05:00
|
|
|
systickDelta = 0xffffff - previousSystickCounter;
|
|
|
|
systickDelta += systickCounter + 1;
|
|
|
|
} else {
|
|
|
|
systickDelta = systickCounter - previousSystickCounter;
|
|
|
|
}
|
|
|
|
|
2020-01-31 14:01:33 -05:00
|
|
|
/*
|
2021-04-18 13:28:14 -04:00
|
|
|
* 1000 ms = 1024 ticks
|
|
|
|
*/
|
2020-01-31 14:01:33 -05:00
|
|
|
auto correctedDelta = systickDelta / 1024;
|
2021-04-18 13:28:14 -04:00
|
|
|
auto rest = (systickDelta - (correctedDelta * 1024));
|
|
|
|
if (systickCounter >= rest) {
|
2020-01-31 14:01:33 -05:00
|
|
|
previousSystickCounter = systickCounter - rest;
|
|
|
|
} else {
|
|
|
|
previousSystickCounter = 0xffffff - (rest - systickCounter);
|
|
|
|
}
|
|
|
|
|
2020-06-07 14:04:43 -04:00
|
|
|
currentDateTime += std::chrono::seconds(correctedDelta);
|
|
|
|
uptime += std::chrono::seconds(correctedDelta);
|
2020-01-18 07:56:25 -05:00
|
|
|
|
|
|
|
auto dp = date::floor<date::days>(currentDateTime);
|
2021-04-18 13:28:14 -04:00
|
|
|
auto time = date::make_time(currentDateTime - dp);
|
2020-01-18 07:56:25 -05:00
|
|
|
auto yearMonthDay = date::year_month_day(dp);
|
|
|
|
|
2021-07-24 07:01:11 -04:00
|
|
|
year = static_cast<int>(yearMonthDay.year());
|
|
|
|
month = static_cast<Months>(static_cast<unsigned>(yearMonthDay.month()));
|
|
|
|
day = static_cast<unsigned>(yearMonthDay.day());
|
2020-01-18 07:56:25 -05:00
|
|
|
dayOfWeek = static_cast<Days>(date::weekday(yearMonthDay).iso_encoding());
|
|
|
|
|
|
|
|
hour = time.hours().count();
|
|
|
|
minute = time.minutes().count();
|
|
|
|
second = time.seconds().count();
|
2021-04-02 11:33:49 -04:00
|
|
|
|
|
|
|
// Notify new day to SystemTask
|
2021-04-18 13:28:14 -04:00
|
|
|
if (hour == 0 and not isMidnightAlreadyNotified) {
|
2021-04-02 11:33:49 -04:00
|
|
|
isMidnightAlreadyNotified = true;
|
2021-08-17 19:53:57 -04:00
|
|
|
if (systemTask != nullptr)
|
2021-06-06 09:56:03 -04:00
|
|
|
systemTask->PushMessage(System::Messages::OnNewDay);
|
2021-04-02 11:33:49 -04:00
|
|
|
} else if (hour != 0) {
|
|
|
|
isMidnightAlreadyNotified = false;
|
|
|
|
}
|
2019-12-28 08:34:50 -05:00
|
|
|
}
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
const char* DateTime::MonthShortToString() {
|
2021-10-10 10:08:19 -04:00
|
|
|
return MonthsString[static_cast<uint8_t>(month)];
|
2021-02-24 14:40:24 -05:00
|
|
|
}
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
const char* DateTime::DayOfWeekShortToString() {
|
2021-10-10 10:08:19 -04:00
|
|
|
return DaysStringShort[static_cast<uint8_t>(dayOfWeek)];
|
2021-07-09 11:32:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* DateTime::MonthShortToStringLow(Months month) {
|
2021-10-10 10:08:19 -04:00
|
|
|
return MonthsStringLow[static_cast<uint8_t>(month)];
|
2021-02-24 14:40:24 -05:00
|
|
|
}
|
|
|
|
|
2021-06-06 09:56:03 -04:00
|
|
|
void DateTime::Register(Pinetime::System::SystemTask* systemTask) {
|
|
|
|
this->systemTask = systemTask;
|
|
|
|
}
|
|
|
|
|