RonRonDK
RonRonDK

Reputation: 435

Arduino with RTC getting the wrong year

I am working on this Arduino test application as a part of a bigger project. I have connected my Arduino to a Grove RTC (Realtime clock DS1307) module which is now spitting out date and time in my serial monitor. However the year is wrong. As seen on the below picture it is showing the value 46 in the Year field.

Serial monitor

Below is the two methods i am using to get date and time and then print it out. I get the year value from the Year field of the tmElements struct. The tmElements type is residing in the Time library.

// Gets date and time and prints out in "DD/MM/YYYY - HH:MM:SS" format.
void getTime(){
  tmElements_t tm;
  if (RTC.read(tm)){
    getFormattedValue(tm.Day);
    Serial.print("/");
    getFormattedValue(tm.Month);
    Serial.print("/");
    Serial.print(tm.Year);
    Serial.print(" - ");
    getFormattedValue(tm.Hour);
    Serial.print(":");
    getFormattedValue(tm.Minute);
    Serial.print(":");
    getFormattedValue(tm.Second);
    Serial.println();
  }
}

// Formats the time value to two digits. Example: if hour is 7 it will be formatted as 07.
void getFormattedValue(int number) {
  if (number >= 0 && number < 10) {
    Serial.write('0');
  }
  Serial.print(number);
}

How come I am getting this wrong value? Can somebody please guide me in the right direction?

Upvotes: 0

Views: 2213

Answers (1)

RonRonDK
RonRonDK

Reputation: 435

Ahh.. Already found the problem myself. The problem was that I didn't use the tmYearToCalendar() method.

So instead of:

Serial.print(tm.Year);

It had to be:

Serial.print(tmYearToCalendar(tm.Year));

Works like a charm now. Just thought I would share it with you all.

Upvotes: 1

Related Questions