receiver: store CSVs by local date and keep UTC history fallback

This commit is contained in:
2026-02-18 01:34:47 +01:00
parent 6f359b11d3
commit 92ac7e8810
2 changed files with 27 additions and 9 deletions

View File

@@ -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<time_t>(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) {

View File

@@ -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<time_t>(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<time_t>(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;
}