From ef5d9597887d75f6c218ef1fe750a58406c4eaaf Mon Sep 17 00:00:00 2001 From: Rebbe Pod <66928914+RebbePod@users.noreply.github.com> Date: Mon, 24 Jan 2022 15:54:46 -0500 Subject: [PATCH] Add increment_day function to ESPTime (#2955) --- esphome/components/time/real_time_clock.cpp | 17 +++++++++++++++++ esphome/components/time/real_time_clock.h | 2 ++ 2 files changed, 19 insertions(+) diff --git a/esphome/components/time/real_time_clock.cpp b/esphome/components/time/real_time_clock.cpp index 6f6739d293..0469ba2c37 100644 --- a/esphome/components/time/real_time_clock.cpp +++ b/esphome/components/time/real_time_clock.cpp @@ -131,6 +131,23 @@ void ESPTime::increment_second() { this->year++; } } +void ESPTime::increment_day() { + this->timestamp += 86400; + + // increment day + increment_time_value(this->day_of_week, 1, 8); + + if (increment_time_value(this->day_of_month, 1, days_in_month(this->month, this->year) + 1)) { + // day of month roll-over, increment month + increment_time_value(this->month, 1, 13); + } + + uint16_t days_in_year = (this->year % 4 == 0) ? 366 : 365; + if (increment_time_value(this->day_of_year, 1, days_in_year + 1)) { + // day of year roll-over, increment year + this->year++; + } +} void ESPTime::recalc_timestamp_utc(bool use_day_of_year) { time_t res = 0; diff --git a/esphome/components/time/real_time_clock.h b/esphome/components/time/real_time_clock.h index 0c6fa6f3a0..c45deb0be5 100644 --- a/esphome/components/time/real_time_clock.h +++ b/esphome/components/time/real_time_clock.h @@ -90,6 +90,8 @@ struct ESPTime { /// Increment this clock instance by one second. void increment_second(); + /// Increment this clock instance by one day. + void increment_day(); bool operator<(ESPTime other); bool operator<=(ESPTime other); bool operator==(ESPTime other);