5#elif defined(USE_ZEPHYR)
6#include <zephyr/posix/time.h>
13#if defined(USE_RP2040) || defined(USE_ZEPHYR)
22static const char *
const TAG =
"time";
27#ifdef USE_TIME_TIMEZONE
41#ifdef USE_TIME_TIMEZONE
49 ESP_LOGCONFIG(TAG,
"Timezone: UTC%+d:%02d (DST UTC%+d:%02d)", std_h, std_m, dst_h, dst_m);
51 ESP_LOGCONFIG(TAG,
"Timezone: UTC%+d:%02d", std_h, std_m);
54 auto time = this->
now();
55 ESP_LOGCONFIG(TAG,
"Current time: %04d-%02d-%02d %02d:%02d:%02d", time.year, time.month, time.day_of_month, time.hour,
56 time.minute, time.second);
60 ESP_LOGVV(TAG,
"Got epoch %" PRIu32, epoch);
63 constexpr time_t min_valid_epoch = 1546300800;
66 if (current_time >= min_valid_epoch) {
68 int32_t diff =
static_cast<int32_t
>(epoch -
static_cast<uint32_t
>(current_time));
69 if (diff >= -1 && diff <= 1) {
80 ts.tv_sec =
static_cast<time_t
>(epoch);
82 int ret = clock_settime(CLOCK_REALTIME, &ts);
85 ESP_LOGW(TAG,
"clock_settime() failed with code %d", ret);
88 struct timeval timev {
89 .tv_sec =
static_cast<time_t
>(epoch), .tv_usec = 0,
91 struct timezone
tz = {0, 0};
92 int ret = settimeofday(&timev, &
tz);
96 ret = settimeofday(&timev,
nullptr);
100 ESP_LOGW(TAG,
"setimeofday() failed with code %d", ret);
103 auto time = this->
now();
104 ESP_LOGD(TAG,
"Synchronized time: %04d-%02d-%02d %02d:%02d:%02d", time.year, time.month, time.day_of_month, time.hour,
105 time.minute, time.second);
110#ifdef USE_TIME_TIMEZONE
115 if (
tz ==
nullptr || *
tz ==
'\0') {
132 ESP_LOGW(TAG,
"Failed to parse timezone: %s",
tz);
time_t timestamp_now()
Get the current time as the UTC epoch since January 1st 1970.
ESPTime now()
Get the time in the currently defined timezone.
void apply_timezone_(const char *tz)
LazyCallbackManager< void()> time_sync_callback_
void dump_config() override
void synchronize_epoch_(uint32_t epoch)
Report a unix epoch as current time.
void set_global_tz(const ParsedTimezone &tz)
Set the global timezone used by epoch_to_local_tm() when called without a timezone.
const ParsedTimezone & get_global_tz()
Get the global timezone.
ESPTime __attribute__((noinline)) RealTimeClock
bool parse_posix_tz(const char *tz_string, ParsedTimezone &result)
Parse a POSIX TZ string into a ParsedTimezone struct.
bool const ParsedTimezone & tz
bool epoch_to_local_tm(time_t utc_epoch, const ParsedTimezone &tz, struct tm *out_tm)
Convert a UTC epoch to local time using the parsed timezone.
A more user-friendly version of struct tm from time.h.
static ESPTime from_epoch_local(time_t epoch)
Convert an UTC epoch timestamp to a local time ESPTime instance.
static ESPTime from_c_tm(struct tm *c_tm, time_t c_time)
Convert a C tm struct instance with a C unix epoch timestamp to an ESPTime instance.
static ESPTime from_epoch_utc(time_t epoch)
Convert an UTC epoch timestamp to a UTC time ESPTime instance.
Parsed POSIX timezone information (packed for 32-bit: 32 bytes)
bool has_dst() const
Check if this timezone has DST rules.
int32_t dst_offset_seconds
DST offset from UTC in seconds.
int32_t std_offset_seconds
Standard time offset from UTC in seconds (positive = west)