ESPHome  2022.11.4
real_time_clock.h
Go to the documentation of this file.
1 #pragma once
2 
5 #include "esphome/core/helpers.h"
6 #include <bitset>
7 #include <cstdlib>
8 #include <ctime>
9 
10 namespace esphome {
11 namespace time {
12 
14 struct ESPTime {
18  uint8_t second;
20  uint8_t minute;
22  uint8_t hour;
24  uint8_t day_of_week;
26  uint8_t day_of_month;
28  uint16_t day_of_year;
30  uint8_t month;
32  uint16_t year;
34  bool is_dst;
36  time_t timestamp;
37 
43  size_t strftime(char *buffer, size_t buffer_len, const char *format);
44 
51  std::string strftime(const std::string &format);
52 
54  bool is_valid() const { return this->year >= 2019 && this->fields_in_range(); }
55 
57  bool fields_in_range() const {
58  return this->second < 61 && this->minute < 60 && this->hour < 24 && this->day_of_week > 0 &&
59  this->day_of_week < 8 && this->day_of_month > 0 && this->day_of_month < 32 && this->day_of_year > 0 &&
60  this->day_of_year < 367 && this->month > 0 && this->month < 13;
61  }
62 
64  static ESPTime from_c_tm(struct tm *c_tm, time_t c_time);
65 
71  static ESPTime from_epoch_local(time_t epoch) {
72  struct tm *c_tm = ::localtime(&epoch);
73  return ESPTime::from_c_tm(c_tm, epoch);
74  }
80  static ESPTime from_epoch_utc(time_t epoch) {
81  struct tm *c_tm = ::gmtime(&epoch);
82  return ESPTime::from_c_tm(c_tm, epoch);
83  }
84 
86  void recalc_timestamp_utc(bool use_day_of_year = true);
87 
89  struct tm to_c_tm();
90 
91  static int32_t timezone_offset();
92 
94  void increment_second();
96  void increment_day();
97  bool operator<(ESPTime other);
98  bool operator<=(ESPTime other);
99  bool operator==(ESPTime other);
100  bool operator>=(ESPTime other);
101  bool operator>(ESPTime other);
102 };
103 
111  public:
112  explicit RealTimeClock();
113 
115  void set_timezone(const std::string &tz) { this->timezone_ = tz; }
116 
118  std::string get_timezone() { return this->timezone_; }
119 
121  ESPTime now() { return ESPTime::from_epoch_local(this->timestamp_now()); }
122 
124  ESPTime utcnow() { return ESPTime::from_epoch_utc(this->timestamp_now()); }
125 
127  time_t timestamp_now() { return ::time(nullptr); }
128 
129  void call_setup() override;
130 
131  void add_on_time_sync_callback(std::function<void()> callback) {
132  this->time_sync_callback_.add(std::move(callback));
133  };
134 
135  protected:
137  void synchronize_epoch_(uint32_t epoch);
138 
139  std::string timezone_{};
140  void apply_timezone_();
141 
143 };
144 
145 template<typename... Ts> class TimeHasTimeCondition : public Condition<Ts...> {
146  public:
147  TimeHasTimeCondition(RealTimeClock *parent) : parent_(parent) {}
148  bool check(Ts... x) override { return this->parent_->now().is_valid(); }
149 
150  protected:
152 };
153 
154 } // namespace time
155 } // namespace esphome
uint8_t month
month; january=1 [1-12]
ESPTime now()
Get the time in the currently defined timezone.
TimeHasTimeCondition(RealTimeClock *parent)
bool is_valid() const
Check if this ESPTime is valid (all fields in range and year is greater than 2018) ...
The RealTimeClock class exposes common timekeeping functions via the device&#39;s local real-time clock...
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.
std::string get_timezone()
Get the time zone currently in use.
void add_on_time_sync_callback(std::function< void()> callback)
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...
This class simplifies creating components that periodically check a state.
Definition: component.h:267
void increment_second()
Increment this clock instance by one second.
void set_timezone(const std::string &tz)
Set the time zone.
static ESPTime from_epoch_local(time_t epoch)
Convert an UTC epoch timestamp to a local time ESPTime instance.
CallbackManager< void()> time_sync_callback_
bool is_dst
daylight saving time flag
uint8_t minute
minutes after the hour [0-59]
void increment_day()
Increment this clock instance by one day.
uint16_t day_of_year
day of the year [1-366]
A more user-friendly version of struct tm from time.h.
Base class for all automation conditions.
Definition: automation.h:74
time_t timestamp_now()
Get the current time as the UTC epoch since January 1st 1970.
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 fields_in_range() const
Check if all time fields of this ESPTime are in range.
bool operator>(ESPTime other)
bool operator<(ESPTime other)
ESPTime utcnow()
Get the time without any time zone or DST corrections.
uint8_t day_of_week
day of the week; sunday=1 [1-7]
Definition: a4988.cpp:4
uint8_t day_of_month
day of the month [1-31]
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()
uint8_t hour
hours since midnight [0-23]
bool operator>=(ESPTime other)