fix(time): apply timezone before lcd startup
This commit is contained in:
@@ -237,12 +237,15 @@ esp_err_t platform_storage_erase(const char *ns, const char *key);
|
||||
/**
|
||||
* @brief Wall-clock time synchronization (SNTP) primitives.
|
||||
*
|
||||
* - `platform_time_set_timezone()` applies the process-local POSIX time zone
|
||||
* without starting SNTP. Call it during boot before local time is rendered.
|
||||
* - `platform_time_sync_start()` is idempotent: it sets the process time zone
|
||||
* (`posix_tz`, e.g. "CST-8"; ignored when NULL/empty) on every call and
|
||||
* starts the SNTP client once. Must be called after the network is up.
|
||||
* - `platform_time_is_synced()` reports whether the system clock has advanced
|
||||
* past a sane epoch lower bound (i.e. SNTP has set the time at least once).
|
||||
*/
|
||||
esp_err_t platform_time_set_timezone(const char *posix_tz);
|
||||
esp_err_t platform_time_sync_start(const char *sntp_server, const char *posix_tz);
|
||||
bool platform_time_is_synced(void);
|
||||
|
||||
|
||||
@@ -23,11 +23,27 @@ static const char *TAG = "platform_time";
|
||||
static bool s_sntp_started;
|
||||
static portMUX_TYPE s_time_lock = portMUX_INITIALIZER_UNLOCKED;
|
||||
|
||||
esp_err_t platform_time_set_timezone(const char *posix_tz)
|
||||
{
|
||||
if (posix_tz == NULL || posix_tz[0] == '\0') {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
if (setenv("TZ", posix_tz, 1) != 0) {
|
||||
ESP_LOGE(TAG, "Set time zone failed (tz=%s)", posix_tz);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
tzset();
|
||||
ESP_LOGI(TAG, "Local time zone applied (tz=%s)", posix_tz);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t platform_time_sync_start(const char *sntp_server, const char *posix_tz)
|
||||
{
|
||||
if (posix_tz != NULL && posix_tz[0] != '\0') {
|
||||
setenv("TZ", posix_tz, 1);
|
||||
tzset();
|
||||
esp_err_t timezone_err = platform_time_set_timezone(posix_tz);
|
||||
if (timezone_err != ESP_OK) {
|
||||
return timezone_err;
|
||||
}
|
||||
}
|
||||
|
||||
portENTER_CRITICAL(&s_time_lock);
|
||||
|
||||
Reference in New Issue
Block a user