1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-29 22:24:26 +00:00

Move ESPTime into core esphome namespace (#4926)

* Prep-work for datetime entities

* Fix some includes and remove some restrictions on printing time on displays

* format

* format

* More formatting

* Move function contents

* Ignore clang-tidy
This commit is contained in:
Jesse Hills
2023-06-09 10:24:44 +12:00
committed by GitHub
parent ce13979690
commit 302dea4169
31 changed files with 376 additions and 403 deletions

View File

@@ -1,5 +1,7 @@
#include "automation.h"
#include "esphome/core/log.h"
#include <cinttypes>
namespace esphome {

View File

@@ -1,7 +1,9 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/core/automation.h"
#include "esphome/core/component.h"
#include "esphome/core/time.h"
#include "real_time_clock.h"
#include <vector>

View File

@@ -52,171 +52,5 @@ void RealTimeClock::apply_timezone_() {
tzset();
}
size_t ESPTime::strftime(char *buffer, size_t buffer_len, const char *format) {
struct tm c_tm = this->to_c_tm();
return ::strftime(buffer, buffer_len, format, &c_tm);
}
ESPTime ESPTime::from_c_tm(struct tm *c_tm, time_t c_time) {
ESPTime res{};
res.second = uint8_t(c_tm->tm_sec);
res.minute = uint8_t(c_tm->tm_min);
res.hour = uint8_t(c_tm->tm_hour);
res.day_of_week = uint8_t(c_tm->tm_wday + 1);
res.day_of_month = uint8_t(c_tm->tm_mday);
res.day_of_year = uint16_t(c_tm->tm_yday + 1);
res.month = uint8_t(c_tm->tm_mon + 1);
res.year = uint16_t(c_tm->tm_year + 1900);
res.is_dst = bool(c_tm->tm_isdst);
res.timestamp = c_time;
return res;
}
struct tm ESPTime::to_c_tm() {
struct tm c_tm {};
c_tm.tm_sec = this->second;
c_tm.tm_min = this->minute;
c_tm.tm_hour = this->hour;
c_tm.tm_mday = this->day_of_month;
c_tm.tm_mon = this->month - 1;
c_tm.tm_year = this->year - 1900;
c_tm.tm_wday = this->day_of_week - 1;
c_tm.tm_yday = this->day_of_year - 1;
c_tm.tm_isdst = this->is_dst;
return c_tm;
}
std::string ESPTime::strftime(const std::string &format) {
std::string timestr;
timestr.resize(format.size() * 4);
struct tm c_tm = this->to_c_tm();
size_t len = ::strftime(&timestr[0], timestr.size(), format.c_str(), &c_tm);
while (len == 0) {
timestr.resize(timestr.size() * 2);
len = ::strftime(&timestr[0], timestr.size(), format.c_str(), &c_tm);
}
timestr.resize(len);
return timestr;
}
template<typename T> bool increment_time_value(T &current, uint16_t begin, uint16_t end) {
current++;
if (current >= end) {
current = begin;
return true;
}
return false;
}
static bool is_leap_year(uint32_t year) { return (year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0); }
static uint8_t days_in_month(uint8_t month, uint16_t year) {
static const uint8_t DAYS_IN_MONTH[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
uint8_t days = DAYS_IN_MONTH[month];
if (month == 2 && is_leap_year(year))
return 29;
return days;
}
void ESPTime::increment_second() {
this->timestamp++;
if (!increment_time_value(this->second, 0, 60))
return;
// second roll-over, increment minute
if (!increment_time_value(this->minute, 0, 60))
return;
// minute roll-over, increment hour
if (!increment_time_value(this->hour, 0, 24))
return;
// hour roll-over, 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::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;
if (!this->fields_in_range()) {
this->timestamp = -1;
return;
}
for (int i = 1970; i < this->year; i++)
res += is_leap_year(i) ? 366 : 365;
if (use_day_of_year) {
res += this->day_of_year - 1;
} else {
for (int i = 1; i < this->month; i++)
res += days_in_month(i, this->year);
res += this->day_of_month - 1;
}
res *= 24;
res += this->hour;
res *= 60;
res += this->minute;
res *= 60;
res += this->second;
this->timestamp = res;
}
int32_t ESPTime::timezone_offset() {
int32_t offset = 0;
time_t now = ::time(nullptr);
auto local = ESPTime::from_epoch_local(now);
auto utc = ESPTime::from_epoch_utc(now);
bool negative = utc.hour > local.hour && local.day_of_year <= utc.day_of_year;
if (utc.minute > local.minute) {
local.minute += 60;
local.hour -= 1;
}
offset += (local.minute - utc.minute) * 60;
if (negative) {
offset -= (utc.hour - local.hour) * 3600;
} else {
if (utc.hour > local.hour) {
local.hour += 24;
}
offset += (local.hour - utc.hour) * 3600;
}
return offset;
}
bool ESPTime::operator<(ESPTime other) { return this->timestamp < other.timestamp; }
bool ESPTime::operator<=(ESPTime other) { return this->timestamp <= other.timestamp; }
bool ESPTime::operator==(ESPTime other) { return this->timestamp == other.timestamp; }
bool ESPTime::operator>=(ESPTime other) { return this->timestamp >= other.timestamp; }
bool ESPTime::operator>(ESPTime other) { return this->timestamp > other.timestamp; }
} // namespace time
} // namespace esphome

View File

@@ -1,106 +1,15 @@
#pragma once
#include <bitset>
#include <cstdlib>
#include "esphome/core/automation.h"
#include "esphome/core/component.h"
#include "esphome/core/helpers.h"
#include <bitset>
#include <cstdlib>
#include <ctime>
#include "esphome/core/time.h"
namespace esphome {
namespace time {
/// A more user-friendly version of struct tm from time.h
struct ESPTime {
/** seconds after the minute [0-60]
* @note second is generally 0-59; the extra range is to accommodate leap seconds.
*/
uint8_t second;
/// minutes after the hour [0-59]
uint8_t minute;
/// hours since midnight [0-23]
uint8_t hour;
/// day of the week; sunday=1 [1-7]
uint8_t day_of_week;
/// day of the month [1-31]
uint8_t day_of_month;
/// day of the year [1-366]
uint16_t day_of_year;
/// month; january=1 [1-12]
uint8_t month;
/// year
uint16_t year;
/// daylight saving time flag
bool is_dst;
/// unix epoch time (seconds since UTC Midnight January 1, 1970)
time_t timestamp;
/** Convert this ESPTime struct to a null-terminated c string buffer as specified by the format argument.
* Up to buffer_len bytes are written.
*
* @see https://www.gnu.org/software/libc/manual/html_node/Formatting-Calendar-Time.html#index-strftime
*/
size_t strftime(char *buffer, size_t buffer_len, const char *format);
/** Convert this ESPTime struct to a string as specified by the format argument.
* @see https://www.gnu.org/software/libc/manual/html_node/Formatting-Calendar-Time.html#index-strftime
*
* @warning This method uses dynamically allocated strings which can cause heap fragmentation with some
* microcontrollers.
*/
std::string strftime(const std::string &format);
/// Check if this ESPTime is valid (all fields in range and year is greater than 2018)
bool is_valid() const { return this->year >= 2019 && this->fields_in_range(); }
/// Check if all time fields of this ESPTime are in range.
bool fields_in_range() const {
return this->second < 61 && this->minute < 60 && this->hour < 24 && this->day_of_week > 0 &&
this->day_of_week < 8 && this->day_of_month > 0 && this->day_of_month < 32 && this->day_of_year > 0 &&
this->day_of_year < 367 && this->month > 0 && this->month < 13;
}
/// Convert a C tm struct instance with a C unix epoch timestamp to an ESPTime instance.
static ESPTime from_c_tm(struct tm *c_tm, time_t c_time);
/** Convert an UTC epoch timestamp to a local time ESPTime instance.
*
* @param epoch Seconds since 1st January 1970. In UTC.
* @return The generated ESPTime
*/
static ESPTime from_epoch_local(time_t epoch) {
struct tm *c_tm = ::localtime(&epoch);
return ESPTime::from_c_tm(c_tm, epoch);
}
/** Convert an UTC epoch timestamp to a UTC time ESPTime instance.
*
* @param epoch Seconds since 1st January 1970. In UTC.
* @return The generated ESPTime
*/
static ESPTime from_epoch_utc(time_t epoch) {
struct tm *c_tm = ::gmtime(&epoch);
return ESPTime::from_c_tm(c_tm, epoch);
}
/// Recalculate the timestamp field from the other fields of this ESPTime instance (must be UTC).
void recalc_timestamp_utc(bool use_day_of_year = true);
/// Convert this ESPTime instance back to a tm struct.
struct tm to_c_tm();
static int32_t timezone_offset();
/// 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);
bool operator>=(ESPTime other);
bool operator>(ESPTime other);
};
/// The RealTimeClock class exposes common timekeeping functions via the device's local real-time clock.
///
/// \note