From 7619625403ef9de5dca0cfce8e81d9f54a3b722d Mon Sep 17 00:00:00 2001 From: nedko Date: Tue, 30 Jun 2026 17:00:16 +0300 Subject: [PATCH] Fixed time alignment --- TRMNL.cpp | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/TRMNL.cpp b/TRMNL.cpp index 52c8f9b..697a77b 100644 --- a/TRMNL.cpp +++ b/TRMNL.cpp @@ -1,11 +1,14 @@ #include "TRMNL.h" #include "helpers.h" +#include + #include using std::list; using std::map; using std::optional; +using std::set; using std::string; using nlohmann::json; @@ -78,31 +81,37 @@ int TRMNL::get_next_refresh(int min_time, int offset) const struct tm* now = localtime(&curr); int tmp_refresh = m_refresh_rate; - bool align_minutes = false; - bool align_hours = false; + set allowed_minutes = {0, 1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30}; + set allowed_hours = {1, 2, 3, 4, 6, 8, 12, 24}; + // Check whether refresh rate is minutes aligned if (0 == m_refresh_rate % 60) { - align_minutes = true; tmp_refresh /= 60; + now->tm_sec = 0; + // Check whether refresh rate is hours aligned if (0 == tmp_refresh % 60) { - align_hours = true; tmp_refresh /= 60; + now->tm_min = 0; + + // Hours within the day + if (0 != allowed_hours.count(tmp_refresh)) + { + now->tm_hour -= now->tm_hour % tmp_refresh; + } + now->tm_hour += tmp_refresh; + } + else + { + // Minutes within the hour + if (0 != allowed_minutes.count(tmp_refresh)) + { + now->tm_min -= now->tm_min % tmp_refresh; + } + now->tm_min += tmp_refresh; } - } - - if (align_minutes) - { - now->tm_sec = 0; - now->tm_min += 1; - } - - if (align_hours) - { - now->tm_min = 0; - now->tm_hour += 1; } time_t next = mktime(now);