DateTimeController: Use std::tm for storing date

This commit is contained in:
Riku Isokoski 2023-02-23 19:15:23 +02:00
parent b63bb798ac
commit 959778d770
2 changed files with 22 additions and 31 deletions

View file

@ -1,5 +1,4 @@
#include "components/datetime/DateTimeController.h"
#include <date/date.h>
#include <libraries/log/nrf_log.h>
#include <systemtask/SystemTask.h>
@ -37,8 +36,6 @@ void DateTime::SetTime(uint16_t year, uint8_t month, uint8_t day, uint8_t hour,
NRF_LOG_INFO("%d %d %d ", hour, minute, second);
UpdateTime(previousSystickCounter);
NRF_LOG_INFO("* %d %d %d ", this->hour, this->minute, this->second);
NRF_LOG_INFO("* %d %d %d ", this->day, this->month, this->year);
systemTask->PushMessage(System::Messages::OnNewTime);
}
@ -72,18 +69,11 @@ void DateTime::UpdateTime(uint32_t systickCounter) {
currentDateTime += std::chrono::seconds(correctedDelta);
uptime += std::chrono::seconds(correctedDelta);
auto dp = date::floor<date::days>(currentDateTime);
auto time = date::make_time(currentDateTime - dp);
auto yearMonthDay = date::year_month_day(dp);
std::time_t currentTime = std::chrono::system_clock::to_time_t(currentDateTime);
localTime = *std::localtime(&currentTime);
year = static_cast<int>(yearMonthDay.year());
month = static_cast<Months>(static_cast<unsigned>(yearMonthDay.month()));
day = static_cast<unsigned>(yearMonthDay.day());
dayOfWeek = static_cast<Days>(date::weekday(yearMonthDay).iso_encoding());
hour = time.hours().count();
minute = time.minutes().count();
second = time.seconds().count();
auto minute = Minutes();
auto hour = Hours();
if (minute == 0 && !isHourAlreadyNotified) {
isHourAlreadyNotified = true;
@ -114,11 +104,11 @@ void DateTime::UpdateTime(uint32_t systickCounter) {
}
const char* DateTime::MonthShortToString() const {
return MonthsString[static_cast<uint8_t>(month)];
return MonthsString[static_cast<uint8_t>(Month())];
}
const char* DateTime::DayOfWeekShortToString() const {
return DaysStringShort[static_cast<uint8_t>(dayOfWeek)];
return DaysStringShort[static_cast<uint8_t>(DayOfWeek())];
}
const char* DateTime::MonthShortToStringLow(Months month) {
@ -126,7 +116,7 @@ const char* DateTime::MonthShortToStringLow(Months month) {
}
const char* DateTime::DayOfWeekShortToStringLow() const {
return DaysStringShortLow[static_cast<uint8_t>(dayOfWeek)];
return DaysStringShortLow[static_cast<uint8_t>(DayOfWeek())];
}
void DateTime::Register(Pinetime::System::SystemTask* systemTask) {
@ -136,6 +126,8 @@ void DateTime::Register(Pinetime::System::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) {

View file

@ -2,6 +2,7 @@
#include <cstdint>
#include <chrono>
#include <ctime>
#include <string>
#include "components/settings/Settings.h"
@ -47,31 +48,35 @@ namespace Pinetime {
void UpdateTime(uint32_t systickCounter);
uint16_t Year() const {
return year;
return 1900 + localTime.tm_year;
}
Months Month() const {
return month;
return static_cast<Months>(localTime.tm_mon + 1);
}
uint8_t Day() const {
return day;
return localTime.tm_mday;
}
Days DayOfWeek() const {
return dayOfWeek;
int daysSinceSunday = localTime.tm_wday;
if (daysSinceSunday == 0) {
return Days::Sunday;
}
return static_cast<Days>(daysSinceSunday);
}
uint8_t Hours() const {
return hour;
return localTime.tm_hour;
}
uint8_t Minutes() const {
return minute;
return localTime.tm_min;
}
uint8_t Seconds() const {
return second;
return localTime.tm_sec;
}
/*
@ -132,13 +137,7 @@ namespace Pinetime {
std::string FormattedTime();
private:
uint16_t year = 0;
Months month = Months::Unknown;
uint8_t day = 0;
Days dayOfWeek = Days::Unknown;
uint8_t hour = 0;
uint8_t minute = 0;
uint8_t second = 0;
std::tm localTime;
int8_t tzOffset = 0;
int8_t dstOffset = 0;