9#include "freertos/FreeRTOS.h"
10#include "freertos/semphr.h"
11#include "freertos/task.h"
21#ifndef CONFIG_TIME_SERVICE_SNTP_ENABLE
22#define CONFIG_TIME_SERVICE_SNTP_ENABLE 1
25#ifndef CONFIG_TIME_SERVICE_SNTP_SERVER
26#define CONFIG_TIME_SERVICE_SNTP_SERVER "pool.ntp.org"
29#ifndef CONFIG_TIME_SERVICE_SNTP_SYNC_TIMEOUT_MS
30#define CONFIG_TIME_SERVICE_SNTP_SYNC_TIMEOUT_MS 15000
33#ifndef CONFIG_TIME_SERVICE_SNTP_MAX_SYNC_ATTEMPTS
34#define CONFIG_TIME_SERVICE_SNTP_MAX_SYNC_ATTEMPTS 3
37#ifndef CONFIG_TIME_SERVICE_SNTP_RETRY_DELAY_MS
38#define CONFIG_TIME_SERVICE_SNTP_RETRY_DELAY_MS 5000
41static const char *
TAG =
"time_service";
51 time_t now = time(NULL);
53 return ESP_ERR_INVALID_STATE;
61 struct timeval now = {
62 .tv_sec = (time_t)epoch,
66 return (settimeofday(&now, NULL) == 0) ? ESP_OK : ESP_FAIL;
71#if CONFIG_TIME_SERVICE_SNTP_ENABLE
76 esp_sntp_setoperatingmode(ESP_SNTP_OPMODE_POLL);
77 esp_sntp_set_sync_mode(SNTP_SYNC_MODE_IMMED);
84 return ESP_ERR_NOT_SUPPORTED;
92#if CONFIG_TIME_SERVICE_SNTP_ENABLE
94 const TickType_t poll_delay = pdMS_TO_TICKS(1000);
95 bool synchronized =
false;
106 ESP_LOGW(
TAG,
"SNTP sync aborted (network disconnected)");
111 ESP_LOGW(
TAG,
"SNTP init failed (attempt %d)", attempt);
113 vTaskDelay(retry_delay);
119 if (esp_sntp_get_sync_status() == SNTP_SYNC_STATUS_COMPLETED) {
130 vTaskDelay(poll_delay);
138 ESP_LOGW(
TAG,
"SNTP sync timeout (attempt %d/%d)",
143 vTaskDelay(retry_delay);
148 ESP_LOGW(
TAG,
"SNTP not synchronized on this network session");
170 {
"Europe/Stockholm",
"CET-1CEST,M3.5.0,M10.5.0/3"},
171 {
"Europe/London",
"GMT0BST,M3.5.0/1,M10.5.0"},
172 {
"America/New_York",
"EST5EDT,M3.2.0,M11.1.0"},
173 {
"America/Chicago",
"CST6CDT,M3.2.0,M11.1.0"},
174 {
"America/Denver",
"MST7MDT,M3.2.0,M11.1.0"},
175 {
"America/Los_Angeles",
"PST8PDT,M3.2.0,M11.1.0"},
176 {
"America/Sao_Paulo",
"<-03>3"},
177 {
"Asia/Dubai",
"<+04>-4"},
178 {
"Asia/Kolkata",
"<+0530>-5:30"},
179 {
"Asia/Bangkok",
"<+07>-7"},
180 {
"Asia/Singapore",
"<+08>-8"},
181 {
"Asia/Tokyo",
"JST-9"},
182 {
"Australia/Sydney",
"AEST-10AEDT,M10.1.0,M4.1.0/3"},
183 {
"Pacific/Auckland",
"NZST-12NZDT,M9.5.0,M4.1.0/3"},
191 int sign = (offset_min >= 0) ? 1 : -1;
192 int abs_min = (offset_min >= 0) ? (
int)offset_min : -(int)offset_min;
193 int h = abs_min / 60;
194 int m = abs_min % 60;
196 int posix_h = -(sign * h);
199 snprintf(buf, buf_len,
"<%s%02d>%d",
200 sign >= 0 ?
"+" :
"-", h, posix_h);
202 snprintf(buf, buf_len,
"<%s%02d%02d>%d:%02d",
203 sign >= 0 ?
"+" :
"-", h, m, posix_h, m);
209 const char *posix_str = NULL;
213 if (location_id != NULL) {
215 if (strcmp(location_id,
s_tz_table[i].location_id) == 0) {
222 if (posix_str == NULL) {
224 posix_str = generated;
227 setenv(
"TZ", posix_str, 1);
230 ESP_LOGI(
TAG,
"Timezone: location=\"%s\" posix=\"%s\"",
231 location_id ? location_id :
"custom", posix_str);
236 int64_t saved_epoch = 0;
237 bool saved_valid =
false;
242 return ESP_ERR_NO_MEM;
257 "restore saved epoch failed");
272 ESP_RETURN_ON_FALSE(epoch > 0, ESP_ERR_INVALID_ARG,
TAG,
"epoch must be > 0");
285 bool start_sync_task =
false;
290#if CONFIG_TIME_SERVICE_SNTP_ENABLE
293 start_sync_task =
true;
299 if (start_sync_task) {
306 if (task_ok != pdPASS) {
310 ESP_LOGE(
TAG,
"Failed to create SNTP sync task");
317 bool persist_valid_epoch =
false;
318 time_t utc_time = time(NULL);
328 persist_valid_epoch =
true;
332 if (persist_valid_epoch) {
338 memset(&local_tm, 0,
sizeof(local_tm));
339 localtime_r(&utc_time, &local_tm);
341 if (out_tm != NULL) {
345 if (out_epoch != NULL) {
347 *out_epoch = (int64_t)utc_time;
void app_state_get_time_epoch(int64_t *epoch, bool *valid)
Get stored epoch value and validity flag.
void app_state_get_config(app_config_t *config)
Get current persisted configuration.
esp_err_t app_state_set_time_epoch(int64_t epoch, bool valid)
Update stored epoch and validity flag.
Thread-safe application state and persisted configuration APIs.
Persisted configuration model.
char timezone_location[APP_TIMEZONE_LOCATION_MAX_LEN+1]
static void time_service_sntp_sync_task(void *arg)
void time_service_apply_timezone(const char *location_id, int16_t fallback_offset_min)
Apply a timezone by IANA location ID with DST support.
#define CONFIG_TIME_SERVICE_SNTP_SYNC_TIMEOUT_MS
static SemaphoreHandle_t s_time_lock
esp_err_t time_service_init(void)
Initialize time service state and restore persisted time fallback.
static const size_t s_tz_table_count
#define CONFIG_TIME_SERVICE_SNTP_SERVER
static esp_err_t time_service_try_sntp_init(void)
esp_err_t time_service_set_epoch(int64_t epoch)
Set current UTC epoch and persist it as valid time.
#define CONFIG_TIME_SERVICE_SNTP_MAX_SYNC_ATTEMPTS
static bool s_network_connected
static esp_err_t time_service_store_epoch_from_system(void)
static bool s_sntp_initialized
static const time_service_tz_entry_t s_tz_table[]
void time_service_notify_network_connected(bool connected)
Notify the service about network connectivity changes.
static bool s_sntp_sync_task_running
bool time_service_get_local_time(struct tm *out_tm, int64_t *out_epoch)
Get local time adjusted by the configured timezone offset.
static void time_service_posix_from_offset(int16_t offset_min, char *buf, size_t buf_len)
#define CONFIG_TIME_SERVICE_SNTP_RETRY_DELAY_MS
static esp_err_t time_service_set_system_epoch(int64_t epoch)
Time service APIs for local time access and synchronization triggers.