help using the time library on ESP32

I'm very confused as to how to get values from the time library.

The SimpleTime example is exactly NO help.

Firstly, there are many time.h files on my system;
there seem to be seven different versions; 18643; 7203; 2699; 2676; 1828; 399; and 21 bytes

The general idea is that they SEEM to create a structure holding these values

struct tm {
int8_t tm_sec; /< seconds after the minute - [ 0 to 59 ] */
int8_t tm_min; /
< minutes after the hour - [ 0 to 59 ] */
int8_t tm_hour; /< hours since midnight - [ 0 to 23 ] */
int8_t tm_mday; /
< day of the month - [ 1 to 31 ] */
int8_t tm_wday; /< days since Sunday - [ 0 to 6 ] */
int8_t tm_mon; /
< months since January - [ 0 to 11 ] */
int16_t tm_year; /< years since 1900 */
int16_t tm_yday; /
< days since January 1 - [ 0 to 365 ] */
int16_t tm_isdst; /**< Daylight Saving Time flag */
};
https://www.cplusplus.com/reference/ctime/tm/

From the SimpleTime example I can print the date and time like this

Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");

but I'm totally at a loss HOW that works. Although I have found the "format specifiers"
http://www.cplusplus.com/reference/ctime/strftime/

What I'm TRYING to do is

1: to create a string (yyyy_mm.csv eg 2021_04.csv) that I can use as a filename in SPIFFS;
I've now discovered I can do this:

 strftime (buffer, 80, "%Y_%m.csv", &timeinfo); //4 digit year, 2 digit month
  Serial.print("Filename is ");
  Serial.println(buffer);

which outputs Filename is 2021_04.csv

2: to get the values of day, hour, minute, second as integers.

Any help greatly appreciated!

1 Like

For that Serial.print, look at Print.cpp

In the SimpleTime example, there is this:

  struct tm timeinfo;
  if(!getLocalTime(&timeinfo)){
    Serial.println("Failed to obtain time");
    return;
  }
  Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");

timeinfo is an empty tm object, then its values are set by calling getLocalTime

You can then use timeinfo.tm_hour etc

Thanks @guix that all works. it would be great if someone would write a tutorial on using the time functions though as there is a LOT of conflicting info on the web. (I must have seen most of it!)

 char buffer [80];
  strftime (buffer, 80, "%Y_%m.csv", &timeinfo); //4 digit year, 2 digit month
  Serial.print("Filename is ");
  Serial.println(buffer);
  int my_seconds = timeinfo.tm_sec;  //gets seconds as an integer
  Serial.println(my_seconds);

thanks for the link - this explains a LOT (that the arduino Serial.print doesnt!)

size_t Print::print(struct tm * timeinfo, const char * format)
{
    const char * f = format;
    if(!f){
        f = "%c";
    }
    char buf[64];
    size_t written = strftime(buf, 64, f, timeinfo);
    if(written == 0){
        return written;
    }
    return print(buf);
}

?Is there an easy way to check if the latest sychronization to NTP was successful?

1 Like

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.