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