mirror of
https://github.com/esphome/esphome.git
synced 2024-11-27 17:27:59 +01:00
fix local time timestamp calculation (#7807)
Co-authored-by: Samuel Sieb <samuel@sieb.net>
This commit is contained in:
parent
f4766ab74f
commit
a70cee1dc1
3 changed files with 21 additions and 21 deletions
|
@ -60,9 +60,7 @@ ESPTime DateTimeEntity::state_as_esptime() const {
|
||||||
obj.hour = this->hour_;
|
obj.hour = this->hour_;
|
||||||
obj.minute = this->minute_;
|
obj.minute = this->minute_;
|
||||||
obj.second = this->second_;
|
obj.second = this->second_;
|
||||||
obj.day_of_week = 1; // Required to be valid for recalc_timestamp_local but not used.
|
obj.recalc_timestamp_local();
|
||||||
obj.day_of_year = 1; // Required to be valid for recalc_timestamp_local but not used.
|
|
||||||
obj.recalc_timestamp_local(false);
|
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,20 +5,18 @@
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
|
|
||||||
bool is_leap_year(uint32_t year) { return (year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0); }
|
|
||||||
|
|
||||||
uint8_t days_in_month(uint8_t month, uint16_t year) {
|
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};
|
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 && (year % 4 == 0))
|
||||||
if (month == 2 && is_leap_year(year))
|
|
||||||
return 29;
|
return 29;
|
||||||
return days;
|
return DAYS_IN_MONTH[month];
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t ESPTime::strftime(char *buffer, size_t buffer_len, const char *format) {
|
size_t ESPTime::strftime(char *buffer, size_t buffer_len, const char *format) {
|
||||||
struct tm c_tm = this->to_c_tm();
|
struct tm c_tm = this->to_c_tm();
|
||||||
return ::strftime(buffer, buffer_len, format, &c_tm);
|
return ::strftime(buffer, buffer_len, format, &c_tm);
|
||||||
}
|
}
|
||||||
|
|
||||||
ESPTime ESPTime::from_c_tm(struct tm *c_tm, time_t c_time) {
|
ESPTime ESPTime::from_c_tm(struct tm *c_tm, time_t c_time) {
|
||||||
ESPTime res{};
|
ESPTime res{};
|
||||||
res.second = uint8_t(c_tm->tm_sec);
|
res.second = uint8_t(c_tm->tm_sec);
|
||||||
|
@ -33,6 +31,7 @@ ESPTime ESPTime::from_c_tm(struct tm *c_tm, time_t c_time) {
|
||||||
res.timestamp = c_time;
|
res.timestamp = c_time;
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct tm ESPTime::to_c_tm() {
|
struct tm ESPTime::to_c_tm() {
|
||||||
struct tm c_tm {};
|
struct tm c_tm {};
|
||||||
c_tm.tm_sec = this->second;
|
c_tm.tm_sec = this->second;
|
||||||
|
@ -46,6 +45,7 @@ struct tm ESPTime::to_c_tm() {
|
||||||
c_tm.tm_isdst = this->is_dst;
|
c_tm.tm_isdst = this->is_dst;
|
||||||
return c_tm;
|
return c_tm;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string ESPTime::strftime(const std::string &format) {
|
std::string ESPTime::strftime(const std::string &format) {
|
||||||
std::string timestr;
|
std::string timestr;
|
||||||
timestr.resize(format.size() * 4);
|
timestr.resize(format.size() * 4);
|
||||||
|
@ -142,6 +142,7 @@ void ESPTime::increment_second() {
|
||||||
this->year++;
|
this->year++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ESPTime::increment_day() {
|
void ESPTime::increment_day() {
|
||||||
this->timestamp += 86400;
|
this->timestamp += 86400;
|
||||||
|
|
||||||
|
@ -159,23 +160,22 @@ void ESPTime::increment_day() {
|
||||||
this->year++;
|
this->year++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ESPTime::recalc_timestamp_utc(bool use_day_of_year) {
|
void ESPTime::recalc_timestamp_utc(bool use_day_of_year) {
|
||||||
time_t res = 0;
|
time_t res = 0;
|
||||||
|
|
||||||
if (!this->fields_in_range()) {
|
if (!this->fields_in_range()) {
|
||||||
this->timestamp = -1;
|
this->timestamp = -1;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 1970; i < this->year; i++)
|
for (int i = 1970; i < this->year; i++)
|
||||||
res += is_leap_year(i) ? 366 : 365;
|
res += (year % 4 == 0) ? 366 : 365;
|
||||||
|
|
||||||
if (use_day_of_year) {
|
if (use_day_of_year) {
|
||||||
res += this->day_of_year - 1;
|
res += this->day_of_year - 1;
|
||||||
} else {
|
} else {
|
||||||
for (int i = 1; i < this->month; i++)
|
for (int i = 1; i < this->month; i++)
|
||||||
res += days_in_month(i, this->year);
|
res += days_in_month(i, this->year);
|
||||||
|
|
||||||
res += this->day_of_month - 1;
|
res += this->day_of_month - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -188,13 +188,17 @@ void ESPTime::recalc_timestamp_utc(bool use_day_of_year) {
|
||||||
this->timestamp = res;
|
this->timestamp = res;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ESPTime::recalc_timestamp_local(bool use_day_of_year) {
|
void ESPTime::recalc_timestamp_local() {
|
||||||
this->recalc_timestamp_utc(use_day_of_year);
|
struct tm tm;
|
||||||
this->timestamp -= ESPTime::timezone_offset();
|
|
||||||
ESPTime temp = ESPTime::from_epoch_local(this->timestamp);
|
tm.tm_year = this->year - 1900;
|
||||||
if (temp.is_dst) {
|
tm.tm_mon = this->month - 1;
|
||||||
this->timestamp -= 3600;
|
tm.tm_mday = this->day_of_month;
|
||||||
}
|
tm.tm_hour = this->hour;
|
||||||
|
tm.tm_min = this->minute;
|
||||||
|
tm.tm_sec = this->second;
|
||||||
|
|
||||||
|
this->timestamp = mktime(&tm);
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t ESPTime::timezone_offset() {
|
int32_t ESPTime::timezone_offset() {
|
||||||
|
|
|
@ -9,8 +9,6 @@ namespace esphome {
|
||||||
|
|
||||||
template<typename T> bool increment_time_value(T ¤t, uint16_t begin, uint16_t end);
|
template<typename T> bool increment_time_value(T ¤t, uint16_t begin, uint16_t end);
|
||||||
|
|
||||||
bool is_leap_year(uint32_t year);
|
|
||||||
|
|
||||||
uint8_t days_in_month(uint8_t month, uint16_t year);
|
uint8_t days_in_month(uint8_t month, uint16_t year);
|
||||||
|
|
||||||
/// A more user-friendly version of struct tm from time.h
|
/// A more user-friendly version of struct tm from time.h
|
||||||
|
@ -100,7 +98,7 @@ struct ESPTime {
|
||||||
void recalc_timestamp_utc(bool use_day_of_year = true);
|
void recalc_timestamp_utc(bool use_day_of_year = true);
|
||||||
|
|
||||||
/// Recalculate the timestamp field from the other fields of this ESPTime instance assuming local fields.
|
/// Recalculate the timestamp field from the other fields of this ESPTime instance assuming local fields.
|
||||||
void recalc_timestamp_local(bool use_day_of_year = true);
|
void recalc_timestamp_local();
|
||||||
|
|
||||||
/// Convert this ESPTime instance back to a tm struct.
|
/// Convert this ESPTime instance back to a tm struct.
|
||||||
struct tm to_c_tm();
|
struct tm to_c_tm();
|
||||||
|
|
Loading…
Reference in a new issue