Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sterretje opened this issue Mar 23, 2016 · 0 comments
Open

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

sterretje opened this issue Mar 23, 2016 · 0 comments

Comments

@sterretje
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant