34 lines
903 B
C
34 lines
903 B
C
|
|
#pragma once
|
||
|
|
|
||
|
|
#include <Arduino.h>
|
||
|
|
#include <time.h>
|
||
|
|
|
||
|
|
// Returns true if the system clock has been set to a plausible time (year >= 2020).
|
||
|
|
inline bool hasValidTime() {
|
||
|
|
time_t now = time(nullptr);
|
||
|
|
struct tm t;
|
||
|
|
gmtime_r(&now, &t);
|
||
|
|
return (t.tm_year + 1900) >= 2020;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Returns the current UTC time as an ISO 8601 string (e.g., "2026-08-04T17:30:00.000Z").
|
||
|
|
inline String isoNow() {
|
||
|
|
time_t now = time(nullptr);
|
||
|
|
struct tm t;
|
||
|
|
gmtime_r(&now, &t);
|
||
|
|
char buf[32];
|
||
|
|
strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S.000Z", &t);
|
||
|
|
return String(buf);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Returns (now - 30 days) as an ISO 8601 UTC string.
|
||
|
|
inline String isoNowMinus30d() {
|
||
|
|
time_t now = time(nullptr);
|
||
|
|
now -= 30 * 24 * 3600; // Subtract 30 days in seconds
|
||
|
|
struct tm t;
|
||
|
|
gmtime_r(&now, &t);
|
||
|
|
char buf[32];
|
||
|
|
strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S.000Z", &t);
|
||
|
|
return String(buf);
|
||
|
|
}
|