ESPHome  2021.10.2
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 
92  void increment_second();
93  bool operator<(ESPTime other);
94  bool operator<=(ESPTime other);
95  bool operator==(ESPTime other);
96  bool operator>=(ESPTime other);
97  bool operator>(ESPTime other);
98 };
99 
107  public:
108  explicit RealTimeClock();
109 
111  void set_timezone(const std::string &tz) { this->timezone_ = tz; }
112 
114  std::string get_timezone() { return this->timezone_; }
115 
117  ESPTime now() { return ESPTime::from_epoch_local(this->timestamp_now()); }
118 
120  ESPTime utcnow() { return ESPTime::from_epoch_utc(this->timestamp_now()); }
121 
123  time_t timestamp_now() { return ::time(nullptr); }
124 
125  void call_setup() override;
126 
127  void add_on_time_sync_callback(std::function<void()> callback) {
128  this->time_sync_callback_.add(std::move(callback));
129  };
130 
131  protected:
133  void synchronize_epoch_(uint32_t epoch);
134 
135  std::string timezone_{};
136 
138 };
139 
140 template<typename... Ts> class TimeHasTimeCondition : public Condition<Ts...> {
141  public:
142  TimeHasTimeCondition(RealTimeClock *parent) : parent_(parent) {}
143  bool check(Ts... x) override { return this->parent_->now().is_valid(); }
144 
145  protected:
147 };
148 
149 } // namespace time
150 } // 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:234
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]
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:33
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]
Library based on https://github.com/miguelbalboa/rfid and adapted to ESPHome by . ...
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)...
uint8_t hour
hours since midnight [0-23]
bool operator>=(ESPTime other)