strptime - what am I doing wrong?

dbaicz
Posts: 1
Joined: Mon Nov 06, 2023 10:30 am

strptime - what am I doing wrong?

Postby dbaicz » 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:

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() {
}
Result is '23/10/2023 23:37:15'. I do not know what I am doing wrong. Anyone have any ideas?

Shoyur
Posts: 3
Joined: Tue Nov 07, 2023 6:25 pm

Re: strptime - what am I doing wrong?

Postby Shoyur » Tue Nov 07, 2023 6:56 pm

Hi,
maybe there is a better solution, sorry im not an expert, but i was also not able to use a tag to recognize its not PM in the tm structure.
so why not parse your string and manually put the values back to your tm values.
i was thinking maybe the string length can vary (the day or the hour could have a single character).

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);

  int mo, d, y, h, m, s;
  char value[3];
  sscanf(input, "%d/%d/%d %d:%d:%d %2s", &d, &mo, &y, &h, &m, &s, value);
  if (strcmp(value, "PM") == 0 && h != 12) {
    h += 12;
  } else if (strcmp(value, "AM") == 0 && h == 12) {
    h = 0;
  }

  ts.tm_sec = s;
  ts.tm_min = m;
  ts.tm_hour = h;
  ts.tm_mday = d;
  ts.tm_mon = mo - 1;
  ts.tm_year = y - 1900;

  // test result
  strftime(output, sizeof(output), "%d/%m/%Y %H:%M:%S", &ts);
  Serial.printf("Output is '%s'\n", output);
}

void loop() {
}

User avatar
mbratch
Posts: 303
Joined: Fri Jun 11, 2021 1:51 pm

Re: strptime - what am I doing wrong?

Postby mbratch » Wed Nov 08, 2023 7:29 pm

As far as I can tell, you aren't calling the functions wrong. That would lead me to think there could be a bug in the library perhaps.

I am not using Arduino but ESP-IDF. I will give this a try on that system and see what I get.

MicroController
Posts: 1760
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: strptime - what am I doing wrong?

Postby MicroController » Wed Nov 08, 2023 9:38 pm

"%p" depends on the current locale, which may also define %p to be empty. Maybe the (default) locale is not set to what you need.

qubdtq
Posts: 12
Joined: Thu Sep 21, 2023 3:41 am

Re: strptime - what am I doing wrong?

Postby qubdtq » Thu Apr 25, 2024 11:43 pm

In case it's helpful, I believe this was a bug and is now fixed:

https://github.com/espressif/arduino-esp32/issues/9184

Who is online

Users browsing this forum: No registered users and 73 guests