[solved]Help to use strcpy to copy date and time values please

Hi,

I am not a professional, so please help me. I would like to copy date and time values to already defined variable ( char sdate[11] and char stime[9] ). I have tried like

strcpy(stime, hour()),printDigits(minute()),printDigits(second());
strcpy(sdate, year(),"-",month(),"-";day());

it sais invalid conversion from int to const char. I would like to do this because I want to try reading out date and time from NTP server instead of the RTC just for testing. With RTS I used

strcpy(stime, rtc.formatTime());
strcpy(sdate, rtc.formatDate(RTCC_DATE_ASIA));

Copy to Sd using these variables ( sdate and stime ). And of course Time.h library is added.

Thank you!

oh sorry, and printdigits came from here:

void printDigits(int digits){
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);

because of displaying the clock to the serial monitor

void clockDisplay(){
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year());
  Serial.println();

format os sdate should be 2014-03-06, and stime format should be 14:15:22

strcpy copies strings but what you are copying is not a string, hence the problem. Look at using sprintf() to copy ints to a string.

  sprintf(sdate, "%d-%d-%d", year(), month(), day());
  Serial.println(sdate);

Dear UKHeliBob,

thanks for the quick response! Can I find somewhere here a description about sprintf() , especially I would like to know the meaning of %d , %s , %i, %t, and so on..?

Can I also use for the time this one:

sprintf(stime, "%d:%d:%d", hour(), minute(), second());

UKHeliBob:
strcpy copies strings but what you are copying is not a string, hence the problem. Look at using sprintf() to copy ints to a string.

  sprintf(sdate, "%d-%d-%d", year(), month(), day());

Serial.println(sdate);

sprintf() and associated functions are not Arduino specific, they are C functions. Google is your friend.

Yes, you can use the format that you suggest for time.

Ok, great! Thank you for the help!

Hi,

using here sprintf() is working, but the format is not the best.

sprintf(stime, "%d:%d:%d", hour(), minute(), second());
...
sprintf(sdate, "%d-%d-%d", year(), month(), day());

sdate and stime are char (char sdate[11] and char stime[9] ).
Now the values are:
date is 2014-3-11 (should be 2014-03-11) and time is 10:9:3 (should be 10:09:03).

Try

sprintf(sdate, "%d-%02d-%02d", year(), month(), day());

I will give a try and back to you soon!

Can I find somewhere here a description about sprintf()

Is google broken in your country ?

It is safer to use snprintf rather than sprintf.

UKHeliBob and PeterH,

Format is now ok and I am just looking for the snprintf() specifications! Before this topic I haven't found the %02d value, but now already checked again for the leading 0 issues.
Many thanks for the help!