Time library/DS1307 - RTC.set(0) bug

Hey guys,

I can't set the RTC time to 0. I checked the lib source code and the time setting is pretty straightforward, so I'm not sure whats happening.

#include "Time.h"
#include "DS1307RTC.h"
#include "Wire.h"

time_t zero = 0;

void setup(){
  
Serial.begin(9600);
  
  Serial.println("Start");

  setTime(zero);
  RTC.set(zero);
}

void loop(){

  time_t t;
  t = RTC.get();

  Serial.println(t);
  Serial.print(year(t));
  Serial.print("/");
  Serial.print(month(t));
  Serial.print("/");
  Serial.print(day(t));
  Serial.print(" ");
  Serial.print(hour(t));
  Serial.print(":");
  Serial.print(minute(t));
  Serial.print(":");
  Serial.println(second(t));


  delay(1000);
}

The serial output every time I reset is always the same:

3029529600
2066/1/1 0:0:0
3029529601
2066/1/1 0:0:1
3029529602
2066/1/1 0:0:2

and so on.

Current time is set without issues.

Any input on this is greatly appreciated.

Thanks in advance!

I don't understand the idea of setting real time to 0.

Just curious. I won't be using it like that.

Your code is incomplete; for all I know zero can be 12345.

And what is the library that you use?

Oops, edited.

Latest Time library from PJRC.

I also found this code that makes reference to time formats and the odd number 0 is set to:

Link to the RTC library please.

I assume that the time library is github -> PaulStoffregen/Time

Ah yes, sorry, here are the libraries:

Time: github->PaulStoffregen/Time

DS1307: github->PaulStoffregen/DS1307RTC

Thanks for the links. I did a little digging purely based on source code of the libraries.

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.

Seems like you have found a bug in the RTC library.