strptime - what am I doing wrong?
Posted: Mon Nov 06, 2023 11:09 am
Hi,
On ESP32 I need to convert a date and time in the form "10/23/2023 11:37:15 AM" to a 24 hour format in the form "23/10/2023 11:37:15". I use strptime function, but it completely ignores the AM/PM flag and returns the time as if the source time contained PM.
Example:
Result is '23/10/2023 23:37:15'. I do not know what I am doing wrong. Anyone have any ideas?
On ESP32 I need to convert a date and time in the form "10/23/2023 11:37:15 AM" to a 24 hour format in the form "23/10/2023 11:37:15". I use strptime function, but it completely ignores the AM/PM flag and returns the time as if the source time contained PM.
Example:
Code: Select all
#include <time.h>
struct tm ts;
char output[100];
void setup() {
Serial.begin(115200);
delay(1000);
char input[] = "10/23/2023 11:37:15 AM";
Serial.printf("Input is '%s'\n", input);
strptime(input, "%m/%d/%Y %I:%M:%S %p", &ts);
//strptime(input, "%m/%d/%Y %r", &ts); // the same problem
// test result
strftime(output, sizeof(output), "%d/%m/%Y %H:%M:%S", &ts);
Serial.printf("Output is '%s'\n", output);
}
void loop() {
}