Setting date and time format (yyyy/mm/dd hh:mm:ss) using RTC

Hi, all

I am newbie to arduino. I need your help to solve my problem. Well, I want to display date and time on serial monitor by using RTC and arduino due. But the problem is the format of date and time. I have tried to display date and time using this code. But this code give me an output like yyyy/m/d h:m:s. I need to edit the format like yyyy/mm/dd hh:mm:ss.

#include <Wire.h>
#include <RTClib.h>
RTC_DS1307 rtc;
void setup () {
Serial.begin(9600);
#ifdef AVR
Wire.begin();
#else
Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif
rtc.begin();
if (! rtc.isrunning()) {
  Serial.println("RTC is NOT running!");
  // following line sets the RTC to the date & time this sketch was compiled
  rtc.adjust(DateTime(__DATE__, __TIME__));
}
}
void loop () {
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print("/");
Serial.print("0");
Serial.print(now.month(), DEC);
Serial.print("/");
Serial.print("0");
Serial.print(now.day(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(":");
Serial.print(now.minute(), DEC);
Serial.print(":");
Serial.print(now.second(), DEC);
Serial.println();
delay(1000);
}

I need your help and suggestions. Thanks in advance.

Regards,
Intan

first: char time_buf[16]; // size big enough

then print to this buffer like:

sprintf(time_buf,"%02u:%02u:%02u ", now.hour(),now.minute(),now.second()); Serial.print(time_buf);

Sometimes, if you don’t use many of the sprintf() features, you can save some memory replacing it with a simpler function. For example, the example using sprintf() and just hard coding in some values looked like:

void setup() {
  // put your setup code here, to run once:
  char str[16];  

  Serial.begin(115200);

  sprintf(str,"%02u:%02u:%02u ", 11,55,5);  
  Serial.println(str);
}
void loop() {}

and required 3616 bytes. Another alternative:

void setup() {
  // put your setup code here, to run once:
  char str[16];  

  Serial.begin(115200);

  MakeTime(str, 11,55,5);
  Serial.println(str);
}

void loop() {}

void MakeTime(char *buff, int hour, int min, int sec)
{
  char temp[3];
  
  buff[0] = '\0';
  itoa(hour, temp, 10);
  if (hour < 10) {
    strcpy(buff, "0");
  } 
  strcat(buff, temp);
  strcat(buff, ":");
  
 if (min < 10) {
    strcat(buff, "0");
  } 
  itoa(min, temp, 10);
  strcat(buff, temp);
  strcat(buff, ":");
  
 if (sec < 10) {
    strcat(buff, "0");
  } 
  itoa(sec, temp, 10);
  strcat(buff, temp);
}

while it looks longer, only requires 2518 bytes, a savings of almost 1100 bytes. True, you’d have to write another function, MakeDate(), to supply the rest of your needs, but my guess is that you still end up saving some memory. If memory’s not an issue, sprintf() is pretty easy to use.

Hi, all

I have updated my script, like this:

#include <Wire.h>
#include <RTClib.h>
RTC_DS1307 rtc;
void setup () {
Serial.begin(9600);
#ifdef AVR
Wire.begin();
#else
Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif
rtc.begin();
if (! rtc.isrunning()) {
  Serial.println("RTC is NOT running!");
  // following line sets the RTC to the date & time this sketch was compiled
  rtc.adjust(DateTime(__DATE__, __TIME__));
}
}
void loop () {
DateTime now = rtc.now();

char dt[16];
char tm[16];

sprintf(dt, "%02d/%02d/%02d", now.year(),now.month(),now.day());
sprintf(tm, "%02d:%02d:%02d", now.hour(),now.minute(),now.second());

Serial.print(dt);
Serial.print(" ");
Serial.print(tm);
Serial.println();

delay(1000);
}

But i still have problems. The time in those script is set in am-pm, but i need to set it to 24 hours. and the time that i got is 7minutes slower than GMT+7. For example, it is 13:32:55 now, but the script print it as 01:25:55.
Do you have any suggestions? Thanks in advance.

Regards,
Intan

Set your RTC to correct time

knut_ny: Set your RTC to correct time

Hi, knut_ny

Would you mind to tell me how to set it?

Thanks in advance

Regards, Intan

Adafruit rtclib ??

  • // This line sets the RTC with an explicit date & time, for example to set
  • // January 21, 2014 at 3am you would call:
  • // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));

If you're trying to affect the AM/PM flag, you probably should look at:

http://datasheets.maximintegrated.com/en/ds/DS1307.pdf

Table 2, page 8, shows the Timekeeper register that controls most of what the clock does. Bit 5 of register 0x02 controls the AM/PM flag. Combine that with some previous posts on this Forum and you should be able to figure everything out.