From 959778d770aedb56d773e7fde07c8c967d5b35eb Mon Sep 17 00:00:00 2001 From: Riku Isokoski Date: Thu, 23 Feb 2023 19:15:23 +0200 Subject: [PATCH] DateTimeController: Use std::tm for storing date --- .../datetime/DateTimeController.cpp | 26 +++++++----------- src/components/datetime/DateTimeController.h | 27 +++++++++---------- 2 files changed, 22 insertions(+), 31 deletions(-) diff --git a/src/components/datetime/DateTimeController.cpp b/src/components/datetime/DateTimeController.cpp index 7ebb7b19..9e9fb6e4 100644 --- a/src/components/datetime/DateTimeController.cpp +++ b/src/components/datetime/DateTimeController.cpp @@ -1,5 +1,4 @@ #include "components/datetime/DateTimeController.h" -#include #include #include @@ -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(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(¤tTime); - year = static_cast(yearMonthDay.year()); - month = static_cast(static_cast(yearMonthDay.month())); - day = static_cast(yearMonthDay.day()); - dayOfWeek = static_cast(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(month)]; + return MonthsString[static_cast(Month())]; } const char* DateTime::DayOfWeekShortToString() const { - return DaysStringShort[static_cast(dayOfWeek)]; + return DaysStringShort[static_cast(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(dayOfWeek)]; + return DaysStringShortLow[static_cast(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) { diff --git a/src/components/datetime/DateTimeController.h b/src/components/datetime/DateTimeController.h index b68d18ef..8e3fa8ff 100644 --- a/src/components/datetime/DateTimeController.h +++ b/src/components/datetime/DateTimeController.h @@ -2,6 +2,7 @@ #include #include +#include #include #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(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(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;