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_,
70 if (this->
year_.has_value() && (this->
year_ < 1970 || this->
year_ > 3000)) {
71 ESP_LOGE(TAG,
"Year must be between 1970 and 3000");
77 ESP_LOGE(TAG,
"Month must be between 1 and 12");
81 if (this->
day_.has_value()) {
84 if (this->
month_.has_value()) {
87 if (this->parent_->month != 0) {
88 month = this->parent_->month;
90 ESP_LOGE(TAG,
"Month must be set to validate day");
94 if (this->
year_.has_value()) {
97 if (this->parent_->year != 0) {
98 year = this->parent_->year;
100 ESP_LOGE(TAG,
"Year must be set to validate day");
105 ESP_LOGE(TAG,
"Day must be between 1 and %d for month %d",
days_in_month(month, year), month);
110 if (this->
hour_.has_value() && this->
hour_ > 23) {
111 ESP_LOGE(TAG,
"Hour must be between 0 and 23");
115 ESP_LOGE(TAG,
"Minute must be between 0 and 59");
119 ESP_LOGE(TAG,
"Second must be between 0 and 59");
126 ESP_LOGD(TAG,
"'%s' - Setting", this->parent_->get_name().c_str());
128 if (this->
year_.has_value()) {
129 ESP_LOGD(TAG,
" Year: %d", *this->
year_);
131 if (this->
month_.has_value()) {
132 ESP_LOGD(TAG,
" Month: %d", *this->
month_);
134 if (this->
day_.has_value()) {
135 ESP_LOGD(TAG,
" Day: %d", *this->
day_);
137 if (this->
hour_.has_value()) {
138 ESP_LOGD(TAG,
" Hour: %d", *this->
hour_);
140 if (this->
minute_.has_value()) {
141 ESP_LOGD(TAG,
" Minute: %d", *this->
minute_);
143 if (this->
second_.has_value()) {
144 ESP_LOGD(TAG,
" Second: %d", *this->
second_);
146 this->parent_->control(*
this);
168 ESP_LOGE(TAG,
"Could not convert the time string to an ESPTime object");
171 return this->set_datetime(
val);
176 return this->set_datetime(val);
196 static const int MAX_TIMESTAMP_DRIFT = 900;
200 if (!this->parent_->has_state()) {
203 ESPTime time = this->parent_->rtc_->now();
207 if (this->last_check_.has_value()) {
208 if (*this->last_check_ > time && this->last_check_->
timestamp - time.
timestamp > MAX_TIMESTAMP_DRIFT) {
210 ESP_LOGW(TAG,
"Time has jumped back!");
211 }
else if (*this->last_check_ >= time) {
214 }
else if (time > *this->last_check_ && time.
timestamp - this->last_check_->timestamp > MAX_TIMESTAMP_DRIFT) {
216 ESP_LOGW(TAG,
"Time has jumped ahead!");
217 this->last_check_ = time;
223 if (*this->last_check_ >= time)
226 if (this->matches_(*this->last_check_)) {
233 this->last_check_ = time;
235 ESP_LOGW(TAG,
"Time is out of range!");
236 ESP_LOGD(TAG,
"Second=%02u Minute=%02u Hour=%02u Day=%02u Month=%02u Year=%04u", time.
second, time.
minute,
240 if (this->matches_(time))
245 return time.
is_valid() && time.
year == this->parent_->year && time.
month == this->parent_->month &&
246 time.
day_of_month == this->parent_->day && time.
hour == this->parent_->hour &&
247 time.
minute == this->parent_->minute && time.
second == this->parent_->second;
254 #endif // USE_DATETIME_TIME void apply(DateTimeEntity *datetime)
friend class DateTimeCall
A more user-friendly version of struct tm from time.h.
uint16_t day_of_year
day of the year [1-366]
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) ...
uint8_t day_of_week
day of the week; sunday=1 [1-7]
ESPTime state_as_esptime() const override
void recalc_timestamp_local(bool use_day_of_year=true)
Recalculate the timestamp field from the other fields of this ESPTime instance assuming local fields...
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.