Continuing a Question asked few days ago, answered by Edgar, a new question has rised:

My goal was using a past time_t value, and convert it into a struct tm variable in order to use it, part as a Char to print a readable text and some for logic queries.

At start NTP is configured as:

void myIOT32::startNTP(const int gmtOffset_sec = 2 * 3600, const int daylightOffset_sec = 3600, const char *ntpServer = "pool.ntp.org")
  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);

and every-time I need to get epoch and or struct tm of current time I call this function

void myIOT32::getTime()
  int a = 0;
  while (a < 3)
    if (getLocalTime(&timeinfo))

BUT, when using the following function I get not GMT (+3) reference as needed :

struct tm *myIOT32::convEpoch(time_t in_time)
  struct tm *convTime = gmtime(&in_time);
  //char time_char[40];
  //sprintf(time_char, "%04d-%02d-%02d %02d:%02d:%02d", convTime->tm_year + 1900, convTime->tm_mon + 1, convTime->tm_mday,
          convTime->tm_hour, convTime->tm_min, convTime->tm_sec);
  return convTime;

Meaning I get to correct epoch time, but strcut tm is 3 hrs earlier ( and no, I do not want to hard-code +3... )

  • 1
    the Arduino and the NTP server don't know the time zone you are in. so create a constant and put it at the beginning of your sketch (yes, hardcode). why do you use the C time library? the TimeLib is much simpler to use – Juraj Jun 18 '20 at 17:07
  • @Juraj - even tough it was config at the beginning when start NTP was executed ? – Guy . D Jun 18 '20 at 18:15
  • @Juraj and the reason I do not want to hard code +3, is daylight saving... – Guy . D Jun 18 '20 at 18:36
  • 1
    in Europe it looks like we have only a few changes to/from DST before us. the date can be calculated. github.com/jandrassy/Regulator/blob/… – Juraj Jun 19 '20 at 5:01
  • @Juraj Yes... that was my fallback. By the way, you asked about the reason not using TimeLib... since in example called "simpleTime" for ESP32 used dates that way ( and not as in ESP8266), so I guessed it is a compatibility issue. – Guy . D Jun 19 '20 at 5:27

replacing gmtime(&timeinfo) with localtime(&timeinfo) in myIOT32::convEpoch passes definition of time-zone as needed.

for more information in this cpluspls site

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.