after second sprintf, char array contains a different value

Hello, can someone explain to me why I'm getting a different value after a second sprintf call?

#include <SPI.h>
#include <SD.h>
#include <DS3232RTC.h>

File myFile;

#define pinCS 53    // SD-Card CS-pin

char dateTimeStart[15] = "00-00-00,00:00";
char dateTimeEnd[15] = "00-00-00,00:00";

signed char round5delta[5] = {0, -1, -2, -3, -4};  // difference to the "rounded to nearest 5" value
long round5(long no) {
  return no + round5delta[no % 5];
}

void setup() {
  Serial.begin(9600);
  Serial1.begin(115200);
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  if (timeStatus() != timeSet)
    Serial.println("Unable to sync with the RTC");
  else
    Serial.println("RTC has set the system time");
  Serial.print("Initializing SD card...");
  if (!SD.begin(53)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");
  Serial1.println("Arduino_ready|"); Serial.println("Arduino_ready|");
  delay(500);
  readSDcard(0, 6);
}

void loop() {
}

void readSDcard(byte line, byte timeFrame) {
  Serial.println("Start SDRead");
  if (timeFrame > 0) {
    tmElements_t sdElements = {second(), minute(), hour(), weekday(), day(), month(), (year() - 1970) };
    time_t sdTime = makeTime(sdElements);
    sdTime = sdTime - (timeFrame * 3600UL);
    breakTime(sdTime, sdElements);
    sprintf(dateTimeStart, "%02d-%02d-%02d,%02d:%02d", (sdElements.Year + 1970), sdElements.Month, sdElements.Day, sdElements.Hour, round5(sdElements.Minute));
    Serial.println(dateTimeStart);
    sprintf(dateTimeEnd, "%02d-%02d-%02d,%02d:%02d", year(), month(), day(), hour(), round5(minute()));
  }
  Serial.println(dateTimeStart);
  Serial.println(dateTimeEnd);
}
RTC has set the system time
Initializing SD card...initialization done.
Start SDRead
2018-10-15,15:00
0
2018-10-15,21:00

The first print of dateTimeStart gives the result I'm expecting. But the second isn't.

(year() - 1970) };

Hold that thought.

The 4 digit year you are generating overflows your buffer.
The field width of 2 is a minimum width, and the year you are providing needs 4. Strip the hundreds off and you may have better luck.

arduarn:
The 4 digit year you are generating overflows your buffer.
The field width of 2 is a minimum width, and the year you are providing needs 4. Strip the hundreds off and you may have better luck.

That makes sense, I didn't think of that. I would like the year to be 4 digits , but using %04d doesn't seem to work either...

#include <SPI.h>
#include <SD.h>
#include <DS3232RTC.h>

File myFile;

#define pinCS 53    // SD-Card CS-pin

char dateTimeStart[15] = "00-00-00,00:00";
char dateTimeEnd[15] = "00-00-00,00:00";

signed char round5delta[5] = {0, -1, -2, -3, -4};  // difference to the "rounded to nearest 5" value
long round5(long no) {
  return no + round5delta[no % 5];
}

void setup() {
  Serial.begin(9600);
  Serial1.begin(115200);
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  if (timeStatus() != timeSet)
    Serial.println("Unable to sync with the RTC");
  else
    Serial.println("RTC has set the system time");
  Serial.print("Initializing SD card...");
  if (!SD.begin(53)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");
  readSDcard(0, 6);
}

void loop() {
}

void readSDcard(byte line, byte timeFrame) {
  Serial.println("Start SDRead");
  if (timeFrame > 0) {
    tmElements_t sdElements = {second(), minute(), hour(), weekday(), day(), month(), (year() - 1970) };
    time_t sdTime = makeTime(sdElements);
    sdTime = sdTime - (timeFrame * 3600UL);
    breakTime(sdTime, sdElements);
    sprintf(dateTimeStart, "%04d-%02d-%02d,%02d:%02d", (sdElements.Year + 1970), sdElements.Month, sdElements.Day, sdElements.Hour, round5(sdElements.Minute));
    Serial.println(dateTimeStart);
    sprintf(dateTimeEnd, "%04d-%02d-%02d,%02d:%02d", year(), month(), day(), hour(), round5(minute()));
  }
  Serial.println(dateTimeStart);
  Serial.println(dateTimeEnd);
}
RTC has set the system time
Initializing SD card...initialization done.
Start SDRead
2018-10-15,15:50
0
2018-10-15,21:50

Still not the result I'm hoping for.
What am I doing wrong?

The important bit is here:

char dateTimeStart[15] = "00-00-00,00:00";
char dateTimeEnd[15] = "00-00-00,00:00";

Ah, of course! Thanks!
I'm getting there, micro-step by micro-step :wink:

Just in case I decide I want to convert the 4 digit year in to a 2 digit one, is there a simple way to do that?

Coos:
Just in case I decide I want to convert the 4 digit year in to a 2 digit one, is there a simple way to do that?

yearValue % 100    // % is the modulus operator

Thanks!