1

I have a web service that returns timestamps in ISO 8601 format, e.g., "2021年06月25日T12:00:00" I'm trying to turn the timestamp string into a tm type struct from the ESP8266 library <time.h>, to perform time calculations using the difftime() function.

I've been referring to the Redhat time functions https://sourceware.org/newlib/libc.html#time, as it seem to be compatible with the ESP library, though all i can find are functions converting tm structs to timestamp strings. Are there any functions to convert a string timestamp into a tm struct and then back into epoch? Or what would be the best way to approach this kind of conversion on the ESP8266 platform?

asked Apr 29, 2021 at 4:01
3
  • Does this answer your question? How to parse 20180810T143000Z to time_t Commented Apr 29, 2021 at 5:26
  • Thank you! This method works too, though i find the answer below to be more useful. Commented Apr 29, 2021 at 6:53
  • sorry yes, there is a different format. strptime can't parse a timestamp without delimiters. Commented Apr 29, 2021 at 7:08

1 Answer 1

3

Try strptime from the time.h.

void setup()
{
 
 struct tm tm = {0};
 char buf[100];
 
 // Convert to tm struct
 strptime("2001年11月12日 18:31:01", "%Y-%m-%d %H:%M:%S", &tm);
 
 // Can convert to any other format
 strftime(buf, sizeof(buf), "%d %b %Y %H:%M", &tm);
 Serial.printf("%s", buf);
}
void loop()
{
 yield();
}
answered Apr 29, 2021 at 4:49
1
  • Thank you. The method above works. I just formatted my string as "%Y-%m-%dT%H:%M:%S",instead. Commented Apr 29, 2021 at 6:51

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.