9 static const uint8_t DAYS_IN_MONTH[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
12 return DAYS_IN_MONTH[
month];
16 struct tm c_tm = this->
to_c_tm();
17 return ::strftime(buffer, buffer_len, format, &c_tm);
21 struct tm c_tm = this->
to_c_tm();
22 size_t len =
::strftime(buffer.data(), buffer.size(), format, &c_tm);
27 constexpr char error_str[] =
"ERROR";
28 std::copy_n(error_str,
sizeof(error_str), buffer.data());
29 return sizeof(error_str) - 1;
34 res.
second = uint8_t(c_tm->tm_sec);
35 res.minute = uint8_t(c_tm->tm_min);
36 res.hour = uint8_t(c_tm->tm_hour);
37 res.day_of_week = uint8_t(c_tm->tm_wday + 1);
38 res.day_of_month = uint8_t(c_tm->tm_mday);
39 res.day_of_year = uint16_t(c_tm->tm_yday + 1);
40 res.month = uint8_t(c_tm->tm_mon + 1);
41 res.year = uint16_t(c_tm->tm_year + 1900);
42 res.is_dst = bool(c_tm->tm_isdst);
43 res.timestamp = c_time;
49 c_tm.tm_sec = this->
second;
50 c_tm.tm_min = this->
minute;
51 c_tm.tm_hour = this->
hour;
52 c_tm.tm_mday = this->day_of_month;
53 c_tm.tm_mon = this->
month - 1;
54 c_tm.tm_year = this->
year - 1900;
55 c_tm.tm_wday = this->day_of_week - 1;
56 c_tm.tm_yday = this->day_of_year - 1;
57 c_tm.tm_isdst = this->is_dst;
64 return std::string(buf,
len);
70static bool parse_digits(
const char *&p,
const char *
end,
int count, uint16_t &value) {
72 for (
int i = 0; i < count; i++) {
73 if (p >=
end || *p <
'0' || *p >
'9')
75 value = value * 10 + (*p -
'0');
82static bool expect_char(
const char *&p,
const char *
end,
char expected) {
83 if (p >=
end || *p != expected)
97 if (time_to_parse ==
nullptr ||
len == 0)
100 const char *p = time_to_parse;
101 const char *
end = time_to_parse +
len;
102 uint16_t v1, v2, v3, v4, v5, v6;
105 if (
len >= 10 && time_to_parse[4] ==
'-') {
107 if (!parse_digits(p,
end, 4, v1))
109 if (!expect_char(p,
end,
'-'))
111 if (!parse_digits(p,
end, 2, v2))
113 if (!expect_char(p,
end,
'-'))
115 if (!parse_digits(p,
end, 2, v3))
127 if (!expect_char(p,
end,
' '))
131 if (!parse_digits(p,
end, 2, v4))
133 if (!expect_char(p,
end,
':'))
135 if (!parse_digits(p,
end, 2, v5))
147 if (!expect_char(p,
end,
':'))
149 if (!parse_digits(p,
end, 2, v6))
157 if (
len >= 5 && time_to_parse[2] ==
':') {
158 if (!parse_digits(p,
end, 2, v1))
160 if (!expect_char(p,
end,
':'))
162 if (!parse_digits(p,
end, 2, v2))
174 if (!expect_char(p,
end,
':'))
176 if (!parse_digits(p,
end, 2, v3))
207 uint16_t days_in_year = (this->
year % 4 == 0) ? 366 : 365;
225 uint16_t days_in_year = (this->
year % 4 == 0) ? 366 : 365;
239 for (
int i = 1970; i < this->
year; i++)
240 res += (i % 4 == 0) ? 366 : 365;
242 if (use_day_of_year) {
245 for (
int i = 1; i < this->
month; i++)
260#ifdef USE_TIME_TIMEZONE
286 if (dst_valid && std_valid) {
289 }
else if (dst_valid) {
292 }
else if (std_valid) {
308#ifdef USE_TIME_TIMEZONE
309 time_t now = ::time(
nullptr);
331 if (current >=
end) {
bool is_in_dst(time_t utc_epoch, const ParsedTimezone &tz)
Check if a given UTC epoch falls within DST for the parsed timezone.
const ParsedTimezone & get_global_tz()
Get the global timezone.
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.
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]
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)