ESPHome 2025.9.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 {
11namespace time {
12
20 public:
21 explicit RealTimeClock();
22
23#ifdef USE_TIME_TIMEZONE
25 void set_timezone(const std::string &tz) {
26 this->timezone_ = tz;
27 this->apply_timezone_();
28 }
29
31 std::string get_timezone() { return this->timezone_; }
32#endif
33
36
39
41 time_t timestamp_now() { return ::time(nullptr); }
42
43 void add_on_time_sync_callback(std::function<void()> &&callback) {
44 this->time_sync_callback_.add(std::move(callback));
45 };
46
47 protected:
49 void synchronize_epoch_(uint32_t epoch);
50
51#ifdef USE_TIME_TIMEZONE
52 std::string timezone_{};
53 void apply_timezone_();
54#endif
55
57};
58
59template<typename... Ts> class TimeHasTimeCondition : public Condition<Ts...> {
60 public:
62 bool check(Ts... x) override { return this->parent_->now().is_valid(); }
63
64 protected:
66};
67
68} // namespace time
69} // namespace esphome
Base class for all automation conditions.
Definition automation.h:124
This class simplifies creating components that periodically check a state.
Definition component.h:425
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.
TimeHasTimeCondition(RealTimeClock *parent)
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
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:83
bool is_valid() const
Check if this ESPTime is valid (all fields in range and year is greater than 2018)
Definition time.h:59
static ESPTime from_epoch_utc(time_t epoch)
Convert an UTC epoch timestamp to a UTC time ESPTime instance.
Definition time.h:92
uint16_t x
Definition tt21100.cpp:5