Fixed time alignment

This commit is contained in:
2026-06-30 17:00:16 +03:00
parent 30922ca93c
commit 7619625403

View File

@@ -1,11 +1,14 @@
#include "TRMNL.h" #include "TRMNL.h"
#include "helpers.h" #include "helpers.h"
#include <set>
#include <time.h> #include <time.h>
using std::list; using std::list;
using std::map; using std::map;
using std::optional; using std::optional;
using std::set;
using std::string; using std::string;
using nlohmann::json; using nlohmann::json;
@@ -78,31 +81,37 @@ int TRMNL::get_next_refresh(int min_time, int offset) const
struct tm* now = localtime(&curr); struct tm* now = localtime(&curr);
int tmp_refresh = m_refresh_rate; int tmp_refresh = m_refresh_rate;
bool align_minutes = false; set<int> allowed_minutes = {0, 1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30};
bool align_hours = false; set<int> allowed_hours = {1, 2, 3, 4, 6, 8, 12, 24};
// Check whether refresh rate is minutes aligned
if (0 == m_refresh_rate % 60) if (0 == m_refresh_rate % 60)
{ {
align_minutes = true;
tmp_refresh /= 60; tmp_refresh /= 60;
now->tm_sec = 0;
// Check whether refresh rate is hours aligned
if (0 == tmp_refresh % 60) if (0 == tmp_refresh % 60)
{ {
align_hours = true;
tmp_refresh /= 60; 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); time_t next = mktime(now);