Skip to content

Commit 34b2c87

Browse files
committed
Better way
1 parent 11014b6 commit 34b2c87

File tree

4 files changed

+17
-18
lines changed

4 files changed

+17
-18
lines changed

src/components/datetime/DateTimeController.cpp

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,12 @@ namespace {
1414
constexpr const char* const MonthsString[] = {"--", "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"};
1515
constexpr const char* const MonthsStringLow[] =
1616
{"--", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
17-
18-
constexpr int compileTimeAtoi(const char* str) {
19-
int result = 0;
20-
while (*str >= '0' && *str <= '9') {
21-
result = result * 10 + *str - '0';
22-
str++;
23-
}
24-
return result;
25-
}
2617
}
2718

2819
DateTime::DateTime(Controllers::Settings& settingsController) : settingsController {settingsController} {
2920
mutex = xSemaphoreCreateMutex();
3021
ASSERT(mutex != nullptr);
3122
xSemaphoreGive(mutex);
32-
33-
// __DATE__ is a string of the format "MMM DD YYYY", so an offset of 7 gives the start of the year
34-
SetTime(compileTimeAtoi(&__DATE__[7]), 1, 1, 0, 0, 0, false);
3523
}
3624

3725
void DateTime::SetCurrentTime(std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> t) {
@@ -41,7 +29,7 @@ void DateTime::SetCurrentTime(std::chrono::time_point<std::chrono::system_clock,
4129
xSemaphoreGive(mutex);
4230
}
4331

44-
void DateTime::SetTime(uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second, bool log) {
32+
void DateTime::SetTime(uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second) {
4533
std::tm tm = {
4634
/* .tm_sec = */ second,
4735
/* .tm_min = */ minute,
@@ -51,10 +39,8 @@ void DateTime::SetTime(uint16_t year, uint8_t month, uint8_t day, uint8_t hour,
5139
/* .tm_year = */ year - 1900,
5240
};
5341

54-
if (log) {
55-
NRF_LOG_INFO("%d %d %d ", day, month, year);
56-
NRF_LOG_INFO("%d %d %d ", hour, minute, second);
57-
}
42+
NRF_LOG_INFO("%d %d %d ", day, month, year);
43+
NRF_LOG_INFO("%d %d %d ", hour, minute, second);
5844

5945
tm.tm_isdst = -1; // Use DST value from local time zone
6046

src/components/datetime/DateTimeController.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ namespace Pinetime {
3434
December
3535
};
3636

37-
void SetTime(uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second, bool log = true);
37+
void SetTime(uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second);
3838

3939
/*
4040
* setter corresponding to the BLE Set Local Time characteristic.

src/main.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include "systemtask/SystemTask.h"
4848
#include "touchhandler/TouchHandler.h"
4949
#include "buttonhandler/ButtonHandler.h"
50+
#include "utility/Math.h"
5051

5152
#if NRF_LOG_ENABLED
5253
#include "logging/NrfLogger.h"
@@ -355,6 +356,9 @@ int main() {
355356
if (NoInit_MagicWord == NoInit_MagicValue) {
356357
dateTimeController.SetCurrentTime(NoInit_BackUpTime);
357358
} else {
359+
// __DATE__ is a string of the format "MMM DD YYYY", so an offset of 7 gives the start of the year
360+
dateTimeController.SetTime(Pinetime::Utility::compileTimeAtoi(&__DATE__[7]), 1, 1, 0, 0, 0);
361+
358362
// Clear Memory to known state
359363
memset(&__start_noinit_data, 0, (uintptr_t) &__stop_noinit_data - (uintptr_t) &__start_noinit_data);
360364
NoInit_MagicWord = NoInit_MagicValue;

src/utility/Math.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,14 @@ namespace Pinetime {
2929
}
3030
return res;
3131
}
32+
33+
constexpr int compileTimeAtoi(const char* str) {
34+
int result = 0;
35+
while (*str >= '0' && *str <= '9') {
36+
result = result * 10 + *str - '0';
37+
str++;
38+
}
39+
return result;
40+
}
3241
}
3342
}

0 commit comments

Comments
 (0)