10 static const uint8_t DAYS_IN_MONTH[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
13 return DAYS_IN_MONTH[
month];
17 struct tm c_tm = this->
to_c_tm();
18 return ::strftime(buffer, buffer_len, format, &c_tm);
22 struct tm c_tm = this->
to_c_tm();
23 size_t len =
::strftime(buffer.data(), buffer.size(), format, &c_tm);
28 constexpr char error_str[] =
"ERROR";
29 std::copy_n(error_str,
sizeof(error_str), buffer.data());
30 return sizeof(error_str) - 1;
35 res.
second = uint8_t(c_tm->tm_sec);
36 res.minute = uint8_t(c_tm->tm_min);
37 res.hour = uint8_t(c_tm->tm_hour);
38 res.day_of_week = uint8_t(c_tm->tm_wday + 1);
39 res.day_of_month = uint8_t(c_tm->tm_mday);
40 res.day_of_year = uint16_t(c_tm->tm_yday + 1);
41 res.month = uint8_t(c_tm->tm_mon + 1);
42 res.year = uint16_t(c_tm->tm_year + 1900);
43 res.is_dst = bool(c_tm->tm_isdst);
44 res.timestamp = c_time;
50 c_tm.tm_sec = this->
second;
51 c_tm.tm_min = this->
minute;
52 c_tm.tm_hour = this->
hour;
53 c_tm.tm_mday = this->day_of_month;
54 c_tm.tm_mon = this->
month - 1;
55 c_tm.tm_year = this->
year - 1900;
56 c_tm.tm_wday = this->day_of_week - 1;
57 c_tm.tm_yday = this->day_of_year - 1;
58 c_tm.tm_isdst = this->is_dst;
65 return std::string(buf,
len);
78 const int ilen =
static_cast<int>(
len);
80 if (sscanf(time_to_parse,
"%04hu-%02hhu-%02hhu %02hhu:%02hhu:%02hhu %n", &
year, &
month, &
day,
91 }
else if (sscanf(time_to_parse,
"%04hu-%02hhu-%02hhu %02hhu:%02hhu %n", &
year, &
month, &
day,
101 }
else if (sscanf(time_to_parse,
"%02hhu:%02hhu:%02hhu %n", &
hour, &
minute, &
second, &num) == 3 &&
106 }
else if (sscanf(time_to_parse,
"%02hhu:%02hhu %n", &
hour, &
minute, &num) == 2 &&
111 }
else if (sscanf(time_to_parse,
"%04hu-%02hhu-%02hhu %n", &
year, &
month, &
day, &num) == 3 &&
143 uint16_t days_in_year = (this->
year % 4 == 0) ? 366 : 365;
161 uint16_t days_in_year = (this->
year % 4 == 0) ? 366 : 365;
175 for (
int i = 1970; i < this->
year; i++)
176 res += (i % 4 == 0) ? 366 : 365;
178 if (use_day_of_year) {
181 for (
int i = 1; i < this->
month; i++)
198 tm.tm_year = this->
year - 1900;
199 tm.tm_mon = this->
month - 1;
201 tm.tm_hour = this->
hour;
210 time_t now = ::time(
nullptr);
211 struct tm local_tm = *::localtime(&now);
212 local_tm.tm_isdst = 0;
213 time_t local_time = mktime(&local_tm);
214 struct tm utc_tm = *::gmtime(&now);
215 time_t utc_time = mktime(&utc_tm);
216 return static_cast<int32_t
>(local_time - utc_time);
227 if (current >=
end) {
Providing packet encoding functions for exchanging data with a remote host.
uint8_t days_in_month(uint8_t month, uint16_t year)
bool increment_time_value(T ¤t, uint16_t begin, uint16_t end)
A more user-friendly version of struct tm from time.h.
void increment_second()
Increment this clock instance by one second.
static int32_t timezone_offset()
uint8_t minute
minutes after the hour [0-59]
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).
bool operator<(const ESPTime &other) const
uint8_t second
seconds after the minute [0-60]
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.
std::string strftime(const std::string &format)
Convert this ESPTime struct to a string as specified by the format argument.
size_t strftime_to(std::span< char, STRFTIME_BUFFER_SIZE > buffer, const char *format)
Format time into a fixed-size buffer, returns length written.
bool operator>(const ESPTime &other) const
uint8_t hour
hours since midnight [0-23]
time_t timestamp
unix epoch time (seconds since UTC Midnight January 1, 1970)
void increment_day()
Increment this clock instance by one day.
uint16_t day_of_year
day of the year [1-366]
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 bool strptime(const char *time_to_parse, size_t len, ESPTime &esp_time)
Convert a string to ESPTime struct as specified by the format argument.
bool operator<=(const ESPTime &other) const
bool operator==(const ESPTime &other) const
void recalc_timestamp_local()
Recalculate the timestamp field from the other fields of this ESPTime instance assuming local fields.
static constexpr size_t STRFTIME_BUFFER_SIZE
Buffer size required for strftime output.
uint8_t day_of_month
day of the month [1-31]
struct tm to_c_tm()
Convert this ESPTime instance back to a tm struct.
bool fields_in_range() const
Check if all time fields of this ESPTime are in range.
uint8_t month
month; january=1 [1-12]
bool operator>=(const ESPTime &other) const
uint8_t day_of_week
day of the week; sunday=1 [1-7]