ESPHome 2026.1.0-dev
Loading...
Searching...
No Matches
real_time_clock.h
Go to the documentation of this file.
1#pragma once
2
3#include <bitset>
4#include <cstdlib>
8#include "esphome/core/time.h"
9
10namespace esphome::time {
11
19 public:
20 explicit RealTimeClock();
21
22#ifdef USE_TIME_TIMEZONE
24 void set_timezone(const std::string &tz) {
25 this->timezone_ = tz;
26 this->apply_timezone_();
27 }
28
30 void set_timezone(const char *tz, size_t len) {
31 if (this->timezone_.length() != len || memcmp(this->timezone_.c_str(), tz, len) != 0) {
32 this->timezone_.assign(tz, len);
33 this->apply_timezone_();
34 }
35 }
36
38 std::string get_timezone() { return this->timezone_; }
39#endif
40
43
46
48 time_t timestamp_now() { return ::time(nullptr); }
49
50 void add_on_time_sync_callback(std::function<void()> &&callback) {
51 this->time_sync_callback_.add(std::move(callback));
52 };
53
54 void dump_config() override;
55
56 protected:
58 void synchronize_epoch_(uint32_t epoch);
59
60#ifdef USE_TIME_TIMEZONE
61 std::string timezone_{};
62 void apply_timezone_();
63#endif
64
66};
67
68template<typename... Ts> class TimeHasTimeCondition : public Condition<Ts...> {
69 public:
71 bool check(const Ts &...x) override { return this->parent_->now().is_valid(); }
72
73 protected:
75};
76
77} // namespace esphome::time
Base class for all automation conditions.
Definition automation.h:183
This class simplifies creating components that periodically check a state.
Definition component.h:474
The RealTimeClock class exposes common timekeeping functions via the device's local real-time clock.
CallbackManager< void()> time_sync_callback_
time_t timestamp_now()
Get the current time as the UTC epoch since January 1st 1970.
void add_on_time_sync_callback(std::function< void()> &&callback)
ESPTime now()
Get the time in the currently defined timezone.
ESPTime utcnow()
Get the time without any time zone or DST corrections.
void set_timezone(const std::string &tz)
Set the time zone.
std::string get_timezone()
Get the time zone currently in use.
void synchronize_epoch_(uint32_t epoch)
Report a unix epoch as current time.
void set_timezone(const char *tz, size_t len)
Set the time zone from raw buffer, only if it differs from the current one.
bool check(const Ts &...x) override
TimeHasTimeCondition(RealTimeClock *parent)
std::string size_t len
Definition helpers.h:533
A more user-friendly version of struct tm from time.h.
Definition time.h:15
static ESPTime from_epoch_local(time_t epoch)
Convert an UTC epoch timestamp to a local time ESPTime instance.
Definition time.h:85
bool is_valid() const
Check if this ESPTime is valid (all fields in range and year is greater than 2018)
Definition time.h:61
static ESPTime from_epoch_utc(time_t epoch)
Convert an UTC epoch timestamp to a UTC time ESPTime instance.
Definition time.h:97
uint16_t x
Definition tt21100.cpp:5