Skip to content

Commit f1651c8

Browse files
FintasticManJF002
authored andcommitted
datetime: Set the default year to the year during compile
1 parent 8a2ee43 commit f1651c8

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

src/components/datetime/DateTimeController.cpp

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,29 @@
77
using namespace Pinetime::Controllers;
88

99
namespace {
10-
char const* DaysStringShort[] = {"--", "MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"};
11-
char const* DaysStringShortLow[] = {"--", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
12-
char const* MonthsString[] = {"--", "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"};
13-
char const* MonthsStringLow[] = {"--", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
10+
constexpr const char* const DaysStringShort[] = {"--", "MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"};
11+
constexpr const char* const DaysStringShortLow[] = {"--", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
12+
constexpr const char* const MonthsString[] = {"--", "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"};
13+
constexpr const char* const MonthsStringLow[] =
14+
{"--", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
15+
16+
constexpr int compileTimeAtoi(const char* str) {
17+
int result = 0;
18+
while (*str >= '0' && *str <= '9') {
19+
result = result * 10 + *str - '0';
20+
str++;
21+
}
22+
return result;
23+
}
1424
}
1525

1626
DateTime::DateTime(Controllers::Settings& settingsController) : settingsController {settingsController} {
1727
mutex = xSemaphoreCreateMutex();
1828
ASSERT(mutex != nullptr);
1929
xSemaphoreGive(mutex);
30+
31+
// __DATE__ is a string of the format "MMM DD YYYY", so an offset of 7 gives the start of the year
32+
SetTime(compileTimeAtoi(&__DATE__[7]), 1, 1, 0, 0, 0);
2033
}
2134

2235
void DateTime::SetCurrentTime(std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> t) {
@@ -46,7 +59,9 @@ void DateTime::SetTime(uint16_t year, uint8_t month, uint8_t day, uint8_t hour,
4659
UpdateTime(previousSystickCounter, true);
4760
xSemaphoreGive(mutex);
4861

49-
systemTask->PushMessage(System::Messages::OnNewTime);
62+
if (systemTask != nullptr) {
63+
systemTask->PushMessage(System::Messages::OnNewTime);
64+
}
5065
}
5166

5267
void DateTime::SetTimeZone(int8_t timezone, int8_t dst) {

0 commit comments

Comments
 (0)