cores icon indicating copy to clipboard operation
cores copied to clipboard

fix leap year calculation bug in makeTime()

Open A-Dunstan opened this issue 9 months ago • 1 comments

makeTime() has a bug when checking if the specified year is a leap year, caused by failing to subtract 70 from the DateTimeFields.year.

This function will demonstrate:

void printDateTime() {
  uint32_t now;
  DateTimeFields dt;
  // uncomment to test current time:
//  now = rtc_get();
  // otherwise use this time to demonstrate the bug in makeTime()
  now = 1715350435; // 2024-05-10 14:13:55
  breakTime(now, dt);
  now = makeTime(dt);

  Serial.printf("%04u-%02u-%02u %02u:%02u:%02u", dt.year+1900, dt.mon+1, dt.mday, dt.hour, dt.min, dt.sec);
  Serial.printf(" -> %u -> ", now);
  breakTime(now, dt);
  Serial.printf("%04u-%02u-%02u %02u:%02u:%02u", dt.year+1900, dt.mon+1, dt.mday, dt.hour, dt.min, dt.sec);
  Serial.println();
}

Output from current code:

2024-05-10 14:13:55 -> 1715264035 -> 2024-05-09 14:13:55

Expected output (after applying PR):

2024-05-10 14:13:55 -> 1715350435 -> 2024-05-10 14:13:55

A-Dunstan avatar May 10 '24 04:05 A-Dunstan

See https://github.com/PaulStoffregen/cores/pull/637 and https://github.com/PaulStoffregen/cores/pull/638 for the same fix

skunktrading avatar May 10 '24 04:05 skunktrading