Hi -- I'm trying to print the current time like this:
char tempstring [128];
struct tm timeinfo;
...
if (getLocalTime(&timeinfo, 1)) // 1s timeout, but once time acquired from NTP should be immediate
{
strftime(tempstring,sizeof(tempstring), "%Y-%m-%d %H:%M:%S", &timeinfo);
// print tempstring to display, etc.
}
...it works, but it shows time in 24-hour format, where 3pm is "15". I want 12 hour format, but it zero-pads: %I makes "03". I've read various ways to convince strftime to not zero-pad (%-I, %#I, etc) but none of them seem to work. %r seemed promising but didn't work.
Thanks! I also tried %r but it didn't print anything... I should say that I'm programming an ESP32 (on the Arduino framework) in vscode/platformIO in case that means anything to this quandary...
strftime is easy to understand. if the code works but isn't exactly what you want, check the reference and change the format, probably changing %H to %I or %e for space padded hour
You could just roll your own (untested code follows):
if (getLocalTime(&timeinfo, 1)) // 1s timeout, but once time acquired from NTP should be immediate
{
snprintf(tempstring,sizeof(tempstring), "%04u-%02u-%02u %u:%%02u:%02u",
timeinfo.tm_year+1900,
timeinfo.tm_mon+1,
timeinfo.tm_mday,
timeinfo.tm_hour % 12 ? timeinfo.tm_hour % 12 : 12,
timeinfo.tm_min,
timeinfo.tm_sec);
// print tempstring to display, etc.
}