ESPHome 2025.12.0-dev
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 <cinttypes>
5
6namespace esphome {
7
8uint8_t days_in_month(uint8_t month, uint16_t year) {
9 static const uint8_t DAYS_IN_MONTH[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
10 if (month == 2 && (year % 4 == 0))
11 return 29;
12 return DAYS_IN_MONTH[month];
13}
14
15size_t ESPTime::strftime(char *buffer, size_t buffer_len, const char *format) {
16 struct tm c_tm = this->to_c_tm();
17 return ::strftime(buffer, buffer_len, format, &c_tm);
18}
19
20ESPTime ESPTime::from_c_tm(struct tm *c_tm, time_t c_time) {
21 ESPTime res{};
22 res.second = uint8_t(c_tm->tm_sec);
23 res.minute = uint8_t(c_tm->tm_min);
24 res.hour = uint8_t(c_tm->tm_hour);
25 res.day_of_week = uint8_t(c_tm->tm_wday + 1);
26 res.day_of_month = uint8_t(c_tm->tm_mday);
27 res.day_of_year = uint16_t(c_tm->tm_yday + 1);
28 res.month = uint8_t(c_tm->tm_mon + 1);
29 res.year = uint16_t(c_tm->tm_year + 1900);
30 res.is_dst = bool(c_tm->tm_isdst);
31 res.timestamp = c_time;
32 return res;
33}
34
35struct tm ESPTime::to_c_tm() {
36 struct tm c_tm {};
37 c_tm.tm_sec = this->second;
38 c_tm.tm_min = this->minute;
39 c_tm.tm_hour = this->hour;
40 c_tm.tm_mday = this->day_of_month;
41 c_tm.tm_mon = this->month - 1;
42 c_tm.tm_year = this->year - 1900;
43 c_tm.tm_wday = this->day_of_week - 1;
44 c_tm.tm_yday = this->day_of_year - 1;
45 c_tm.tm_isdst = this->is_dst;
46 return c_tm;
47}
48
49std::string ESPTime::strftime(const char *format) {
50 struct tm c_tm = this->to_c_tm();
51 char buf[128];
52 size_t len = ::strftime(buf, sizeof(buf), format, &c_tm);
53 if (len > 0) {
54 return std::string(buf, len);
55 }
56 return "ERROR";
57}
58
59std::string ESPTime::strftime(const std::string &format) { return this->strftime(format.c_str()); }
60
61bool ESPTime::strptime(const std::string &time_to_parse, ESPTime &esp_time) {
62 uint16_t year;
63 uint8_t month;
64 uint8_t day;
65 uint8_t hour;
66 uint8_t minute;
67 uint8_t second;
68 int num;
69
70 if (sscanf(time_to_parse.c_str(), "%04hu-%02hhu-%02hhu %02hhu:%02hhu:%02hhu %n", &year, &month, &day, // NOLINT
71 &hour, // NOLINT
72 &minute, // NOLINT
73 &second, &num) == 6 && // NOLINT
74 num == static_cast<int>(time_to_parse.size())) {
75 esp_time.year = year;
76 esp_time.month = month;
77 esp_time.day_of_month = day;
78 esp_time.hour = hour;
79 esp_time.minute = minute;
80 esp_time.second = second;
81 } else if (sscanf(time_to_parse.c_str(), "%04hu-%02hhu-%02hhu %02hhu:%02hhu %n", &year, &month, &day, // NOLINT
82 &hour, // NOLINT
83 &minute, &num) == 5 && // NOLINT
84 num == static_cast<int>(time_to_parse.size())) {
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 = 0;
91 } else if (sscanf(time_to_parse.c_str(), "%02hhu:%02hhu:%02hhu %n", &hour, &minute, &second, &num) == 3 && // NOLINT
92 num == static_cast<int>(time_to_parse.size())) {
93 esp_time.hour = hour;
94 esp_time.minute = minute;
95 esp_time.second = second;
96 } else if (sscanf(time_to_parse.c_str(), "%02hhu:%02hhu %n", &hour, &minute, &num) == 2 && // NOLINT
97 num == static_cast<int>(time_to_parse.size())) {
98 esp_time.hour = hour;
99 esp_time.minute = minute;
100 esp_time.second = 0;
101 } else if (sscanf(time_to_parse.c_str(), "%04hu-%02hhu-%02hhu %n", &year, &month, &day, &num) == 3 && // NOLINT
102 num == static_cast<int>(time_to_parse.size())) {
103 esp_time.year = year;
104 esp_time.month = month;
105 esp_time.day_of_month = day;
106 } else {
107 return false;
108 }
109 return true;
110}
111
113 this->timestamp++;
114 if (!increment_time_value(this->second, 0, 60))
115 return;
116
117 // second roll-over, increment minute
118 if (!increment_time_value(this->minute, 0, 60))
119 return;
120
121 // minute roll-over, increment hour
122 if (!increment_time_value(this->hour, 0, 24))
123 return;
124
125 // hour roll-over, increment day
127
128 if (increment_time_value(this->day_of_month, 1, days_in_month(this->month, this->year) + 1)) {
129 // day of month roll-over, increment month
130 increment_time_value(this->month, 1, 13);
131 }
132
133 uint16_t days_in_year = (this->year % 4 == 0) ? 366 : 365;
134 if (increment_time_value(this->day_of_year, 1, days_in_year + 1)) {
135 // day of year roll-over, increment year
136 this->year++;
137 }
138}
139
141 this->timestamp += 86400;
142
143 // increment day
145
146 if (increment_time_value(this->day_of_month, 1, days_in_month(this->month, this->year) + 1)) {
147 // day of month roll-over, increment month
148 increment_time_value(this->month, 1, 13);
149 }
150
151 uint16_t days_in_year = (this->year % 4 == 0) ? 366 : 365;
152 if (increment_time_value(this->day_of_year, 1, days_in_year + 1)) {
153 // day of year roll-over, increment year
154 this->year++;
155 }
156}
157
158void ESPTime::recalc_timestamp_utc(bool use_day_of_year) {
159 time_t res = 0;
160 if (!this->fields_in_range()) {
161 this->timestamp = -1;
162 return;
163 }
164
165 for (int i = 1970; i < this->year; i++)
166 res += (i % 4 == 0) ? 366 : 365;
167
168 if (use_day_of_year) {
169 res += this->day_of_year - 1;
170 } else {
171 for (int i = 1; i < this->month; i++)
172 res += days_in_month(i, this->year);
173 res += this->day_of_month - 1;
174 }
175
176 res *= 24;
177 res += this->hour;
178 res *= 60;
179 res += this->minute;
180 res *= 60;
181 res += this->second;
182 this->timestamp = res;
183}
184
186 struct tm tm;
187
188 tm.tm_year = this->year - 1900;
189 tm.tm_mon = this->month - 1;
190 tm.tm_mday = this->day_of_month;
191 tm.tm_hour = this->hour;
192 tm.tm_min = this->minute;
193 tm.tm_sec = this->second;
194 tm.tm_isdst = -1;
195
196 this->timestamp = mktime(&tm);
197}
198
200 time_t now = ::time(nullptr);
201 struct tm local_tm = *::localtime(&now);
202 local_tm.tm_isdst = 0; // Cause mktime to ignore daylight saving time because we want to include it in the offset.
203 time_t local_time = mktime(&local_tm);
204 struct tm utc_tm = *::gmtime(&now);
205 time_t utc_time = mktime(&utc_tm);
206 return static_cast<int32_t>(local_time - utc_time);
207}
208
209bool ESPTime::operator<(const ESPTime &other) const { return this->timestamp < other.timestamp; }
210bool ESPTime::operator<=(const ESPTime &other) const { return this->timestamp <= other.timestamp; }
211bool ESPTime::operator==(const ESPTime &other) const { return this->timestamp == other.timestamp; }
212bool ESPTime::operator>=(const ESPTime &other) const { return this->timestamp >= other.timestamp; }
213bool ESPTime::operator>(const ESPTime &other) const { return this->timestamp > other.timestamp; }
214
215template<typename T> bool increment_time_value(T &current, uint16_t begin, uint16_t end) {
216 current++;
217 if (current >= end) {
218 current = begin;
219 return true;
220 }
221 return false;
222}
223
224} // 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:8
std::string size_t len
Definition helpers.h:483
bool increment_time_value(T &current, uint16_t begin, uint16_t end)
Definition time.cpp:215
A more user-friendly version of struct tm from time.h.
Definition time.h:15
void increment_second()
Increment this clock instance by one second.
Definition time.cpp:112
static int32_t timezone_offset()
Definition time.cpp:199
uint8_t minute
minutes after the hour [0-59]
Definition time.h:21
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:158
bool operator<(const ESPTime &other) const
Definition time.cpp:209
uint8_t second
seconds after the minute [0-60]
Definition time.h:19
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:15
bool operator>(const ESPTime &other) const
Definition time.cpp:213
uint8_t hour
hours since midnight [0-23]
Definition time.h:23
time_t timestamp
unix epoch time (seconds since UTC Midnight January 1, 1970)
Definition time.h:37
void increment_day()
Increment this clock instance by one day.
Definition time.cpp:140
uint16_t day_of_year
day of the year [1-366]
Definition time.h:29
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:61
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:20
bool operator<=(const ESPTime &other) const
Definition time.cpp:210
bool operator==(const ESPTime &other) const
Definition time.cpp:211
void recalc_timestamp_local()
Recalculate the timestamp field from the other fields of this ESPTime instance assuming local fields.
Definition time.cpp:185
uint8_t day_of_month
day of the month [1-31]
Definition time.h:27
struct tm to_c_tm()
Convert this ESPTime instance back to a tm struct.
Definition time.cpp:35
uint16_t year
year
Definition time.h:33
bool fields_in_range() const
Check if all time fields of this ESPTime are in range.
Definition time.h:64
uint8_t month
month; january=1 [1-12]
Definition time.h:31
bool operator>=(const ESPTime &other) const
Definition time.cpp:212
uint8_t day_of_week
day of the week; sunday=1 [1-7]
Definition time.h:25
uint8_t end[39]
Definition sun_gtil2.cpp:17