ESPHome  2024.12.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 uint8_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 
15 size_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 
20 ESPTime 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 
35 struct 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 
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 }
145 
147  this->timestamp += 86400;
148 
149  // increment day
150  increment_time_value(this->day_of_week, 1, 8);
151 
152  if (increment_time_value(this->day_of_month, 1, days_in_month(this->month, this->year) + 1)) {
153  // day of month roll-over, increment month
154  increment_time_value(this->month, 1, 13);
155  }
156 
157  uint16_t days_in_year = (this->year % 4 == 0) ? 366 : 365;
158  if (increment_time_value(this->day_of_year, 1, days_in_year + 1)) {
159  // day of year roll-over, increment year
160  this->year++;
161  }
162 }
163 
164 void ESPTime::recalc_timestamp_utc(bool use_day_of_year) {
165  time_t res = 0;
166  if (!this->fields_in_range()) {
167  this->timestamp = -1;
168  return;
169  }
170 
171  for (int i = 1970; i < this->year; i++)
172  res += (i % 4 == 0) ? 366 : 365;
173 
174  if (use_day_of_year) {
175  res += this->day_of_year - 1;
176  } else {
177  for (int i = 1; i < this->month; i++)
178  res += days_in_month(i, this->year);
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 
192  struct tm tm;
193 
194  tm.tm_year = this->year - 1900;
195  tm.tm_mon = this->month - 1;
196  tm.tm_mday = this->day_of_month;
197  tm.tm_hour = this->hour;
198  tm.tm_min = this->minute;
199  tm.tm_sec = this->second;
200 
201  this->timestamp = mktime(&tm);
202 }
203 
205  int32_t offset = 0;
206  time_t now = ::time(nullptr);
207  auto local = ESPTime::from_epoch_local(now);
208  auto utc = ESPTime::from_epoch_utc(now);
209  bool negative = utc.hour > local.hour && local.day_of_year <= utc.day_of_year;
210 
211  if (utc.minute > local.minute) {
212  local.minute += 60;
213  local.hour -= 1;
214  }
215  offset += (local.minute - utc.minute) * 60;
216 
217  if (negative) {
218  offset -= (utc.hour - local.hour) * 3600;
219  } else {
220  if (utc.hour > local.hour) {
221  local.hour += 24;
222  }
223  offset += (local.hour - utc.hour) * 3600;
224  }
225  return offset;
226 }
227 
228 bool ESPTime::operator<(ESPTime other) { return this->timestamp < other.timestamp; }
229 bool ESPTime::operator<=(ESPTime other) { return this->timestamp <= other.timestamp; }
230 bool ESPTime::operator==(ESPTime other) { return this->timestamp == other.timestamp; }
231 bool ESPTime::operator>=(ESPTime other) { return this->timestamp >= other.timestamp; }
232 bool ESPTime::operator>(ESPTime other) { return this->timestamp > other.timestamp; }
233 
234 template<typename T> bool increment_time_value(T &current, uint16_t begin, uint16_t end) {
235  current++;
236  if (current >= end) {
237  current = begin;
238  return true;
239  }
240  return false;
241 }
242 
243 } // 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:92
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
void recalc_timestamp_local()
Recalculate the timestamp field from the other fields of this ESPTime instance assuming local fields...
Definition: time.cpp:191
static int32_t timezone_offset()
Definition: time.cpp:204
A more user-friendly version of struct tm from time.h.
Definition: time.h:15
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 is_dst
daylight saving time flag
Definition: time.h:35
void increment_day()
Increment this clock instance by one day.
Definition: time.cpp:146
uint16_t day_of_year
day of the year [1-366]
Definition: time.h:29
bool operator<=(ESPTime other)
Definition: time.cpp:229
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:8
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:83
bool operator>(ESPTime other)
Definition: time.cpp:232
bool increment_time_value(T &current, uint16_t begin, uint16_t end)
Definition: time.cpp:234
uint8_t second
seconds after the minute [0-60]
Definition: time.h:19
time_t timestamp
unix epoch time (seconds since UTC Midnight January 1, 1970)
Definition: time.h:37
struct tm to_c_tm()
Convert this ESPTime instance back to a tm struct.
Definition: time.cpp:35
uint8_t minute
minutes after the hour [0-59]
Definition: time.h:21
bool operator==(ESPTime other)
Definition: time.cpp:230
uint8_t day_of_week
day of the week; sunday=1 [1-7]
Definition: time.h:25
uint16_t year
year
Definition: time.h:33
std::string size_t len
Definition: helpers.h:293
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:164
uint8_t month
month; january=1 [1-12]
Definition: time.h:31
uint8_t end[39]
Definition: sun_gtil2.cpp:31
uint8_t hour
hours since midnight [0-23]
Definition: time.h:23
bool operator<(ESPTime other)
Definition: time.cpp:228
uint8_t day_of_month
day of the month [1-31]
Definition: time.h:27
uint8_t month
Definition: date_entity.h:123
bool operator>=(ESPTime other)
Definition: time.cpp:231
bool fields_in_range() const
Check if all time fields of this ESPTime are in range.
Definition: time.h:62
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