Help to get struct values(Arduino,C++)

Asked

Viewed 129 times

-1

I am developing an application with esp32 (similar to Arduino) that gets date and time from an NTP server. Use the following function to display these values:

void printLocalTime()
{
  struct tm timeinfo;
  if(!getLocalTime(&timeinfo)){
    Serial.println("Falha ao obter hora");
    return;
  }
  Serial.println(&timeinfo, "%A, %B, %d %Y %H:%M:%S");
}

I can get (display) the values normally, but I want to store them in a variable, but I don’t know how to access them inside the struct timeinfo, someone can help me?

  • 1

    Use timeinfo(dot) and the name of the value of the struct you want to access, the data of this struct are in the link: http://www.cplusplus.com/reference/ctime/tm/ Example: timeinfo.tm_sec

  • Thank you very much!

1 answer

1


To access members of a struct just use the syntax variavel.membro. In the case of struct tm, the members are:

tm_sec   int    seconds after the minute   0-61*
tm_min   int    minutes after the hour     0-59
tm_hour  int    hours since midnight       0-23
tm_mday  int    day of the month           1-31
tm_mon   int    months since January       0-11
tm_year  int    years since 1900
tm_wday  int    days since Sunday          0-6
tm_yday  int    days since January 1       0-365
tm_isdst int    Daylight Saving Time flag   

So the call from Serial.print*() could look like this:

Serial.printf("%d/%02d/%04d %d:%02d:%02d",
    timeinfo.tm_year + 1900, timeinfo.tm_mon + 1, timeinfo.tm_mday,
    timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.