ESPHome 2026.3.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#ifdef USE_TIME_TIMEZONE
10#include "posix_tz.h"
11#endif
12
13namespace esphome::time {
14
22 public:
23 explicit RealTimeClock();
24
25#ifdef USE_TIME_TIMEZONE
27 void set_timezone(const char *tz) { this->apply_timezone_(tz); }
28
31 void set_timezone(const char *tz, size_t len) {
32 if (tz == nullptr) {
33 this->apply_timezone_(nullptr);
34 return;
35 }
36 // Stack buffer - TZ strings from tzdata are typically short (< 50 chars)
37 char buf[128];
38 if (len >= sizeof(buf))
39 len = sizeof(buf) - 1;
40 memcpy(buf, tz, len);
41 buf[len] = '\0';
42 this->apply_timezone_(buf);
43 }
44
46 void set_timezone(const std::string &tz) { this->apply_timezone_(tz.c_str()); }
47#endif
48
51
54
56 time_t timestamp_now() { return ::time(nullptr); }
57
58 void add_on_time_sync_callback(std::function<void()> &&callback) {
59 this->time_sync_callback_.add(std::move(callback));
60 };
61
62 void dump_config() override;
63
64 protected:
66 void synchronize_epoch_(uint32_t epoch);
67
68#ifdef USE_TIME_TIMEZONE
69 void apply_timezone_(const char *tz);
70#endif
71
73};
74
75template<typename... Ts> class TimeHasTimeCondition : public Condition<Ts...> {
76 public:
78 bool check(const Ts &...x) override { return this->parent_->now().is_valid(); }
79
80 protected:
82};
83
84} // namespace esphome::time
Base class for all automation conditions.
Definition automation.h:304
This class simplifies creating components that periodically check a state.
Definition component.h:527
The RealTimeClock class exposes common timekeeping functions via the device's local real-time clock.
time_t timestamp_now()
Get the current time as the UTC epoch since January 1st 1970.
void set_timezone(const char *tz)
Set the time zone from a POSIX TZ string.
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 apply_timezone_(const char *tz)
LazyCallbackManager< void()> time_sync_callback_
void set_timezone(const std::string &tz)
Set the time zone from a std::string.
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 a character buffer with known length.
bool check(const Ts &...x) override
TimeHasTimeCondition(RealTimeClock *parent)
bool const ParsedTimezone & tz
Definition posix_tz.cpp:346
std::string size_t len
Definition helpers.h:817
A more user-friendly version of struct tm from time.h.
Definition time.h:21
bool is_valid() const
Check if this ESPTime is valid (all fields in range and year is greater than or equal to 2019)
Definition time.h:78
static ESPTime from_epoch_utc(time_t epoch)
Convert an UTC epoch timestamp to a UTC time ESPTime instance.
Definition time.h:129
uint16_t x
Definition tt21100.cpp:5