ESPHome 2026.2.1
Loading...
Searching...
No Matches
time.cpp
Go to the documentation of this file.
1#include "time.h" // NOLINT
2#include "helpers.h"
3
4#include <algorithm>
5#include <cinttypes>
6
7namespace esphome {
8
9uint8_t days_in_month(uint8_t month, uint16_t year) {
10 static const uint8_t DAYS_IN_MONTH[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
11 if (month == 2 && (year % 4 == 0))
12 return 29;
13 return DAYS_IN_MONTH[month];
14}
15
16size_t ESPTime::strftime(char *buffer, size_t buffer_len, const char *format) {
17 struct tm c_tm = this->to_c_tm();
18 return ::strftime(buffer, buffer_len, format, &c_tm);
19}
20
21size_t ESPTime::strftime_to(std::span<char, STRFTIME_BUFFER_SIZE> buffer, const char *format) {
22 struct tm c_tm = this->to_c_tm();
23 size_t len = ::strftime(buffer.data(), buffer.size(), format, &c_tm);
24 if (len > 0) {
25 return len;
26 }
27 // Write "ERROR" to buffer on failure for consistent behavior
28 constexpr char error_str[] = "ERROR";
29 std::copy_n(error_str, sizeof(error_str), buffer.data());
30 return sizeof(error_str) - 1; // Length excluding null terminator
31}
32
33ESPTime ESPTime::from_c_tm(struct tm *c_tm, time_t c_time) {
34 ESPTime res{};
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;
45 return res;
46}
47
48struct tm ESPTime::to_c_tm() {
49 struct tm c_tm {};
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;
59 return c_tm;
60}
61
62std::string ESPTime::strftime(const char *format) {
63 char buf[STRFTIME_BUFFER_SIZE];
64 size_t len = this->strftime_to(buf, format);
65 return std::string(buf, len);
66}
67
68std::string ESPTime::strftime(const std::string &format) { return this->strftime(format.c_str()); }
69
70bool ESPTime::strptime(const char *time_to_parse, size_t len, ESPTime &esp_time) {
71 uint16_t year;
72 uint8_t month;
73 uint8_t day;
74 uint8_t hour;
75 uint8_t minute;
76 uint8_t second;
77 int num;
78 const int ilen = static_cast<int>(len);
79
80 if (sscanf(time_to_parse, "%04hu-%02hhu-%02hhu %02hhu:%02hhu:%02hhu %n", &year, &month, &day, // NOLINT
81 &hour, // NOLINT
82 &minute, // NOLINT
83 &second, &num) == 6 && // NOLINT
84 num == ilen) {
85 esp_time.year = year;
86 esp_time.month = month;
87 esp_time.day_of_month = day;
88 esp_time.hour = hour;
89 esp_time.minute = minute;
90 esp_time.second = second;
91 } else if (sscanf(time_to_parse, "%04hu-%02hhu-%02hhu %02hhu:%02hhu %n", &year, &month, &day, // NOLINT
92 &hour, // NOLINT
93 &minute, &num) == 5 && // NOLINT
94 num == ilen) {
95 esp_time.year = year;
96 esp_time.month = month;
97 esp_time.day_of_month = day;
98 esp_time.hour = hour;
99 esp_time.minute = minute;
100 esp_time.second = 0;
101 } else if (sscanf(time_to_parse, "%02hhu:%02hhu:%02hhu %n", &hour, &minute, &second, &num) == 3 && // NOLINT
102 num == ilen) {
103 esp_time.hour = hour;
104 esp_time.minute = minute;
105 esp_time.second = second;
106 } else if (sscanf(time_to_parse, "%02hhu:%02hhu %n", &hour, &minute, &num) == 2 && // NOLINT
107 num == ilen) {
108 esp_time.hour = hour;
109 esp_time.minute = minute;
110 esp_time.second = 0;
111 } else if (sscanf(time_to_parse, "%04hu-%02hhu-%02hhu %n", &year, &month, &day, &num) == 3 && // NOLINT
112 num == ilen) {
113 esp_time.year = year;
114 esp_time.month = month;
115 esp_time.day_of_month = day;
116 } else {
117 return false;
118 }
119 return true;
120}
121
123 this->timestamp++;
124 if (!increment_time_value(this->second, 0, 60))
125 return;
126
127 // second roll-over, increment minute
128 if (!increment_time_value(this->minute, 0, 60))
129 return;
130
131 // minute roll-over, increment hour
132 if (!increment_time_value(this->hour, 0, 24))
133 return;
134
135 // hour roll-over, increment day
137
138 if (increment_time_value(this->day_of_month, 1, days_in_month(this->month, this->year) + 1)) {
139 // day of month roll-over, increment month
140 increment_time_value(this->month, 1, 13);
141 }
142
143 uint16_t days_in_year = (this->year % 4 == 0) ? 366 : 365;
144 if (increment_time_value(this->day_of_year, 1, days_in_year + 1)) {
145 // day of year roll-over, increment year
146 this->year++;
147 }
148}
149
151 this->timestamp += 86400;
152
153 // increment day
155
156 if (increment_time_value(this->day_of_month, 1, days_in_month(this->month, this->year) + 1)) {
157 // day of month roll-over, increment month
158 increment_time_value(this->month, 1, 13);
159 }
160
161 uint16_t days_in_year = (this->year % 4 == 0) ? 366 : 365;
162 if (increment_time_value(this->day_of_year, 1, days_in_year + 1)) {
163 // day of year roll-over, increment year
164 this->year++;
165 }
166}
167
168void ESPTime::recalc_timestamp_utc(bool use_day_of_year) {
169 time_t res = 0;
170 if (!this->fields_in_range()) {
171 this->timestamp = -1;
172 return;
173 }
174
175 for (int i = 1970; i < this->year; i++)
176 res += (i % 4 == 0) ? 366 : 365;
177
178 if (use_day_of_year) {
179 res += this->day_of_year - 1;
180 } else {
181 for (int i = 1; i < this->month; i++)
182 res += days_in_month(i, this->year);
183 res += this->day_of_month - 1;
184 }
185
186 res *= 24;
187 res += this->hour;
188 res *= 60;
189 res += this->minute;
190 res *= 60;
191 res += this->second;
192 this->timestamp = res;
193}
194
196 struct tm tm;
197
198 tm.tm_year = this->year - 1900;
199 tm.tm_mon = this->month - 1;
200 tm.tm_mday = this->day_of_month;
201 tm.tm_hour = this->hour;
202 tm.tm_min = this->minute;
203 tm.tm_sec = this->second;
204 tm.tm_isdst = -1;
205
206 this->timestamp = mktime(&tm);
207}
208
210 time_t now = ::time(nullptr);
211 struct tm local_tm = *::localtime(&now);
212 local_tm.tm_isdst = 0; // Cause mktime to ignore daylight saving time because we want to include it in the offset.
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);
217}
218
219bool ESPTime::operator<(const ESPTime &other) const { return this->timestamp < other.timestamp; }
220bool ESPTime::operator<=(const ESPTime &other) const { return this->timestamp <= other.timestamp; }
221bool ESPTime::operator==(const ESPTime &other) const { return this->timestamp == other.timestamp; }
222bool ESPTime::operator>=(const ESPTime &other) const { return this->timestamp >= other.timestamp; }
223bool ESPTime::operator>(const ESPTime &other) const { return this->timestamp > other.timestamp; }
224
225template<typename T> bool increment_time_value(T &current, uint16_t begin, uint16_t end) {
226 current++;
227 if (current >= end) {
228 current = begin;
229 return true;
230 }
231 return false;
232}
233
234} // namespace esphome
uint8_t month
Definition date_entity.h:1
uint16_t year
Definition date_entity.h:0
uint8_t day
Definition date_entity.h:2
uint8_t second
uint8_t minute
uint8_t hour
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint8_t days_in_month(uint8_t month, uint16_t year)
Definition time.cpp:9
std::string size_t len
Definition helpers.h:692
bool increment_time_value(T &current, uint16_t begin, uint16_t end)
Definition time.cpp:225
A more user-friendly version of struct tm from time.h.
Definition time.h:17
void increment_second()
Increment this clock instance by one second.
Definition time.cpp:122
static int32_t timezone_offset()
Definition time.cpp:209
uint8_t minute
minutes after the hour [0-59]
Definition time.h:26
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).
Definition time.cpp:168
bool operator<(const ESPTime &other) const
Definition time.cpp:219
uint8_t second
seconds after the minute [0-60]
Definition time.h:24
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.
Definition time.cpp:16
std::string strftime(const std::string &format)
Convert this ESPTime struct to a string as specified by the format argument.
Definition time.cpp:68
size_t strftime_to(std::span< char, STRFTIME_BUFFER_SIZE > buffer, const char *format)
Format time into a fixed-size buffer, returns length written.
Definition time.cpp:21
bool operator>(const ESPTime &other) const
Definition time.cpp:223
uint8_t hour
hours since midnight [0-23]
Definition time.h:28
time_t timestamp
unix epoch time (seconds since UTC Midnight January 1, 1970)
Definition time.h:42
void increment_day()
Increment this clock instance by one day.
Definition time.cpp:150
uint16_t day_of_year
day of the year [1-366]
Definition time.h:34
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.
Definition time.cpp:33
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.
Definition time.cpp:70
bool operator<=(const ESPTime &other) const
Definition time.cpp:220
bool operator==(const ESPTime &other) const
Definition time.cpp:221
void recalc_timestamp_local()
Recalculate the timestamp field from the other fields of this ESPTime instance assuming local fields.
Definition time.cpp:195
static constexpr size_t STRFTIME_BUFFER_SIZE
Buffer size required for strftime output.
Definition time.h:19
uint8_t day_of_month
day of the month [1-31]
Definition time.h:32
struct tm to_c_tm()
Convert this ESPTime instance back to a tm struct.
Definition time.cpp:48
uint16_t year
year
Definition time.h:38
bool fields_in_range() const
Check if all time fields of this ESPTime are in range.
Definition time.h:77
uint8_t month
month; january=1 [1-12]
Definition time.h:36
bool operator>=(const ESPTime &other) const
Definition time.cpp:222
uint8_t day_of_week
day of the week; sunday=1 [1-7]
Definition time.h:30
uint8_t end[39]
Definition sun_gtil2.cpp:17