ESPHome  2023.3.1
real_time_clock.cpp
Go to the documentation of this file.
1 #include "real_time_clock.h"
2 #include "esphome/core/log.h"
3 #include "lwip/opt.h"
4 #ifdef USE_ESP8266
5 #include "sys/time.h"
6 #endif
7 #ifdef USE_RP2040
8 #include <sys/time.h>
9 #endif
10 #include <cerrno>
11 
12 namespace esphome {
13 namespace time {
14 
15 static const char *const TAG = "time";
16 
19  this->apply_timezone_();
21 }
22 void RealTimeClock::synchronize_epoch_(uint32_t epoch) {
23  // Update UTC epoch time.
24  struct timeval timev {
25  .tv_sec = static_cast<time_t>(epoch), .tv_usec = 0,
26  };
27  ESP_LOGVV(TAG, "Got epoch %u", epoch);
28  timezone tz = {0, 0};
29  int ret = settimeofday(&timev, &tz);
30  if (ret == EINVAL) {
31  // Some ESP8266 frameworks abort when timezone parameter is not NULL
32  // while ESP32 expects it not to be NULL
33  ret = settimeofday(&timev, nullptr);
34  }
35 
36  // Move timezone back to local timezone.
37  this->apply_timezone_();
38 
39  if (ret != 0) {
40  ESP_LOGW(TAG, "setimeofday() failed with code %d", ret);
41  }
42 
43  auto time = this->now();
44  ESP_LOGD(TAG, "Synchronized time: %04d-%02d-%02d %02d:%02d:%02d", time.year, time.month, time.day_of_month, time.hour,
45  time.minute, time.second);
46 
47  this->time_sync_callback_.call();
48 }
49 
51  setenv("TZ", this->timezone_.c_str(), 1);
52  tzset();
53 }
54 
55 size_t ESPTime::strftime(char *buffer, size_t buffer_len, const char *format) {
56  struct tm c_tm = this->to_c_tm();
57  return ::strftime(buffer, buffer_len, format, &c_tm);
58 }
59 ESPTime ESPTime::from_c_tm(struct tm *c_tm, time_t c_time) {
60  ESPTime res{};
61  res.second = uint8_t(c_tm->tm_sec);
62  res.minute = uint8_t(c_tm->tm_min);
63  res.hour = uint8_t(c_tm->tm_hour);
64  res.day_of_week = uint8_t(c_tm->tm_wday + 1);
65  res.day_of_month = uint8_t(c_tm->tm_mday);
66  res.day_of_year = uint16_t(c_tm->tm_yday + 1);
67  res.month = uint8_t(c_tm->tm_mon + 1);
68  res.year = uint16_t(c_tm->tm_year + 1900);
69  res.is_dst = bool(c_tm->tm_isdst);
70  res.timestamp = c_time;
71  return res;
72 }
73 struct tm ESPTime::to_c_tm() {
74  struct tm c_tm {};
75  c_tm.tm_sec = this->second;
76  c_tm.tm_min = this->minute;
77  c_tm.tm_hour = this->hour;
78  c_tm.tm_mday = this->day_of_month;
79  c_tm.tm_mon = this->month - 1;
80  c_tm.tm_year = this->year - 1900;
81  c_tm.tm_wday = this->day_of_week - 1;
82  c_tm.tm_yday = this->day_of_year - 1;
83  c_tm.tm_isdst = this->is_dst;
84  return c_tm;
85 }
86 std::string ESPTime::strftime(const std::string &format) {
87  std::string timestr;
88  timestr.resize(format.size() * 4);
89  struct tm c_tm = this->to_c_tm();
90  size_t len = ::strftime(&timestr[0], timestr.size(), format.c_str(), &c_tm);
91  while (len == 0) {
92  timestr.resize(timestr.size() * 2);
93  len = ::strftime(&timestr[0], timestr.size(), format.c_str(), &c_tm);
94  }
95  timestr.resize(len);
96  return timestr;
97 }
98 
99 template<typename T> bool increment_time_value(T &current, uint16_t begin, uint16_t end) {
100  current++;
101  if (current >= end) {
102  current = begin;
103  return true;
104  }
105  return false;
106 }
107 
108 static bool is_leap_year(uint32_t year) { return (year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0); }
109 
110 static uint8_t days_in_month(uint8_t month, uint16_t year) {
111  static const uint8_t DAYS_IN_MONTH[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
112  uint8_t days = DAYS_IN_MONTH[month];
113  if (month == 2 && is_leap_year(year))
114  return 29;
115  return days;
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 
192  int32_t offset = 0;
193  time_t now = ::time(nullptr);
194  auto local = ESPTime::from_epoch_local(now);
195  auto utc = ESPTime::from_epoch_utc(now);
196  bool negative = utc.hour > local.hour && local.day_of_year <= utc.day_of_year;
197 
198  if (utc.minute > local.minute) {
199  local.minute += 60;
200  local.hour -= 1;
201  }
202  offset += (local.minute - utc.minute) * 60;
203 
204  if (negative) {
205  offset -= (utc.hour - local.hour) * 3600;
206  } else {
207  if (utc.hour > local.hour) {
208  local.hour += 24;
209  }
210  offset += (local.hour - utc.hour) * 3600;
211  }
212  return offset;
213 }
214 
215 bool ESPTime::operator<(ESPTime other) { return this->timestamp < other.timestamp; }
216 bool ESPTime::operator<=(ESPTime other) { return this->timestamp <= other.timestamp; }
217 bool ESPTime::operator==(ESPTime other) { return this->timestamp == other.timestamp; }
218 bool ESPTime::operator>=(ESPTime other) { return this->timestamp >= other.timestamp; }
219 bool ESPTime::operator>(ESPTime other) { return this->timestamp > other.timestamp; }
220 
221 } // namespace time
222 } // namespace esphome
ESPTime now()
Get the time in the currently defined timezone.
time_t timestamp
unix epoch time (seconds since UTC Midnight January 1, 1970)
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.
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...
void increment_second()
Increment this clock instance by one second.
bool increment_time_value(T &current, uint16_t begin, uint16_t end)
static ESPTime from_epoch_local(time_t epoch)
Convert an UTC epoch timestamp to a local time ESPTime instance.
CallbackManager< void()> time_sync_callback_
void increment_day()
Increment this clock instance by one day.
A more user-friendly version of struct tm from time.h.
bool operator<=(ESPTime other)
static ESPTime from_epoch_utc(time_t epoch)
Convert an UTC epoch timestamp to a UTC time ESPTime instance.
uint8_t second
seconds after the minute [0-60]
bool operator==(ESPTime other)
bool operator>(ESPTime other)
bool operator<(ESPTime other)
void call_setup() override
Definition: component.cpp:187
std::string size_t len
Definition: helpers.h:286
Definition: a4988.cpp:4
void synchronize_epoch_(uint32_t epoch)
Report a unix epoch as current time.
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)...
static int32_t timezone_offset()
bool operator>=(ESPTime other)