ESPHome  2024.4.1
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(), "%02hhu:%02hhu:%02hhu %n", &hour, &minute, &second, &num) == 3 && // NOLINT
88  num == time_to_parse.size()) {
89  esp_time.hour = hour;
90  esp_time.minute = minute;
91  esp_time.second = second;
92  } else if (sscanf(time_to_parse.c_str(), "%02hhu:%02hhu %n", &hour, &minute, &num) == 2 && // NOLINT
93  num == time_to_parse.size()) {
94  esp_time.hour = hour;
95  esp_time.minute = minute;
96  esp_time.second = 0;
97  } else if (sscanf(time_to_parse.c_str(), "%04hu-%02hhu-%02hhu %n", &year, &month, &day, &num) == 3 && // NOLINT
98  num == time_to_parse.size()) {
99  esp_time.year = year;
100  esp_time.month = month;
101  esp_time.day_of_month = day;
102  } else {
103  return false;
104  }
105  return true;
106 }
107 
109  this->timestamp++;
110  if (!increment_time_value(this->second, 0, 60))
111  return;
112 
113  // second roll-over, increment minute
114  if (!increment_time_value(this->minute, 0, 60))
115  return;
116 
117  // minute roll-over, increment hour
118  if (!increment_time_value(this->hour, 0, 24))
119  return;
120 
121  // hour roll-over, increment day
122  increment_time_value(this->day_of_week, 1, 8);
123 
124  if (increment_time_value(this->day_of_month, 1, days_in_month(this->month, this->year) + 1)) {
125  // day of month roll-over, increment month
126  increment_time_value(this->month, 1, 13);
127  }
128 
129  uint16_t days_in_year = (this->year % 4 == 0) ? 366 : 365;
130  if (increment_time_value(this->day_of_year, 1, days_in_year + 1)) {
131  // day of year roll-over, increment year
132  this->year++;
133  }
134 }
136  this->timestamp += 86400;
137 
138  // increment day
139  increment_time_value(this->day_of_week, 1, 8);
140 
141  if (increment_time_value(this->day_of_month, 1, days_in_month(this->month, this->year) + 1)) {
142  // day of month roll-over, increment month
143  increment_time_value(this->month, 1, 13);
144  }
145 
146  uint16_t days_in_year = (this->year % 4 == 0) ? 366 : 365;
147  if (increment_time_value(this->day_of_year, 1, days_in_year + 1)) {
148  // day of year roll-over, increment year
149  this->year++;
150  }
151 }
152 void ESPTime::recalc_timestamp_utc(bool use_day_of_year) {
153  time_t res = 0;
154 
155  if (!this->fields_in_range()) {
156  this->timestamp = -1;
157  return;
158  }
159 
160  for (int i = 1970; i < this->year; i++)
161  res += is_leap_year(i) ? 366 : 365;
162 
163  if (use_day_of_year) {
164  res += this->day_of_year - 1;
165  } else {
166  for (int i = 1; i < this->month; i++)
167  res += days_in_month(i, this->year);
168 
169  res += this->day_of_month - 1;
170  }
171 
172  res *= 24;
173  res += this->hour;
174  res *= 60;
175  res += this->minute;
176  res *= 60;
177  res += this->second;
178  this->timestamp = res;
179 }
180 
182  int32_t offset = 0;
183  time_t now = ::time(nullptr);
184  auto local = ESPTime::from_epoch_local(now);
185  auto utc = ESPTime::from_epoch_utc(now);
186  bool negative = utc.hour > local.hour && local.day_of_year <= utc.day_of_year;
187 
188  if (utc.minute > local.minute) {
189  local.minute += 60;
190  local.hour -= 1;
191  }
192  offset += (local.minute - utc.minute) * 60;
193 
194  if (negative) {
195  offset -= (utc.hour - local.hour) * 3600;
196  } else {
197  if (utc.hour > local.hour) {
198  local.hour += 24;
199  }
200  offset += (local.hour - utc.hour) * 3600;
201  }
202  return offset;
203 }
204 
205 bool ESPTime::operator<(ESPTime other) { return this->timestamp < other.timestamp; }
206 bool ESPTime::operator<=(ESPTime other) { return this->timestamp <= other.timestamp; }
207 bool ESPTime::operator==(ESPTime other) { return this->timestamp == other.timestamp; }
208 bool ESPTime::operator>=(ESPTime other) { return this->timestamp >= other.timestamp; }
209 bool ESPTime::operator>(ESPTime other) { return this->timestamp > other.timestamp; }
210 
211 template<typename T> bool increment_time_value(T &current, uint16_t begin, uint16_t end) {
212  current++;
213  if (current >= end) {
214  current = begin;
215  return true;
216  }
217  return false;
218 }
219 
220 } // namespace esphome
uint16_t year
Definition: date_entity.h:108
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:181
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:135
uint16_t day_of_year
day of the year [1-366]
Definition: time.h:31
bool operator<=(ESPTime other)
Definition: time.cpp:206
void increment_second()
Increment this clock instance by one second.
Definition: time.cpp:108
uint8_t days_in_month(uint8_t month, uint16_t year)
Definition: time.cpp:10
uint8_t day
Definition: date_entity.h:110
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:209
bool increment_time_value(T &current, uint16_t begin, uint16_t end)
Definition: time.cpp:211
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:207
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
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
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:152
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:205
uint8_t day_of_month
day of the month [1-31]
Definition: time.h:29
uint8_t month
Definition: date_entity.h:109
bool operator>=(ESPTime other)
Definition: time.cpp:208
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