Adding timezone support
This commit is contained in:
@@ -85,12 +85,36 @@ STR_SHOW_HIDDEN_FILES: "Show Hidden Files"
|
||||
STR_KEEP_CLOCK_ALIVE: "Keep Clock Alive"
|
||||
STR_CLOCK: "Clock"
|
||||
STR_CLOCK_FORMAT: "Clock Format"
|
||||
STR_TIMEZONE: "Timezone"
|
||||
STR_24H: "24h"
|
||||
STR_12H: "12h"
|
||||
STR_TZ_UTC: "UTC (GMT/BST)"
|
||||
STR_TZ_CET: "Central Europe (CET/CEST)"
|
||||
STR_TZ_EET: "Eastern Europe (EET/EEST)"
|
||||
STR_TZ_EST: "US Eastern (EST/EDT)"
|
||||
STR_TZ_CST: "US Central (CST/CDT)"
|
||||
STR_TZ_MST: "US Mountain (MST/MDT)"
|
||||
STR_TZ_PST: "US Pacific (PST/PDT)"
|
||||
STR_TZ_AEST: "Australia Eastern (AEST/AEDT)"
|
||||
STR_TZ_NZST: "New Zealand (NZST/NZDT)"
|
||||
STR_TZ_MSK: "Russia (MSK)"
|
||||
STR_TZ_UTC_MINUS3: "South America (UTC-3)"
|
||||
STR_TZ_UTC_PLUS4: "Gulf (UTC+4)"
|
||||
STR_TZ_IST: "India (UTC+5:30)"
|
||||
STR_TZ_UTC_PLUS7: "SE Asia (UTC+7)"
|
||||
STR_TZ_UTC_PLUS8: "China/SE Asia (UTC+8)"
|
||||
STR_TZ_UTC_PLUS9: "Japan/Korea (UTC+9)"
|
||||
STR_SYNC_TIME: "Sync Time"
|
||||
STR_DETECT_TIMEZONE: "Detect Timezone"
|
||||
STR_SYNCING_CLOCK: "Syncing clock..."
|
||||
STR_DETECTING_TIMEZONE: "Detecting timezone..."
|
||||
STR_TIME_SYNCED: "Time synced"
|
||||
STR_TIME_SYNC_FAILED: "Time sync failed"
|
||||
STR_TIMEZONE_DETECTED: "Timezone detected"
|
||||
STR_TIMEZONE_DETECT_FAILED: "Timezone detect failed"
|
||||
STR_DST_ACTIVE: "DST: active"
|
||||
STR_DST_INACTIVE: "DST: inactive"
|
||||
STR_DST_UNKNOWN: "DST: unknown"
|
||||
STR_REFRESH_FREQ: "Refresh Frequency"
|
||||
STR_KOREADER_SYNC: "KOReader Sync"
|
||||
STR_CHECK_UPDATES: "Check for updates"
|
||||
|
||||
@@ -6,6 +6,9 @@
|
||||
#include <esp_private/esp_clk.h>
|
||||
#include <esp_sntp.h>
|
||||
#include <sys/time.h>
|
||||
#include <time.h>
|
||||
|
||||
#include <cstdlib>
|
||||
|
||||
// ---- RTC-memory state (survives deep sleep, not cold boot) ----------------
|
||||
|
||||
@@ -19,6 +22,29 @@ RTC_NOINIT_ATTR static uint64_t rtcLpTimeUs; // esp_clk_rtc_time() at capture
|
||||
|
||||
static bool clockApproximate = true;
|
||||
|
||||
struct TimeZoneEntry {
|
||||
const char* tz;
|
||||
};
|
||||
|
||||
static constexpr TimeZoneEntry TIMEZONES[] = {
|
||||
{"GMT0BST,M3.5.0/1,M10.5.0/2"},
|
||||
{"CET-1CEST,M3.5.0/2,M10.5.0/3"},
|
||||
{"EET-2EEST,M3.5.0/3,M10.5.0/4"},
|
||||
{"MSK-3"},
|
||||
{"UTC-4"},
|
||||
{"UTC-5:30"},
|
||||
{"UTC-7"},
|
||||
{"UTC-8"},
|
||||
{"UTC-9"},
|
||||
{"AEST-10AEDT,M10.1.0/2,M4.1.0/3"},
|
||||
{"NZST-12NZDT,M9.5.0/2,M4.1.0/3"},
|
||||
{"UTC+3"},
|
||||
{"EST5EDT,M3.2.0/2,M11.1.0/2"},
|
||||
{"CST6CDT,M3.2.0/2,M11.1.0/2"},
|
||||
{"MST7MDT,M3.2.0/2,M11.1.0/2"},
|
||||
{"PST8PDT,M3.2.0/2,M11.1.0/2"},
|
||||
};
|
||||
|
||||
// ---- NVS helpers ----------------------------------------------------------
|
||||
|
||||
static constexpr char NVS_NAMESPACE[] = "halclock";
|
||||
@@ -65,6 +91,13 @@ static void capture(bool lpValid) {
|
||||
|
||||
namespace HalClock {
|
||||
|
||||
void applyTimezone(uint8_t timeZoneSetting) {
|
||||
const size_t index = timeZoneSetting < (sizeof(TIMEZONES) / sizeof(TIMEZONES[0])) ? timeZoneSetting : 0;
|
||||
setenv("TZ", TIMEZONES[index].tz, 1);
|
||||
tzset();
|
||||
LOG_DBG("CLK", "Timezone applied: %s", TIMEZONES[index].tz);
|
||||
}
|
||||
|
||||
bool syncNtp() {
|
||||
if (esp_sntp_enabled()) {
|
||||
esp_sntp_stop();
|
||||
|
||||
@@ -30,6 +30,9 @@ namespace HalClock {
|
||||
/// Returns true if the sync succeeded.
|
||||
bool syncNtp();
|
||||
|
||||
/// Apply timezone/DST rules via the POSIX TZ string for the given setting.
|
||||
void applyTimezone(uint8_t timeZoneSetting);
|
||||
|
||||
/// Call just before deep sleep. Snapshots the current system time to RTC
|
||||
/// memory and NVS so it can be restored on wake / cold boot. Pass true when
|
||||
/// the LP timer is kept alive during sleep.
|
||||
|
||||
Reference in New Issue
Block a user