ESPHome  2024.2.0
time.cpp
Go to the documentation of this file.
1 #include "time.h" // NOLINT
2 
3 namespace esphome {
4 
5 static bool is_leap_year(uint32_t year) { return (year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0); }
6 
7 static uint8_t days_in_month(uint8_t month, uint16_t year) {
8  static const uint8_t DAYS_IN_MONTH[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
9  uint8_t days = DAYS_IN_MONTH[month];
10  if (month == 2 && is_leap_year(year))
11  return 29;
12  return days;
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 ESPTime ESPTime::from_c_tm(struct tm *c_tm, time_t c_time) {
20  ESPTime res{};
21  res.second = uint8_t(c_tm->tm_sec);
22  res.minute = uint8_t(c_tm->tm_min);
23  res.hour = uint8_t(c_tm->tm_hour);
24  res.day_of_week = uint8_t(c_tm->tm_wday + 1);
25  res.day_of_month = uint8_t(c_tm->tm_mday);
26  res.day_of_year = uint16_t(c_tm->tm_yday + 1);
27  res.month = uint8_t(c_tm->tm_mon + 1);
28  res.year = uint16_t(c_tm->tm_year + 1900);
29  res.is_dst = bool(c_tm->tm_isdst);
30  res.timestamp = c_time;
31  return res;
32 }
33 struct tm ESPTime::to_c_tm() {
34  struct tm c_tm {};
35  c_tm.tm_sec = this->second;
36  c_tm.tm_min = this->minute;
37  c_tm.tm_hour = this->hour;
38  c_tm.tm_mday = this->day_of_month;
39  c_tm.tm_mon = this->month - 1;
40  c_tm.tm_year = this->year - 1900;
41  c_tm.tm_wday = this->day_of_week - 1;
42  c_tm.tm_yday = this->day_of_year - 1;
43  c_tm.tm_isdst = this->is_dst;
44  return c_tm;
45 }
46 std::string ESPTime::strftime(const std::string &format) {
47  std::string timestr;
48  timestr.resize(format.size() * 4);
49  struct tm c_tm = this->to_c_tm();
50  size_t len = ::strftime(&timestr[0], timestr.size(), format.c_str(), &c_tm);
51  while (len == 0) {
52  if (timestr.size() >= 128) {
53  // strftime has failed for reasons unrelated to the size of the buffer
54  // so return a formatting error
55  return "ERROR";
56  }
57  timestr.resize(timestr.size() * 2);
58  len = ::strftime(&timestr[0], timestr.size(), format.c_str(), &c_tm);
59  }
60  timestr.resize(len);
61  return timestr;
62 }
63 
65  this->timestamp++;
66  if (!increment_time_value(this->second, 0, 60))
67  return;
68 
69  // second roll-over, increment minute
70  if (!increment_time_value(this->minute, 0, 60))
71  return;
72 
73  // minute roll-over, increment hour
74  if (!increment_time_value(this->hour, 0, 24))
75  return;
76 
77  // hour roll-over, increment day
78  increment_time_value(this->day_of_week, 1, 8);
79 
80  if (increment_time_value(this->day_of_month, 1, days_in_month(this->month, this->year) + 1)) {
81  // day of month roll-over, increment month
82  increment_time_value(this->month, 1, 13);
83  }
84 
85  uint16_t days_in_year = (this->year % 4 == 0) ? 366 : 365;
86  if (increment_time_value(this->day_of_year, 1, days_in_year + 1)) {
87  // day of year roll-over, increment year
88  this->year++;
89  }
90 }
92  this->timestamp += 86400;
93 
94  // increment day
95  increment_time_value(this->day_of_week, 1, 8);
96 
97  if (increment_time_value(this->day_of_month, 1, days_in_month(this->month, this->year) + 1)) {
98  // day of month roll-over, increment month
99  increment_time_value(this->month, 1, 13);
100  }
101 
102  uint16_t days_in_year = (this->year % 4 == 0) ? 366 : 365;
103  if (increment_time_value(this->day_of_year, 1, days_in_year + 1)) {
104  // day of year roll-over, increment year
105  this->year++;
106  }
107 }
108 void ESPTime::recalc_timestamp_utc(bool use_day_of_year) {
109  time_t res = 0;
110 
111  if (!this->fields_in_range()) {
112  this->timestamp = -1;
113  return;
114  }
115 
116  for (int i = 1970; i < this->year; i++)
117  res += is_leap_year(i) ? 366 : 365;
118 
119  if (use_day_of_year) {
120  res += this->day_of_year - 1;
121  } else {
122  for (int i = 1; i < this->month; i++)
123  res += days_in_month(i, this->year);
124 
125  res += this->day_of_month - 1;
126  }
127 
128  res *= 24;
129  res += this->hour;
130  res *= 60;
131  res += this->minute;
132  res *= 60;
133  res += this->second;
134  this->timestamp = res;
135 }
136 
138  int32_t offset = 0;
139  time_t now = ::time(nullptr);
140  auto local = ESPTime::from_epoch_local(now);
141  auto utc = ESPTime::from_epoch_utc(now);
142  bool negative = utc.hour > local.hour && local.day_of_year <= utc.day_of_year;
143 
144  if (utc.minute > local.minute) {
145  local.minute += 60;
146  local.hour -= 1;
147  }
148  offset += (local.minute - utc.minute) * 60;
149 
150  if (negative) {
151  offset -= (utc.hour - local.hour) * 3600;
152  } else {
153  if (utc.hour > local.hour) {
154  local.hour += 24;
155  }
156  offset += (local.hour - utc.hour) * 3600;
157  }
158  return offset;
159 }
160 
161 bool ESPTime::operator<(ESPTime other) { return this->timestamp < other.timestamp; }
162 bool ESPTime::operator<=(ESPTime other) { return this->timestamp <= other.timestamp; }
163 bool ESPTime::operator==(ESPTime other) { return this->timestamp == other.timestamp; }
164 bool ESPTime::operator>=(ESPTime other) { return this->timestamp >= other.timestamp; }
165 bool ESPTime::operator>(ESPTime other) { return this->timestamp > other.timestamp; }
166 
167 template<typename T> bool increment_time_value(T &current, uint16_t begin, uint16_t end) {
168  current++;
169  if (current >= end) {
170  current = begin;
171  return true;
172  }
173  return false;
174 }
175 
176 } // namespace esphome
static ESPTime from_epoch_utc(time_t epoch)
Convert an UTC epoch timestamp to a UTC time ESPTime instance.
Definition: time.h:83
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
static int32_t timezone_offset()
Definition: time.cpp:137
A more user-friendly version of struct tm from time.h.
Definition: time.h:13
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:19
bool is_dst
daylight saving time flag
Definition: time.h:33
void increment_day()
Increment this clock instance by one day.
Definition: time.cpp:91
uint16_t day_of_year
day of the year [1-366]
Definition: time.h:27
bool operator<=(ESPTime other)
Definition: time.cpp:162
void increment_second()
Increment this clock instance by one second.
Definition: time.cpp:64
static ESPTime from_epoch_local(time_t epoch)
Convert an UTC epoch timestamp to a local time ESPTime instance.
Definition: time.h:74
bool operator>(ESPTime other)
Definition: time.cpp:165
bool increment_time_value(T &current, uint16_t begin, uint16_t end)
Definition: time.cpp:167
uint8_t second
seconds after the minute [0-60]
Definition: time.h:17
time_t timestamp
unix epoch time (seconds since UTC Midnight January 1, 1970)
Definition: time.h:35
struct tm to_c_tm()
Convert this ESPTime instance back to a tm struct.
Definition: time.cpp:33
uint8_t minute
minutes after the hour [0-59]
Definition: time.h:19
bool operator==(ESPTime other)
Definition: time.cpp:163
uint8_t day_of_week
day of the week; sunday=1 [1-7]
Definition: time.h:23
uint16_t year
year
Definition: time.h:31
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:108
uint8_t month
month; january=1 [1-12]
Definition: time.h:29
uint8_t hour
hours since midnight [0-23]
Definition: time.h:21
bool operator<(ESPTime other)
Definition: time.cpp:161
uint8_t day_of_month
day of the month [1-31]
Definition: time.h:25
bool operator>=(ESPTime other)
Definition: time.cpp:164
bool fields_in_range() const
Check if all time fields of this ESPTime are in range.
Definition: time.h:60