Skip to content

strptime doesn't parse AM/PM (%p) #9184

Closed
@chconnor

Description

@chconnor

Board

ESP32

Device Description

ESP32-DevKitC-VIE

Hardware Configuration

many things

Version

v2.0.11

IDE Name

vscode + Platform IO

Operating System

Linux

Flash frequency

default?

PSRAM enabled

yes

Upload speed

default?

Description

Hopefully this is the proper place to post this -- if not please let me know:

strptime() does not seem to parse AM/PM correctly (using the %p token). You will see in the demo code that it fails to detect AM and always makes the time PM. On my desktop, that demo code will print "hour is 8" but on the ESP32 it will print "hour is 20".

My understanding is that the parsing of %p depends on the locale, which may not use AM/PM, but I don't see a way to configure things in a way that makes %p work.

Edit to add: %p does change how the parsing works, however. Parsing "8:00AM" or "8:00PM" comes back as 20:00 with %p present, or always 08:00 without %p.

Sketch

#include <Arduino.h>
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <time.h>

void setup()
{
  Serial.begin(115200);
  while (!Serial) delay(10);

  char timestr[7] = "8:00AM";
  static struct tm temptm;
  static time_t temptime;
  temptime = time(NULL);
  temptm = *localtime(&temptime);
  strptime(timestr, "%I:%M%p", &temptm);
  Serial.printf("hour is %d\n", temptm.tm_hour);
}

void loop()
{
    delay(100);
}

Debug Message

n/a

Other Steps to Reproduce

See also this user report: https://esp32.com/viewtopic.php?t=36652

I have checked existing issues, online documentation and the Troubleshooting Guide

  • I confirm I have checked existing issues, online documentation and Troubleshooting guide.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Done

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions