summaryrefslogtreecommitdiff
path: root/src/components/datetime/DateTimeController.cpp
diff options
context:
space:
mode:
authorMichele Bini <michele.bini@gmail.com>2022-04-17 11:13:41 (GMT)
committerMichele Bini <michele.bini@gmail.com>2022-04-17 13:16:10 (GMT)
commitaca605d4fb4040cc80c1acf101023aa86e7694ba (patch)
tree5f83db7a5bfbf05d732121bb95ca383243cc8eaf /src/components/datetime/DateTimeController.cpp
parentc372ba1908bb861d7f05d057c7fc25202c34bbe3 (diff)
Differentiate time adjustment (< 3h for example time zone change when crossing a border or because of daylight saving) and time setting (for example when the firmware is booted for the first time and the companion app sends the time)
Diffstat (limited to 'src/components/datetime/DateTimeController.cpp')
-rw-r--r--src/components/datetime/DateTimeController.cpp33
1 files changed, 22 insertions, 11 deletions
diff --git a/src/components/datetime/DateTimeController.cpp b/src/components/datetime/DateTimeController.cpp
index 3bfbdc7..2656764 100644
--- a/src/components/datetime/DateTimeController.cpp
+++ b/src/components/datetime/DateTimeController.cpp
@@ -30,6 +30,7 @@ void DateTime::SetTime(
/* .tm_year = */ year - 1900,
};
tm.tm_isdst = -1; // Use DST value from local time zone
+ auto previousDateTime = currentDateTime;
currentDateTime = std::chrono::system_clock::from_time_t(std::mktime(&tm));
NRF_LOG_INFO("%d %d %d ", day, month, year);
@@ -40,7 +41,17 @@ void DateTime::SetTime(
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);
+ decltype(currentDateTime - previousDateTime) timeDifference;
+
+ if (currentDateTime > previousDateTime) {
+ timeDifference = currentDateTime - previousDateTime;
+ } else {
+ timeDifference = previousDateTime - currentDateTime;
+ }
+
+ using namespace std::chrono_literals;
+
+ systemTask->PushMessage(timeDifference < 3h ? System::Messages::OnAdjustTime : System::Messages::OnNewTime);
}
void DateTime::UpdateTime(uint32_t systickCounter) {
@@ -129,16 +140,16 @@ std::string DateTime::FormattedTime() {
// Return time as a string in 12- or 24-hour format
char buff[9];
if (settingsController.GetClockType() == ClockType::H12) {
- uint8_t hour12;
- const char* amPmStr;
- if (hour < 12) {
- hour12 = (hour == 0) ? 12 : hour;
- amPmStr = "AM";
- } else {
- hour12 = (hour == 12) ? 12 : hour - 12;
- amPmStr = "PM";
- }
- sprintf(buff, "%i:%02i %s", hour12, minute, amPmStr);
+ uint8_t hour12;
+ const char* amPmStr;
+ if (hour < 12) {
+ hour12 = (hour == 0) ? 12 : hour;
+ amPmStr = "AM";
+ } else {
+ hour12 = (hour == 12) ? 12 : hour - 12;
+ amPmStr = "PM";
+ }
+ sprintf(buff, "%i:%02i %s", hour12, minute, amPmStr);
} else {
sprintf(buff, "%02i:%02i", hour, minute);
}