Skip to content

Commit 959778d

Browse files
committed
DateTimeController: Use std::tm for storing date
1 parent b63bb79 commit 959778d

2 files changed

Lines changed: 22 additions & 31 deletions

File tree

src/components/datetime/DateTimeController.cpp

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "components/datetime/DateTimeController.h"
2-
#include <date/date.h>
32
#include <libraries/log/nrf_log.h>
43
#include <systemtask/SystemTask.h>
54

@@ -37,8 +36,6 @@ void DateTime::SetTime(uint16_t year, uint8_t month, uint8_t day, uint8_t hour,
3736
NRF_LOG_INFO("%d %d %d ", hour, minute, second);
3837

3938
UpdateTime(previousSystickCounter);
40-
NRF_LOG_INFO("* %d %d %d ", this->hour, this->minute, this->second);
41-
NRF_LOG_INFO("* %d %d %d ", this->day, this->month, this->year);
4239

4340
systemTask->PushMessage(System::Messages::OnNewTime);
4441
}
@@ -72,18 +69,11 @@ void DateTime::UpdateTime(uint32_t systickCounter) {
7269
currentDateTime += std::chrono::seconds(correctedDelta);
7370
uptime += std::chrono::seconds(correctedDelta);
7471

75-
auto dp = date::floor<date::days>(currentDateTime);
76-
auto time = date::make_time(currentDateTime - dp);
77-
auto yearMonthDay = date::year_month_day(dp);
72+
std::time_t currentTime = std::chrono::system_clock::to_time_t(currentDateTime);
73+
localTime = *std::localtime(&currentTime);
7874

79-
year = static_cast<int>(yearMonthDay.year());
80-
month = static_cast<Months>(static_cast<unsigned>(yearMonthDay.month()));
81-
day = static_cast<unsigned>(yearMonthDay.day());
82-
dayOfWeek = static_cast<Days>(date::weekday(yearMonthDay).iso_encoding());
83-
84-
hour = time.hours().count();
85-
minute = time.minutes().count();
86-
second = time.seconds().count();
75+
auto minute = Minutes();
76+
auto hour = Hours();
8777

8878
if (minute == 0 && !isHourAlreadyNotified) {
8979
isHourAlreadyNotified = true;
@@ -114,19 +104,19 @@ void DateTime::UpdateTime(uint32_t systickCounter) {
114104
}
115105

116106
const char* DateTime::MonthShortToString() const {
117-
return MonthsString[static_cast<uint8_t>(month)];
107+
return MonthsString[static_cast<uint8_t>(Month())];
118108
}
119109

120110
const char* DateTime::DayOfWeekShortToString() const {
121-
return DaysStringShort[static_cast<uint8_t>(dayOfWeek)];
111+
return DaysStringShort[static_cast<uint8_t>(DayOfWeek())];
122112
}
123113

124114
const char* DateTime::MonthShortToStringLow(Months month) {
125115
return MonthsStringLow[static_cast<uint8_t>(month)];
126116
}
127117

128118
const char* DateTime::DayOfWeekShortToStringLow() const {
129-
return DaysStringShortLow[static_cast<uint8_t>(dayOfWeek)];
119+
return DaysStringShortLow[static_cast<uint8_t>(DayOfWeek())];
130120
}
131121

132122
void DateTime::Register(Pinetime::System::SystemTask* systemTask) {
@@ -136,6 +126,8 @@ void DateTime::Register(Pinetime::System::SystemTask* systemTask) {
136126
using ClockType = Pinetime::Controllers::Settings::ClockType;
137127

138128
std::string DateTime::FormattedTime() {
129+
auto hour = Hours();
130+
auto minute = Minutes();
139131
// Return time as a string in 12- or 24-hour format
140132
char buff[9];
141133
if (settingsController.GetClockType() == ClockType::H12) {

src/components/datetime/DateTimeController.h

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <cstdint>
44
#include <chrono>
5+
#include <ctime>
56
#include <string>
67
#include "components/settings/Settings.h"
78

@@ -47,31 +48,35 @@ namespace Pinetime {
4748
void UpdateTime(uint32_t systickCounter);
4849

4950
uint16_t Year() const {
50-
return year;
51+
return 1900 + localTime.tm_year;
5152
}
5253

5354
Months Month() const {
54-
return month;
55+
return static_cast<Months>(localTime.tm_mon + 1);
5556
}
5657

5758
uint8_t Day() const {
58-
return day;
59+
return localTime.tm_mday;
5960
}
6061

6162
Days DayOfWeek() const {
62-
return dayOfWeek;
63+
int daysSinceSunday = localTime.tm_wday;
64+
if (daysSinceSunday == 0) {
65+
return Days::Sunday;
66+
}
67+
return static_cast<Days>(daysSinceSunday);
6368
}
6469

6570
uint8_t Hours() const {
66-
return hour;
71+
return localTime.tm_hour;
6772
}
6873

6974
uint8_t Minutes() const {
70-
return minute;
75+
return localTime.tm_min;
7176
}
7277

7378
uint8_t Seconds() const {
74-
return second;
79+
return localTime.tm_sec;
7580
}
7681

7782
/*
@@ -132,13 +137,7 @@ namespace Pinetime {
132137
std::string FormattedTime();
133138

134139
private:
135-
uint16_t year = 0;
136-
Months month = Months::Unknown;
137-
uint8_t day = 0;
138-
Days dayOfWeek = Days::Unknown;
139-
uint8_t hour = 0;
140-
uint8_t minute = 0;
141-
uint8_t second = 0;
140+
std::tm localTime;
142141
int8_t tzOffset = 0;
143142
int8_t dstOffset = 0;
144143

0 commit comments

Comments
 (0)