ESPHome  2024.10.2
real_time_clock.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <bitset>
4 #include <cstdlib>
7 #include "esphome/core/helpers.h"
8 #include "esphome/core/time.h"
9 
10 namespace esphome {
11 namespace time {
12 
20  public:
21  explicit RealTimeClock();
22 
24  void set_timezone(const std::string &tz) { this->timezone_ = tz; }
25 
27  std::string get_timezone() { return this->timezone_; }
28 
31 
34 
36  time_t timestamp_now() { return ::time(nullptr); }
37 
38  void call_setup() override;
39 
40  void add_on_time_sync_callback(std::function<void()> callback) {
41  this->time_sync_callback_.add(std::move(callback));
42  };
43 
44  protected:
46  void synchronize_epoch_(uint32_t epoch);
47 
48  std::string timezone_{};
49  void apply_timezone_();
50 
52 };
53 
54 template<typename... Ts> class TimeHasTimeCondition : public Condition<Ts...> {
55  public:
56  TimeHasTimeCondition(RealTimeClock *parent) : parent_(parent) {}
57  bool check(Ts... x) override { return this->parent_->now().is_valid(); }
58 
59  protected:
61 };
62 
63 } // namespace time
64 } // namespace esphome
ESPTime now()
Get the time in the currently defined timezone.
TimeHasTimeCondition(RealTimeClock *parent)
static ESPTime from_epoch_utc(time_t epoch)
Convert an UTC epoch timestamp to a UTC time ESPTime instance.
Definition: time.h:94
The RealTimeClock class exposes common timekeeping functions via the device&#39;s local real-time clock...
uint16_t x
Definition: tt21100.cpp:17
A more user-friendly version of struct tm from time.h.
Definition: time.h:17
std::string get_timezone()
Get the time zone currently in use.
void add_on_time_sync_callback(std::function< void()> callback)
This class simplifies creating components that periodically check a state.
Definition: component.h:283
void set_timezone(const std::string &tz)
Set the time zone.
CallbackManager< void()> time_sync_callback_
static ESPTime from_epoch_local(time_t epoch)
Convert an UTC epoch timestamp to a local time ESPTime instance.
Definition: time.h:85
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.
ESPTime utcnow()
Get the time without any time zone or DST corrections.
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void synchronize_epoch_(uint32_t epoch)
Report a unix epoch as current time.