mirror of
https://github.com/esphome/esphome.git
synced 2026-10-22 05:46:12 +00:00
[core] Count 2100 as a common year when working out timestamps
This commit is contained in:
@@ -15,6 +15,8 @@ uint8_t days_in_month(uint8_t month, uint16_t year) {
|
||||
return DAYS_IN_MONTH[month];
|
||||
}
|
||||
|
||||
static uint16_t days_in_year(uint16_t year) { return days_in_month(2, year) == 29 ? 366 : 365; }
|
||||
|
||||
size_t ESPTime::strftime(char *buffer, size_t buffer_len, const char *format) {
|
||||
struct tm c_tm = this->to_c_tm();
|
||||
#ifdef USE_TIME_TIMEZONE
|
||||
@@ -252,8 +254,7 @@ void ESPTime::increment_second() {
|
||||
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)) {
|
||||
if (increment_time_value(this->day_of_year, 1, days_in_year(this->year) + 1)) {
|
||||
// day of year roll-over, increment year
|
||||
this->year++;
|
||||
}
|
||||
@@ -270,8 +271,7 @@ void ESPTime::increment_day() {
|
||||
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)) {
|
||||
if (increment_time_value(this->day_of_year, 1, days_in_year(this->year) + 1)) {
|
||||
// day of year roll-over, increment year
|
||||
this->year++;
|
||||
}
|
||||
@@ -285,7 +285,7 @@ void ESPTime::recalc_timestamp_utc(bool use_day_of_year) {
|
||||
}
|
||||
|
||||
for (int i = 1970; i < this->year; i++)
|
||||
res += (i % 4 == 0) ? 366 : 365;
|
||||
res += days_in_year(i);
|
||||
|
||||
if (use_day_of_year) {
|
||||
res += this->day_of_year - 1;
|
||||
|
||||
@@ -75,6 +75,8 @@ TEST(BcdCalendar, DsLayoutTwelveHourMode) {
|
||||
TEST(BcdCalendar, DsLayoutCenturyBit) {
|
||||
const ESPTime time = decode_bcd_calendar<DS_BCD_CALENDAR>(patched(DS_RAW, 5, 0x84).data());
|
||||
EXPECT_EQ(time.year, 2126);
|
||||
// 2100 is not a leap year, so the timestamp is one day earlier than a plain every-four-years count gives.
|
||||
EXPECT_EQ(time.timestamp, 4932057598);
|
||||
uint8_t out[7];
|
||||
encode_bcd_calendar<DS_BCD_CALENDAR>(time, out);
|
||||
EXPECT_EQ(out[5], 0x84);
|
||||
|
||||
Reference in New Issue
Block a user