I am using the ESP32 with Time.h library to log data on the SD card.
I am successfully logging data with date and time.
but help me in adding a leading "0" if the month, date, hour, min or sec is a single digit number.
Below is the part of the code I am using to log the data to the SD card.
I struck the same issue putting the time from a RTC on an OLED on my motorbike.
I read a number of solutions - most just seemed overly complicated. The one I settled for in the end was simple and straight-forward.
Here's the entire routine - hope it helps.
void updateOLED() // Write current time to OLED display
{
DateTime now = rtc.now(); // Get the current time from the RTC
display.clearDisplay(); // Clear the display so we don't get artifacts
display.setCursor(0,0); // Start at top-left corner
if (now.hour() < 10) // Leading "0" required before hour?
{
display.println("0"); // If so then write it to the OLED buffer and ...
display.setCursor(32,0); // Move the cursor for the next digit
}
display.println(now.hour(), DEC); // Write the current hour to the OLED buffer
display.setCursor(66,0); // Setup for correct minutes position
if (now.minute() < 10) // Leading "0" required before minute?
{
display.println("0"); // If so then write it to the OLED buffer
display.setCursor(98,0); // and move the cursor for the next digit
}
display.println(now.minute(), DEC); // Write the current minute to the buffer
display.display(); // Display the buffer contents on the OLED
}
No - mine is just running on a Pro mini. Reason I posted is that most of the solutions I Googled seemed to focus on trying to beat the output into shape using libraries with significant footprints and overheads whereas it seemed to be easily handled just by simply testing that data.
Thought that would port over to any program pretty easily. Sounds like your solution is even easier though.
Currently what my code does at this stage is, it pulls the time from server and then logging the date & time every second. Later it will log lot of other parameters along, every second on a new file everyday. The file name will by the date of the file.
Also the date and time which I am logging, I want the month, date. hour, mins and seconds with a leading 0.
Ohh.. correct..
Its just that I will not have to write multiple lines.
Also I came to know about the PSTR that it saves the strings to the flash memory..
how does that help and what if I do not use the PSTR in the sprintf..?