InfiniTime/src/components/datetime/DateTimeController.cpp

149 lines
4.6 KiB
C++
Raw Normal View History

#include "components/datetime/DateTimeController.h"
#include <libraries/log/nrf_log.h>
#include <systemtask/SystemTask.h>
using namespace Pinetime::Controllers;
namespace {
char const* DaysStringShort[] = {"--", "MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"};
2022-03-01 23:33:08 +00:00
char const* DaysStringShortLow[] = {"--", "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"};
}
DateTime::DateTime(Controllers::Settings& settingsController) : settingsController {settingsController} {
}
2021-08-17 23:53:57 +00:00
void DateTime::SetCurrentTime(std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> t) {
this->currentDateTime = t;
UpdateTime(previousSystickCounter); // Update internal state without updating the time
2021-08-17 23:53:57 +00:00
}
void DateTime::SetTime(uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second) {
std::tm tm = {
/* .tm_sec = */ second,
/* .tm_min = */ minute,
/* .tm_hour = */ hour,
/* .tm_mday = */ day,
/* .tm_mon = */ month - 1,
/* .tm_year = */ year - 1900,
};
tm.tm_isdst = -1; // Use DST value from local time zone
currentDateTime = std::chrono::system_clock::from_time_t(std::mktime(&tm));
NRF_LOG_INFO("%d %d %d ", day, month, year);
NRF_LOG_INFO("%d %d %d ", hour, minute, second);
UpdateTime(previousSystickCounter);
2022-01-22 10:47:23 +00:00
systemTask->PushMessage(System::Messages::OnNewTime);
}
2022-11-23 11:41:28 +00:00
void DateTime::SetTimeZone(int8_t timezone, int8_t dst) {
tzOffset = timezone;
dstOffset = dst;
}
void DateTime::UpdateTime(uint32_t systickCounter) {
// Handle systick counter overflow
uint32_t systickDelta = 0;
if (systickCounter < previousSystickCounter) {
systickDelta = 0xffffff - previousSystickCounter;
systickDelta += systickCounter + 1;
} else {
systickDelta = systickCounter - previousSystickCounter;
}
/*
* 1000 ms = 1024 ticks
*/
auto correctedDelta = systickDelta / 1024;
auto rest = systickDelta % 1024;
if (systickCounter >= rest) {
previousSystickCounter = systickCounter - rest;
} else {
previousSystickCounter = 0xffffff - (rest - systickCounter);
}
currentDateTime += std::chrono::seconds(correctedDelta);
uptime += std::chrono::seconds(correctedDelta);
std::time_t currentTime = std::chrono::system_clock::to_time_t(currentDateTime);
localTime = *std::localtime(&currentTime);
auto minute = Minutes();
auto hour = Hours();
2021-11-07 13:12:01 +00:00
if (minute == 0 && !isHourAlreadyNotified) {
2021-11-07 10:50:33 +00:00
isHourAlreadyNotified = true;
2021-11-07 13:12:01 +00:00
if (systemTask != nullptr) {
2021-11-07 10:50:33 +00:00
systemTask->PushMessage(System::Messages::OnNewHour);
2021-11-07 13:12:01 +00:00
}
2021-11-07 10:50:33 +00:00
} else if (minute != 0) {
isHourAlreadyNotified = false;
}
2021-11-07 13:12:01 +00:00
if ((minute == 0 || minute == 30) && !isHalfHourAlreadyNotified) {
2021-11-07 10:50:33 +00:00
isHalfHourAlreadyNotified = true;
2021-11-07 13:12:01 +00:00
if (systemTask != nullptr) {
2021-11-07 10:50:33 +00:00
systemTask->PushMessage(System::Messages::OnNewHalfHour);
2021-11-07 13:12:01 +00:00
}
} else if (minute != 0 && minute != 30) {
2021-11-07 10:50:33 +00:00
isHalfHourAlreadyNotified = false;
}
// Notify new day to SystemTask
if (hour == 0 and not isMidnightAlreadyNotified) {
isMidnightAlreadyNotified = true;
2021-08-17 23:53:57 +00:00
if (systemTask != nullptr)
systemTask->PushMessage(System::Messages::OnNewDay);
} else if (hour != 0) {
isMidnightAlreadyNotified = false;
}
}
const char* DateTime::MonthShortToString() const {
return MonthsString[static_cast<uint8_t>(Month())];
}
const char* DateTime::DayOfWeekShortToString() const {
return DaysStringShort[static_cast<uint8_t>(DayOfWeek())];
}
const char* DateTime::MonthShortToStringLow(Months month) {
return MonthsStringLow[static_cast<uint8_t>(month)];
}
const char* DateTime::DayOfWeekShortToStringLow() const {
return DaysStringShortLow[static_cast<uint8_t>(DayOfWeek())];
2022-03-01 23:33:08 +00:00
}
void DateTime::Register(Pinetime::System::SystemTask* systemTask) {
this->systemTask = systemTask;
}
using ClockType = Pinetime::Controllers::Settings::ClockType;
std::string DateTime::FormattedTime() {
auto hour = Hours();
auto minute = Minutes();
// Return time as a string in 12- or 24-hour format
char buff[9];
if (settingsController.GetClockType() == ClockType::H12) {
2022-05-09 15:16:08 +00:00
uint8_t hour12;
const char* amPmStr;
if (hour < 12) {
hour12 = (hour == 0) ? 12 : hour;
amPmStr = "AM";
} else {
hour12 = (hour == 12) ? 12 : hour - 12;
amPmStr = "PM";
}
sprintf(buff, "%i:%02i %s", hour12, minute, amPmStr);
} else {
sprintf(buff, "%02i:%02i", hour, minute);
}
return std::string(buff);
}