mirror of
https://github.com/esphome/esphome.git
synced 2025-09-29 00:22:21 +01:00
Sun support (#531)
* Sun * Add sun support * Lint * Updates * Fix elevation * Lint * Update mqtt_climate.cpp
This commit is contained in:
@@ -38,11 +38,11 @@ void CronTrigger::loop() {
|
||||
}
|
||||
|
||||
this->last_check_ = time;
|
||||
if (!time.in_range()) {
|
||||
if (!time.fields_in_range()) {
|
||||
ESP_LOGW(TAG, "Time is out of range!");
|
||||
ESP_LOGD(TAG, "Second=%02u Minute=%02u Hour=%02u DayOfWeek=%u DayOfMonth=%u DayOfYear=%u Month=%u time=%ld",
|
||||
time.second, time.minute, time.hour, time.day_of_week, time.day_of_month, time.day_of_year, time.month,
|
||||
time.time);
|
||||
time.timestamp);
|
||||
}
|
||||
|
||||
if (this->matches(time))
|
||||
|
@@ -35,27 +35,30 @@ size_t ESPTime::strftime(char *buffer, size_t buffer_len, const char *format) {
|
||||
return ::strftime(buffer, buffer_len, format, &c_tm);
|
||||
}
|
||||
ESPTime ESPTime::from_c_tm(struct tm *c_tm, time_t c_time) {
|
||||
return ESPTime{.second = uint8_t(c_tm->tm_sec),
|
||||
.minute = uint8_t(c_tm->tm_min),
|
||||
.hour = uint8_t(c_tm->tm_hour),
|
||||
.day_of_week = uint8_t(c_tm->tm_wday + 1),
|
||||
.day_of_month = uint8_t(c_tm->tm_mday),
|
||||
.day_of_year = uint16_t(c_tm->tm_yday + 1),
|
||||
.month = uint8_t(c_tm->tm_mon + 1),
|
||||
.year = uint16_t(c_tm->tm_year + 1900),
|
||||
.is_dst = bool(c_tm->tm_isdst),
|
||||
.time = 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 = tm{.tm_sec = this->second,
|
||||
.tm_min = this->minute,
|
||||
.tm_hour = this->hour,
|
||||
.tm_mday = this->day_of_month,
|
||||
.tm_mon = this->month - 1,
|
||||
.tm_year = this->year - 1900,
|
||||
.tm_wday = this->day_of_week - 1,
|
||||
.tm_yday = this->day_of_year - 1,
|
||||
.tm_isdst = this->is_dst};
|
||||
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) {
|
||||
@@ -70,7 +73,6 @@ std::string ESPTime::strftime(const std::string &format) {
|
||||
timestr.resize(len);
|
||||
return timestr;
|
||||
}
|
||||
bool ESPTime::is_valid() const { return this->year >= 2018; }
|
||||
|
||||
template<typename T> bool increment_time_value(T ¤t, uint16_t begin, uint16_t end) {
|
||||
current++;
|
||||
@@ -81,8 +83,18 @@ template<typename T> bool increment_time_value(T ¤t, uint16_t begin, uint1
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool is_leap_year(uint32_t year) { return (year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0); }
|
||||
|
||||
static bool 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_in_month = DAYS_IN_MONTH[month];
|
||||
if (month == 2 && is_leap_year(year))
|
||||
days_in_month = 29;
|
||||
return days_in_month;
|
||||
}
|
||||
|
||||
void ESPTime::increment_second() {
|
||||
this->time++;
|
||||
this->timestamp++;
|
||||
if (!increment_time_value(this->second, 0, 60))
|
||||
return;
|
||||
|
||||
@@ -97,12 +109,7 @@ void ESPTime::increment_second() {
|
||||
// hour roll-over, increment day
|
||||
increment_time_value(this->day_of_week, 1, 8);
|
||||
|
||||
static const uint8_t DAYS_IN_MONTH[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
|
||||
uint8_t days_in_month = DAYS_IN_MONTH[this->month];
|
||||
if (this->month == 2 && this->year % 4 == 0)
|
||||
days_in_month = 29;
|
||||
|
||||
if (increment_time_value(this->day_of_month, 1, days_in_month + 1)) {
|
||||
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);
|
||||
}
|
||||
@@ -113,16 +120,39 @@ void ESPTime::increment_second() {
|
||||
this->year++;
|
||||
}
|
||||
}
|
||||
bool ESPTime::operator<(ESPTime other) { return this->time < other.time; }
|
||||
bool ESPTime::operator<=(ESPTime other) { return this->time <= other.time; }
|
||||
bool ESPTime::operator==(ESPTime other) { return this->time == other.time; }
|
||||
bool ESPTime::operator>=(ESPTime other) { return this->time >= other.time; }
|
||||
bool ESPTime::operator>(ESPTime other) { return this->time > other.time; }
|
||||
bool ESPTime::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;
|
||||
void ESPTime::recalc_timestamp_utc(bool use_day_of_year) {
|
||||
time_t res = 0;
|
||||
|
||||
if (!this->fields_in_range()) {
|
||||
this->timestamp = -1;
|
||||
return;
|
||||
}
|
||||
|
||||
for (uint16_t 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 (uint8_t 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;
|
||||
}
|
||||
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
|
||||
|
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <bitset>
|
||||
@@ -30,8 +31,11 @@ struct ESPTime {
|
||||
uint16_t year;
|
||||
/// daylight savings time flag
|
||||
bool is_dst;
|
||||
/// unix epoch time (seconds since UTC Midnight January 1, 1970)
|
||||
time_t time;
|
||||
union {
|
||||
ESPDEPRECATED(".time is deprecated, use .timestamp instead") time_t time;
|
||||
/// 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.
|
||||
@@ -48,13 +52,20 @@ struct ESPTime {
|
||||
*/
|
||||
std::string strftime(const std::string &format);
|
||||
|
||||
bool is_valid() const;
|
||||
/// 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(); }
|
||||
|
||||
bool in_range() const;
|
||||
/// 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 epoch timestamp to an ESPTime instance of local 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
|
||||
@@ -63,7 +74,7 @@ struct ESPTime {
|
||||
struct tm *c_tm = ::localtime(&epoch);
|
||||
return ESPTime::from_c_tm(c_tm, epoch);
|
||||
}
|
||||
/** Convert an epoch timestamp to an ESPTime instance of UTC time.
|
||||
/** Convert an UTC epoch timestamp to a UTC time ESPTime instance.
|
||||
*
|
||||
* @param epoch Seconds since 1st January 1970. In UTC.
|
||||
* @return The generated ESPTime
|
||||
@@ -73,8 +84,13 @@ struct ESPTime {
|
||||
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();
|
||||
|
||||
/// Increment this clock instance by one second.
|
||||
void increment_second();
|
||||
bool operator<(ESPTime other);
|
||||
bool operator<=(ESPTime other);
|
||||
@@ -100,10 +116,10 @@ class RealTimeClock : public Component {
|
||||
std::string get_timezone() { return this->timezone_; }
|
||||
|
||||
/// Get the time in the currently defined timezone.
|
||||
ESPTime now() { return ESPTime::from_epoch_utc(this->timestamp_now()); }
|
||||
ESPTime now() { return ESPTime::from_epoch_local(this->timestamp_now()); }
|
||||
|
||||
/// Get the time without any time zone or DST corrections.
|
||||
ESPTime utcnow() { return ESPTime::from_epoch_local(this->timestamp_now()); }
|
||||
ESPTime utcnow() { return ESPTime::from_epoch_utc(this->timestamp_now()); }
|
||||
|
||||
/// Get the current time as the UTC epoch since January 1st 1970.
|
||||
time_t timestamp_now() { return ::time(nullptr); }
|
||||
|
Reference in New Issue
Block a user