From 92ac7e8810c946e0b8dd88f70683e813372076df Mon Sep 17 00:00:00 2001 From: acidburns Date: Wed, 18 Feb 2026 01:34:47 +0100 Subject: [PATCH] receiver: store CSVs by local date and keep UTC history fallback --- src/sd_logger.cpp | 14 +++++++------- src/web_server.cpp | 22 ++++++++++++++++++++-- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/sd_logger.cpp b/src/sd_logger.cpp index f0660c5..8e1db43 100644 --- a/src/sd_logger.cpp +++ b/src/sd_logger.cpp @@ -27,15 +27,15 @@ static bool ensure_dir(const String &path) { return SD.mkdir(path); } -static String format_date_utc(uint32_t ts_utc) { +static String format_date_local(uint32_t ts_utc) { time_t t = static_cast(ts_utc); - struct tm tm_utc; - gmtime_r(&t, &tm_utc); + struct tm tm_local; + localtime_r(&t, &tm_local); char buf[16]; snprintf(buf, sizeof(buf), "%04d-%02d-%02d", - tm_utc.tm_year + 1900, - tm_utc.tm_mon + 1, - tm_utc.tm_mday); + tm_local.tm_year + 1900, + tm_local.tm_mon + 1, + tm_local.tm_mday); return String(buf); } @@ -94,7 +94,7 @@ void sd_logger_log_sample(const MeterData &data, bool include_error_text) { return; } - String filename = sender_dir + "/" + format_date_utc(data.ts_utc) + ".csv"; + String filename = sender_dir + "/" + format_date_local(data.ts_utc) + ".csv"; bool new_file = !SD.exists(filename); File f = SD.open(filename, FILE_APPEND); if (!f) { diff --git a/src/web_server.cpp b/src/web_server.cpp index 40cc317..b22da72 100644 --- a/src/web_server.cpp +++ b/src/web_server.cpp @@ -243,7 +243,16 @@ static void history_reset() { g_history = {}; } -static String history_date_from_epoch(uint32_t ts_utc) { +static String history_date_from_epoch_local(uint32_t ts_utc) { + time_t t = static_cast(ts_utc); + struct tm tm_local; + localtime_r(&t, &tm_local); + char buf[16]; + snprintf(buf, sizeof(buf), "%04d-%02d-%02d", tm_local.tm_year + 1900, tm_local.tm_mon + 1, tm_local.tm_mday); + return String(buf); +} + +static String history_date_from_epoch_utc(uint32_t ts_utc) { time_t t = static_cast(ts_utc); struct tm tm_utc; gmtime_r(&t, &tm_utc); @@ -298,8 +307,17 @@ static bool history_open_next_file() { g_history.done = true; return false; } - String path = String("/dd3/") + g_history.device_id + "/" + history_date_from_epoch(day_ts) + ".csv"; + String local_date = history_date_from_epoch_local(day_ts); + String path = String("/dd3/") + g_history.device_id + "/" + local_date + ".csv"; g_history.file = SD.open(path.c_str(), FILE_READ); + if (!g_history.file) { + // Compatibility fallback for files written before local-date partitioning. + String utc_date = history_date_from_epoch_utc(day_ts); + if (utc_date != local_date) { + String legacy_path = String("/dd3/") + g_history.device_id + "/" + utc_date + ".csv"; + g_history.file = SD.open(legacy_path.c_str(), FILE_READ); + } + } g_history.day_index++; return true; }