ESPHome 2026.1.5
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 std::string &time_to_parse, 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
79 if (sscanf(time_to_parse.c_str(), "%04hu-%02hhu-%02hhu %02hhu:%02hhu:%02hhu %n", &year, &month, &day, // NOLINT
80 &hour, // NOLINT
81 &minute, // NOLINT
82 &second, &num) == 6 && // NOLINT
83 num == static_cast<int>(time_to_parse.size())) {
84 esp_time.year = year;
85 esp_time.month = month;
86 esp_time.day_of_month = day;
87 esp_time.hour = hour;
88 esp_time.minute = minute;
89 esp_time.second = second;
90 } else if (sscanf(time_to_parse.c_str(), "%04hu-%02hhu-%02hhu %02hhu:%02hhu %n", &year, &month, &day, // NOLINT
91 &hour, // NOLINT
92 &minute, &num) == 5 && // NOLINT
93 num == static_cast<int>(time_to_parse.size())) {
94 esp_time.year = year;
95 esp_time.month = month;
96 esp_time.day_of_month = day;
97 esp_time.hour = hour;
98 esp_time.minute = minute;
99 esp_time.second = 0;
100 } else if (sscanf(time_to_parse.c_str(), "%02hhu:%02hhu:%02hhu %n", &hour, &minute, &second, &num) == 3 && // NOLINT
101 num == static_cast<int>(time_to_parse.size())) {
102 esp_time.hour = hour;
103 esp_time.minute = minute;
104 esp_time.second = second;
105 } else if (sscanf(time_to_parse.c_str(), "%02hhu:%02hhu %n", &hour, &minute, &num) == 2 && // NOLINT
106 num == static_cast<int>(time_to_parse.size())) {
107 esp_time.hour = hour;
108 esp_time.minute = minute;
109 esp_time.second = 0;
110 } else if (sscanf(time_to_parse.c_str(), "%04hu-%02hhu-%02hhu %n", &year, &month, &day, &num) == 3 && // NOLINT
111 num == static_cast<int>(time_to_parse.size())) {
112 esp_time.year = year;
113 esp_time.month = month;
114 esp_time.day_of_month = day;
115 } else {
116 return false;
117 }
118 return true;
119}
120
122 this->timestamp++;
123 if (!increment_time_value(this->second, 0, 60))
124 return;
125
126 // second roll-over, increment minute
127 if (!increment_time_value(this->minute, 0, 60))
128 return;
129
130 // minute roll-over, increment hour
131 if (!increment_time_value(this->hour, 0, 24))
132 return;
133
134 // hour roll-over, increment day
136
137 if (increment_time_value(this->day_of_month, 1, days_in_month(this->month, this->year) + 1)) {
138 // day of month roll-over, increment month
139 increment_time_value(this->month, 1, 13);
140 }
141
142 uint16_t days_in_year = (this->year % 4 == 0) ? 366 : 365;
143 if (increment_time_value(this->day_of_year, 1, days_in_year + 1)) {
144 // day of year roll-over, increment year
145 this->year++;
146 }
147}
148
150 this->timestamp += 86400;
151
152 // increment day
154
155 if (increment_time_value(this->day_of_month, 1, days_in_month(this->month, this->year) + 1)) {
156 // day of month roll-over, increment month
157 increment_time_value(this->month, 1, 13);
158 }
159
160 uint16_t days_in_year = (this->year % 4 == 0) ? 366 : 365;
161 if (increment_time_value(this->day_of_year, 1, days_in_year + 1)) {
162 // day of year roll-over, increment year
163 this->year++;
164 }
165}
166
167void ESPTime::recalc_timestamp_utc(bool use_day_of_year) {
168 time_t res = 0;
169 if (!this->fields_in_range()) {
170 this->timestamp = -1;
171 return;
172 }
173
174 for (int i = 1970; i < this->year; i++)
175 res += (i % 4 == 0) ? 366 : 365;
176
177 if (use_day_of_year) {
178 res += this->day_of_year - 1;
179 } else {
180 for (int i = 1; i < this->month; i++)
181 res += days_in_month(i, this->year);
182 res += this->day_of_month - 1;
183 }
184
185 res *= 24;
186 res += this->hour;
187 res *= 60;
188 res += this->minute;
189 res *= 60;
190 res += this->second;
191 this->timestamp = res;
192}
193
195 struct tm tm;
196
197 tm.tm_year = this->year - 1900;
198 tm.tm_mon = this->month - 1;
199 tm.tm_mday = this->day_of_month;
200 tm.tm_hour = this->hour;
201 tm.tm_min = this->minute;
202 tm.tm_sec = this->second;
203 tm.tm_isdst = -1;
204
205 this->timestamp = mktime(&tm);
206}
207
209 time_t now = ::time(nullptr);
210 struct tm local_tm = *::localtime(&now);
211 local_tm.tm_isdst = 0; // Cause mktime to ignore daylight saving time because we want to include it in the offset.
212 time_t local_time = mktime(&local_tm);
213 struct tm utc_tm = *::gmtime(&now);
214 time_t utc_time = mktime(&utc_tm);
215 return static_cast<int32_t>(local_time - utc_time);
216}
217
218bool ESPTime::operator<(const ESPTime &other) const { return this->timestamp < other.timestamp; }
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; }
223
224template<typename T> bool increment_time_value(T &current, uint16_t begin, uint16_t end) {
225 current++;
226 if (current >= end) {
227 current = begin;
228 return true;
229 }
230 return false;
231}
232
233} // 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:595
bool increment_time_value(T &current, uint16_t begin, uint16_t end)
Definition time.cpp:224
A more user-friendly version of struct tm from time.h.
Definition time.h:16
void increment_second()
Increment this clock instance by one second.
Definition time.cpp:121
static int32_t timezone_offset()
Definition time.cpp:208
uint8_t minute
minutes after the hour [0-59]
Definition time.h:25
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:167
bool operator<(const ESPTime &other) const
Definition time.cpp:218
uint8_t second
seconds after the minute [0-60]
Definition time.h:23
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
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:222
uint8_t hour
hours since midnight [0-23]
Definition time.h:27
time_t timestamp
unix epoch time (seconds since UTC Midnight January 1, 1970)
Definition time.h:41
void increment_day()
Increment this clock instance by one day.
Definition time.cpp:149
uint16_t day_of_year
day of the year [1-366]
Definition time.h:33
static bool strptime(const std::string &time_to_parse, ESPTime &esp_time)
Convert a string to ESPTime struct as specified by the format argument.
Definition time.cpp:70
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
bool operator<=(const ESPTime &other) const
Definition time.cpp:219
bool operator==(const ESPTime &other) const
Definition time.cpp:220
void recalc_timestamp_local()
Recalculate the timestamp field from the other fields of this ESPTime instance assuming local fields.
Definition time.cpp:194
static constexpr size_t STRFTIME_BUFFER_SIZE
Buffer size required for strftime output.
Definition time.h:18
uint8_t day_of_month
day of the month [1-31]
Definition time.h:31
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:37
bool fields_in_range() const
Check if all time fields of this ESPTime are in range.
Definition time.h:76
uint8_t month
month; january=1 [1-12]
Definition time.h:35
bool operator>=(const ESPTime &other) const
Definition time.cpp:221
uint8_t day_of_week
day of the week; sunday=1 [1-7]
Definition time.h:29
uint8_t end[39]
Definition sun_gtil2.cpp:17