15 static const char *
const TAG =
"time";
24 struct timeval timev {
25 .tv_sec =
static_cast<time_t
>(epoch), .tv_usec = 0,
27 ESP_LOGVV(TAG,
"Got epoch %u", epoch);
29 int ret = settimeofday(&timev, &tz);
33 ret = settimeofday(&timev,
nullptr);
40 ESP_LOGW(TAG,
"setimeofday() failed with code %d", ret);
43 auto time = this->
now();
44 ESP_LOGD(TAG,
"Synchronized time: %04d-%02d-%02d %02d:%02d:%02d", time.year, time.month, time.day_of_month, time.hour,
45 time.minute, time.second);
56 struct tm c_tm = this->to_c_tm();
57 return ::strftime(buffer, buffer_len, format, &c_tm);
61 res.
second = uint8_t(c_tm->tm_sec);
62 res.minute = uint8_t(c_tm->tm_min);
63 res.hour = uint8_t(c_tm->tm_hour);
64 res.day_of_week = uint8_t(c_tm->tm_wday + 1);
65 res.day_of_month = uint8_t(c_tm->tm_mday);
66 res.day_of_year = uint16_t(c_tm->tm_yday + 1);
67 res.month = uint8_t(c_tm->tm_mon + 1);
68 res.year = uint16_t(c_tm->tm_year + 1900);
69 res.is_dst = bool(c_tm->tm_isdst);
70 res.timestamp = c_time;
75 c_tm.tm_sec = this->second;
76 c_tm.tm_min = this->minute;
77 c_tm.tm_hour = this->hour;
78 c_tm.tm_mday = this->day_of_month;
79 c_tm.tm_mon = this->month - 1;
80 c_tm.tm_year = this->year - 1900;
81 c_tm.tm_wday = this->day_of_week - 1;
82 c_tm.tm_yday = this->day_of_year - 1;
83 c_tm.tm_isdst = this->is_dst;
88 timestr.resize(format.size() * 4);
89 struct tm c_tm = this->to_c_tm();
90 size_t len = ::strftime(×tr[0], timestr.size(), format.c_str(), &c_tm);
92 timestr.resize(timestr.size() * 2);
93 len = ::strftime(×tr[0], timestr.size(), format.c_str(), &c_tm);
101 if (current >= end) {
108 static bool is_leap_year(uint32_t year) {
return (year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0); }
110 static uint8_t days_in_month(uint8_t month, uint16_t year) {
111 static const uint8_t DAYS_IN_MONTH[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
112 uint8_t days = DAYS_IN_MONTH[month];
113 if (month == 2 && is_leap_year(year))
139 uint16_t days_in_year = (this->year % 4 == 0) ? 366 : 365;
146 this->timestamp += 86400;
156 uint16_t days_in_year = (this->year % 4 == 0) ? 366 : 365;
165 if (!this->fields_in_range()) {
166 this->timestamp = -1;
170 for (
int i = 1970; i < this->year; i++)
171 res += is_leap_year(i) ? 366 : 365;
173 if (use_day_of_year) {
174 res += this->day_of_year - 1;
176 for (
int i = 1; i < this->month; i++)
177 res += days_in_month(i, this->year);
179 res += this->day_of_month - 1;
188 this->timestamp = res;
193 time_t
now = ::time(
nullptr);
196 bool negative = utc.hour > local.hour && local.day_of_year <= utc.day_of_year;
198 if (utc.minute > local.minute) {
202 offset += (local.minute - utc.minute) * 60;
205 offset -= (utc.hour - local.hour) * 3600;
207 if (utc.hour > local.hour) {
210 offset += (local.hour - utc.hour) * 3600;
ESPTime now()
Get the time in the currently defined timezone.
time_t timestamp
unix epoch time (seconds since UTC Midnight January 1, 1970)
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.
size_t strftime(char *buffer, size_t buffer_len, const char *format)
Convert this ESPTime struct to a null-terminated c string buffer as specified by the format argument...
void increment_second()
Increment this clock instance by one second.
bool increment_time_value(T ¤t, uint16_t begin, uint16_t end)
static ESPTime from_epoch_local(time_t epoch)
Convert an UTC epoch timestamp to a local time ESPTime instance.
CallbackManager< void()> time_sync_callback_
void increment_day()
Increment this clock instance by one day.
A more user-friendly version of struct tm from time.h.
bool operator<=(ESPTime other)
static ESPTime from_epoch_utc(time_t epoch)
Convert an UTC epoch timestamp to a UTC time ESPTime instance.
uint8_t second
seconds after the minute [0-60]
bool operator==(ESPTime other)
bool operator>(ESPTime other)
bool operator<(ESPTime other)
void call_setup() override
void synchronize_epoch_(uint32_t epoch)
Report a unix epoch as current time.
void recalc_timestamp_utc(bool use_day_of_year=true)
Recalculate the timestamp field from the other fields of this ESPTime instance (must be UTC)...
static int32_t timezone_offset()
bool operator>=(ESPTime other)
void call_setup() override