3 #ifdef USE_DATETIME_DATETIME 10 static const char *
const TAG =
"datetime.datetime_entity";
17 if (this->year_ < 1970 || this->
year_ > 3000) {
19 ESP_LOGE(TAG,
"Year must be between 1970 and 3000");
22 if (this->month_ < 1 || this->
month_ > 12) {
24 ESP_LOGE(TAG,
"Month must be between 1 and 12");
32 if (this->
hour_ > 23) {
34 ESP_LOGE(TAG,
"Hour must be between 0 and 23");
39 ESP_LOGE(TAG,
"Minute must be between 0 and 59");
44 ESP_LOGE(TAG,
"Second must be between 0 and 59");
48 ESP_LOGD(TAG,
"'%s': Sending datetime %04u-%02u-%02u %02d:%02d:%02d", this->
get_name().c_str(), this->
year_,
68 if (this->
year_.has_value() && (this->
year_ < 1970 || this->
year_ > 3000)) {
69 ESP_LOGE(TAG,
"Year must be between 1970 and 3000");
75 ESP_LOGE(TAG,
"Month must be between 1 and 12");
79 if (this->
day_.has_value()) {
82 if (this->
month_.has_value()) {
85 if (this->parent_->month != 0) {
86 month = this->parent_->month;
88 ESP_LOGE(TAG,
"Month must be set to validate day");
92 if (this->
year_.has_value()) {
95 if (this->parent_->year != 0) {
96 year = this->parent_->year;
98 ESP_LOGE(TAG,
"Year must be set to validate day");
103 ESP_LOGE(TAG,
"Day must be between 1 and %d for month %d",
days_in_month(month, year), month);
108 if (this->
hour_.has_value() && this->
hour_ > 23) {
109 ESP_LOGE(TAG,
"Hour must be between 0 and 23");
113 ESP_LOGE(TAG,
"Minute must be between 0 and 59");
117 ESP_LOGE(TAG,
"Second must be between 0 and 59");
124 ESP_LOGD(TAG,
"'%s' - Setting", this->parent_->get_name().c_str());
126 if (this->
year_.has_value()) {
127 ESP_LOGD(TAG,
" Year: %d", *this->
year_);
129 if (this->
month_.has_value()) {
130 ESP_LOGD(TAG,
" Month: %d", *this->
month_);
132 if (this->
day_.has_value()) {
133 ESP_LOGD(TAG,
" Day: %d", *this->
day_);
135 if (this->
hour_.has_value()) {
136 ESP_LOGD(TAG,
" Hour: %d", *this->
hour_);
138 if (this->
minute_.has_value()) {
139 ESP_LOGD(TAG,
" Minute: %d", *this->
minute_);
141 if (this->
second_.has_value()) {
142 ESP_LOGD(TAG,
" Second: %d", *this->
second_);
144 this->parent_->control(*
this);
166 ESP_LOGE(TAG,
"Could not convert the time string to an ESPTime object");
169 return this->set_datetime(
val);
174 return this->set_datetime(val);
194 static const int MAX_TIMESTAMP_DRIFT = 900;
198 if (!this->parent_->has_state()) {
201 ESPTime time = this->parent_->rtc_->now();
205 if (this->last_check_.has_value()) {
206 if (*this->last_check_ > time && this->last_check_->
timestamp - time.
timestamp > MAX_TIMESTAMP_DRIFT) {
208 ESP_LOGW(TAG,
"Time has jumped back!");
209 }
else if (*this->last_check_ >= time) {
212 }
else if (time > *this->last_check_ && time.
timestamp - this->last_check_->timestamp > MAX_TIMESTAMP_DRIFT) {
214 ESP_LOGW(TAG,
"Time has jumped ahead!");
215 this->last_check_ = time;
221 if (*this->last_check_ >= time)
224 if (this->matches_(*this->last_check_)) {
231 this->last_check_ = time;
233 ESP_LOGW(TAG,
"Time is out of range!");
234 ESP_LOGD(TAG,
"Second=%02u Minute=%02u Hour=%02u Day=%02u Month=%02u Year=%04u", time.
second, time.
minute,
238 if (this->matches_(time))
243 return time.
is_valid() && time.
year == this->parent_->year && time.
month == this->parent_->month &&
244 time.
day_of_month == this->parent_->day && time.
hour == this->parent_->hour &&
245 time.
minute == this->parent_->minute && time.
second == this->parent_->second;
252 #endif // USE_DATETIME_TIME void apply(DateTimeEntity *datetime)
friend class DateTimeCall
void recalc_timestamp_local()
Recalculate the timestamp field from the other fields of this ESPTime instance assuming local fields...
A more user-friendly version of struct tm from time.h.
CallbackManager< void()> state_callback_
void increment_second()
Increment this clock instance by one second.
uint8_t days_in_month(uint8_t month, uint16_t year)
static ESPTime from_epoch_local(time_t epoch)
Convert an UTC epoch timestamp to a local time ESPTime instance.
uint8_t second
seconds after the minute [0-60]
time_t timestamp
unix epoch time (seconds since UTC Midnight January 1, 1970)
uint8_t minute
minutes after the hour [0-59]
bool matches_(const ESPTime &time) const
bool is_valid() const
Check if this ESPTime is valid (all fields in range and year is greater than 2018) ...
ESPTime state_as_esptime() const override
Implementation of SPI Controller mode.
DateTimeCall to_call(DateTimeEntity *datetime)
uint8_t month
month; january=1 [1-12]
uint8_t hour
hours since midnight [0-23]
DateTimeCall & set_datetime(uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second)
uint8_t day_of_month
day of the month [1-31]
const StringRef & get_name() const
bool fields_in_range() const
Check if all time fields of this ESPTime are in range.
static bool strptime(const std::string &time_to_parse, ESPTime &esp_time)
Convert a string to ESPTime struct as specified by the format argument.