I am trying to create a character variable that contains the time-stamp as a string. I have not succeeded.
I have tried two methods to build the string but it does not work.
Code:
#include <Time.h>
#include <Wire.h>
#include <DS1307RTC.h> // a basic DS1307 library that returns time as a time_t
char timestamp[20]; // declare a character variable that contains timestamp
char timestamp1[20];
void setup()
{
Serial.begin(9600);
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");
}
void loop()
{
//this method does not work
timestamp[0]= (char) year();
timestamp[4]='-';
timestamp[5]= (char) month();
timestamp[7]='-';
timestamp[8]= (char) day();
timestamp[10]=' ';
timestamp[11]= (char) hour();
timestamp[13]=':';
timestamp[14]= (char) minute();
timestamp[16]=':';
timestamp[17]= (char) second();
//this method also does not work.
char timestamp2[20]= { (char) year(),'-',(char) month(),'-',(char) day(),' ',(char) hour(),':',(char) minute(),':',(char) second() };
Serial.print("My timestamps: ");
Serial.print(timestamp);
Serial.print(" ");
Serial.print(timestamp2);
Serial.print(" ");
Serial.print("timestamp should look like this!!! ");
Serial.print(year());
Serial.print("-");
printDigits(month());
Serial.print("-");
printDigits(day());
Serial.print(" ");
printDigits(hour());
Serial.print(":");
printDigits(minute());
Serial.print(":");
printDigits(second());
Serial.println();
delay(1000);
}
void printDigits(int digits){
// utility function for digital clock display: prints preceding colon and leading 0
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
Serial monitor shows this:
RTC has set the system time
My timestamps: Ð Ð-- ::% timestamp should look like this!!! 2000-01-19 22:15:37
My timestamps: Ð Ð-- ::& timestamp should look like this!!! 2000-01-19 22:15:38
My timestamps: Ð Ð-- ::' timestamp should look like this!!! 2000-01-19 22:15:39
My timestamps: Ð Ð-- ::( timestamp should look like this!!! 2000-01-19 22:15:40
My timestamps: Ð Ð-- ::) timestamp should look like this!!! 2000-01-19 22:15:41
My timestamps: Ð Ð-- ::* timestamp should look like this!!! 2000-01-19 22:15:42
My timestamps: Ð Ð-- ::+ timestamp should look like this!!! 2000-01-19 22:15:43
My timestamps: Ð Ð-- ::, timestamp should look like this!!! 2000-01-19 22:15:44
My timestamps: Ð Ð-- ::- timestamp should look like this!!! 2000-01-19 22:15:45
My timestamps: Ð Ð-- ::. timestamp should look like this!!! 2000-01-19 22:15:46
My timestamps: Ð Ð-- ::/ timestamp should look like this!!! 2000-01-19 22:15:47
My timestamps: Ð Ð-- ::0 timestamp should look like this!!! 2000-01-19 22:15:48
My timestamps: Ð Ð-- ::1 timestamp should look like this!!! 2000-01-19 22:15:49
My timestamps: Ð Ð-- ::2 timestamp should look like this!!! 2000-01-19 22:15:50
My timestamps: Ð Ð-- ::3 timestamp should look like this!!! 2000-01-19 22:15:51
My timestamps: Ð Ð-- ::4 timestamp should look like this!!! 2000-01-19 22:15:52
My timestamps: Ð Ð-- ::5 timestamp should look like this!!! 2000-01-19 22:15:53
My timestamps: Ð Ð-- ::6 timestamp should look like this!!! 2000-01-19 22:15:54
My timestamps: Ð Ð-- ::7 timestamp should look like this!!! 2000-01-19 22:15:55
My timestamps: Ð Ð-- ::8 timestamp should look like this!!! 2000-01-19 22:15:56
My timestamps: Ð Ð-- ::9 timestamp should look like this!!! 2000-01-19 22:15:57
My timestamps: Ð Ð-- ::: timestamp should look like this!!! 2000-01-19 22:15:58
My timestamps: Ð Ð-- ::; timestamp should look like this!!! 2000-01-19 22:15:59
My timestamps: Ð Ð-- :: timestamp should look like this!!! 2000-01-19 22:16:00
My timestamps: Ð Ð-- :: timestamp should look like this!!! 2000-01-19 22:16:01
My timestamps: Ð Ð-- :: timestamp should look like this!!! 2000-01-19 22:16:02
My timestamps: Ð Ð-- :: timestamp should look like this!!! 2000-01-19 22:16:03
My timestamps: Ð Ð-- :: timestamp should look like this!!! 2000-01-19 22:16:04
My timestamps: Ð Ð-- :: timestamp should look like this!!! 2000-01-19 22:16:05
My timestamps: Ð Ð-- :: timestamp should look like this!!! 2000-01-19 22:16:06
My timestamps: Ð Ð-- ::a timestamp should look like this!!! 2000-01-19 22:16:07
My timestamps: Ð Ð-- :: timestamp should look like this!!! 2000-01-19 22:16:08
My timestamps: Ð Ð-- :: timestamp should look like this!!! 2000-01-19 22:16:09
My timestamps: Ð Ð-- ::
timestamp should look like this!!! 2000-01-19 22:16:11
My timestamps: Ð Ð-- :: timestamp should look like this!!! 2000-01-19 22:16:12
My timestamps: Ð Ð-- ::
timestamp should look like this!!! 2000-01-19 22:16:13
My timestamps: Ð Ð-- :: timestamp should look like this!!! 2000-01-19 22:16:14
How to do it correctly?