DS1307RTC icon indicating copy to clipboard operation
DS1307RTC copied to clipboard

DS1307RTC::set(time_t t) t = 0 results in year 2066

Open sterretje opened this issue 8 years ago • 0 comments

Attempt to set time to 0 gives year 2066.

Analysis (copied from http://forum.arduino.cc/index.php?topic=388120.0, reply 7) This is what seems to happen 1) rtc.set takes the time (in seconds) and breaks it up (breakTime()) into the usual tm elements; this works fine and year is set to 0 2) it writes the time using rtc.write(); write use the macro tmYearToY2k which subtracts 30 from the year. This results in a negative number that is treated as an unsigned. So year becomes 226. This is next converted to bcd using dec2bcd() resulting in 358 (0x166); the value is truncated to 0x66 because the variable is a byte. This truncated value is written to the RTC chip. 3) rtc.get reads the time from the RTC chip using read(); read gets the year and uses the macro y2kYearToTm to add 30 to the year (compensating for the 30 that was subtracted) so the year is now 96. 4) rtc.get now returns a value based on the year 96; therefore printing the variable t in your code does not give 0. 5) The year function uses the macro tmYearToCalendar that adds 1970 to the year. The result is 2066.

Note 1: I did a theoretical analysis based on the reported 'issue' in the above thread Note 2: Title not quite right; it will fail for any time representing a year below 30.

sterretje avatar Mar 23 '16 04:03 sterretje