LRAT:
Hi,
I want the time to display a constant number of digits. Therefore I require a leading zero in front of the seconds.
The code I've written is as follows:
if (now.second() < 10)
{String thisSecond("0" + now.second());}
String thisSecond = String(now.second(), DEC);
Don't use String at all. Do it the right way (example assumes you are printing the full time):
char buffer [16];
uint8_t sec, min, hour;
sec = now.second();
min = now.minute();
hour = now.hour();
sprintf (buffer, "Time: %02u:%02u:%02u\n", hour, min, sec);
Serial.print (buffer);
The "%02u" means "print an unsigned integer with 2 places, fill with leading zero if necessary". So, the code above would print something like this:
[b]"Time: 07:41:03"[/b]
--or--
[b]"Time: 15:24:07"
[/b]
If you leave off the zero in the hours place format string (i.e. use "%2u"), then means "print with two places" and the result would then be this:
[b]"Time: 7:41:03"[/b]
--or--
[b]"Time: 15:24:07"[/b]
Now, "buffer" has to be declared large enough to hold your string, and it doesn't hurt to add a few more just in case. For example, the strings above take up 6 (for "Time: ") plus 8 (for XX:XX:XX) plus 1 for the newline character plus 1 for the ending null (0) character which is 6 + 8 + 1 + 1 = 16 which is why I chose "buffer[16]" and maybe "buffer[24]" would have been even safer (unless you are short on RAM).
Hope this helps.
(edit): Sorry OldSteve basically gave you the same info. Sorry for the duplicate.
I will add though that using PROGMEM strings is more complicated and only really needed if you are dealing with a LOT of string data and running out of SRAM. For small strings and/or just a few strings to print, just use standard "sprintf". For a large number of strings, use OldSteve's example of PROGMEM strings.