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>
|
2019-12-28 08:34:50 -05:00
|
|
|
|
|
|
|
using namespace Pinetime::Controllers;
|
|
|
|
|
|
|
|
|
2020-01-18 07:56:25 -05: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) {
|
|
|
|
|
|
|
|
currentDateTime = {};
|
|
|
|
currentDateTime += date::years( year-1970);
|
|
|
|
currentDateTime += date::days( day - 1);
|
|
|
|
currentDateTime += date::months( month - 1);
|
|
|
|
|
|
|
|
currentDateTime += std::chrono::hours(hour);
|
|
|
|
currentDateTime += std::chrono::minutes (minute);
|
|
|
|
currentDateTime += std::chrono::seconds (second);
|
|
|
|
|
|
|
|
currentDateTime -= std::chrono::hours(3); // TODO WHYYYY?
|
|
|
|
NRF_LOG_INFO("%d %d %d ", day, month, year);
|
|
|
|
NRF_LOG_INFO("%d %d %d ", hour, minute, second);
|
|
|
|
previousSystickCounter = systickCounter;
|
|
|
|
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;
|
|
|
|
if(systickCounter < previousSystickCounter) {
|
|
|
|
systickDelta = 0xffffff - previousSystickCounter;
|
|
|
|
systickDelta += systickCounter + 1;
|
|
|
|
} else {
|
|
|
|
systickDelta = systickCounter - previousSystickCounter;
|
|
|
|
}
|
|
|
|
|
2020-01-31 14:01:33 -05:00
|
|
|
/*
|
|
|
|
* 1000 ms = 1024 ticks
|
|
|
|
*/
|
|
|
|
auto correctedDelta = systickDelta / 1024;
|
|
|
|
auto rest = (systickDelta - (correctedDelta*1024));
|
|
|
|
if(systickCounter >= rest) {
|
|
|
|
previousSystickCounter = systickCounter - rest;
|
|
|
|
} else {
|
|
|
|
previousSystickCounter = 0xffffff - (rest - systickCounter);
|
|
|
|
}
|
|
|
|
|
2020-02-10 15:05:33 -05:00
|
|
|
currentDateTime += std::chrono::milliseconds (systickDelta*10);
|
2020-01-18 07:56:25 -05:00
|
|
|
|
|
|
|
auto dp = date::floor<date::days>(currentDateTime);
|
|
|
|
auto time = date::make_time(currentDateTime-dp);
|
|
|
|
auto yearMonthDay = date::year_month_day(dp);
|
|
|
|
|
|
|
|
year = (int)yearMonthDay.year();
|
|
|
|
month = static_cast<Months>((unsigned)yearMonthDay.month());
|
|
|
|
day = (unsigned)yearMonthDay.day();
|
|
|
|
dayOfWeek = static_cast<Days>(date::weekday(yearMonthDay).iso_encoding());
|
|
|
|
|
|
|
|
hour = time.hours().count();
|
|
|
|
minute = time.minutes().count();
|
|
|
|
second = time.seconds().count();
|
2019-12-28 08:34:50 -05:00
|
|
|
}
|
|
|
|
|