33 ESP_LOGVV(TAG,
"Got epoch %" PRIu32, epoch);
36 constexpr time_t min_valid_epoch = 1546300800;
39 if (current_time >= min_valid_epoch) {
41 int32_t diff =
static_cast<int32_t
>(epoch -
static_cast<uint32_t
>(current_time));
42 if (diff >= -1 && diff <= 1) {
53 ts.tv_sec =
static_cast<time_t
>(epoch);
55 int ret = clock_settime(CLOCK_REALTIME, &ts);
58 ESP_LOGW(TAG,
"clock_settime() failed with code %d", ret);
61 struct timeval timev {
62 .tv_sec =
static_cast<time_t
>(epoch), .tv_usec = 0,
64 struct timezone tz = {0, 0};
65 int ret = settimeofday(&timev, &tz);
69 ret = settimeofday(&timev,
nullptr);
72#ifdef USE_TIME_TIMEZONE
78 ESP_LOGW(TAG,
"setimeofday() failed with code %d", ret);
81 auto time = this->
now();
82 ESP_LOGD(TAG,
"Synchronized time: %04d-%02d-%02d %02d:%02d:%02d", time.year, time.month, time.day_of_month, time.hour,
83 time.minute, time.second);