-->
Page 1 of 1

different behavior using setTime(time_t) and setTime(hour, .

PostPosted: Mon Mar 27, 2023 12:54 pm
by Gigi_56
Hello, I would like to tell you about a problem I'm having in initializing the system date to the current date, including timezone, using NTP DST included in the ESP8266 core (I'm using version 3.1.0).
After setTime (date in seconds) if I print the value of now() and hour() they are different: now() is in line with the current time, hour() is one hour behind.
After setTime (with hour , minutes, seconds, day, month, year) the value of now() is one hour ahead of the current time while the value of hour() is in line with the current time.
How can this mystery be explained. Where is it that I'm wrong and/or I don't understand how now() and hour() work?
Thanks for your help
Code: Select all#include <ESP8266WiFi.h>
#include <TimeLib.h>
#include <time.h>

// Timezone definition
#define MYTZ "CET-1CEST,M3.5.0,M10.5.0/3"

/*--------------------------------------------------*/
static tm getDateTimeByParams(long time) {
  struct tm *newtime;
  const time_t tim = time;
  newtime = localtime(&tim);
  return *newtime;
}
/**
   Input tm time format and return String with format pattern
   by Renzo Mischianti <www.mischianti.org>
*/
static String getDateTimeStringByParams(tm *newtime, char* pattern = (char *)"%d.%m.%Y %H:%M:%S") {
  char buffer[30];
  strftime(buffer, 30, pattern, newtime);
  return buffer;
}
/**
   Input time in epoch format format and return String with format pattern
   by Renzo Mischianti <www.mischianti.org>
*/
static String getTimeGMY_HMS(long time, char* pattern = (char *)"%d.%m.%Y-%H:%M:%S") {
  //    struct tm *newtime;
  tm newtime;
  newtime = getDateTimeByParams(time);
  return getDateTimeStringByParams(&newtime, pattern);
}

char tmnow[9] = "00:00:00";         //  "hh:mm:ss"
char dt[11]   = "00.00.0000";

void loop() {
  static uint32_t pTime;
  if (millis() - pTime > 1000) {
    pTime = millis();

    time_t ora;
    tm tInfo;   
    getLocalTime(&tInfo);        //Update tInfo struct
   
    Serial.print("\n tInfo.tm_mday      : ");  Serial.println(tInfo.tm_mday);
    Serial.print(" tInfo.tm_mon +1    : ");  Serial.println(tInfo.tm_mon+1);
    Serial.print(" tInfo.tm_year+1900 : ");  Serial.println(tInfo.tm_year+1900);
    Serial.print(" tInfo.tm_hour      : ");  Serial.println(tInfo.tm_hour);
    Serial.print(" tInfo.tm_min       : ");  Serial.println(tInfo.tm_min);
    Serial.print(" tInfo.tm_sec       : ");  Serial.println(tInfo.tm_sec);
    Serial.print(" tInfo.tm_wday      : ");  Serial.println(tInfo.tm_wday);
    Serial.print(" tInfo.tm_isdst     : ");  Serial.println(tInfo.tm_isdst);
   
    ora = mktime(&tInfo);
    setTime(tInfo.tm_hour, tInfo.tm_min, tInfo.tm_sec, tInfo.tm_mday, tInfo.tm_mon + 1, tInfo.tm_year + 1900);
   
  Serial.println("\n----------  setTime(tmt.tm_hour, tmt.tm_min,...... ");
  Serial.print("\n1. ora                  : "); Serial.println(ora);
  Serial.print("1. now()                : "); Serial.println(now());
  Serial.print("1. getTimeGMY_HMS(now()): "); Serial.println(getTimeGMY_HMS(now()));
  Serial.print("1. getTimeGMY_HMS(ora)  : "); Serial.println(getTimeGMY_HMS(ora));
  sprintf(tmnow, "%02u:%02u:%02u", hour(), minute(), second());
  sprintf(dt, "%02d.%02d.%04d", day(), month(), year());
  Serial.print("1. ora-data hour()      : "); Serial.print(dt); Serial.print("-"); Serial.println(tmnow);
  sprintf(tmnow, "%02u:%02u:%02u", hour(now()), minute(now()), second(now()));
  sprintf(dt, "%02d.%02d.%04d", day(now()), month(now()), year(now()));
  Serial.print("1. ora-data hour(now()) : "); Serial.print(dt); Serial.print("-"); Serial.println(tmnow);
  Serial.print("1. ora                  : ");Serial.println(ctime(&ora));
 
  setTime(ora);
  Serial.println("\n---------- setTime(ora) --------------------");
  Serial.print("\n2. ora                  : "); Serial.println(ora);
  Serial.print("2. now()                : "); Serial.println(now());
  Serial.print("2. getTimeGMY_HMS(now()): "); Serial.println(getTimeGMY_HMS(now()));
  Serial.print("2. getTimeGMY_HMS(ora)  : "); Serial.println(getTimeGMY_HMS(ora));
  sprintf(tmnow, "%02u:%02u:%02u", hour(), minute(), second());
  sprintf(dt, "%02d.%02d.%04d", day(), month(), year());
  Serial.print("2. ora-data hour()      : "); Serial.print(dt); Serial.print("-"); Serial.println(tmnow);
   
  sprintf(tmnow, "%02u:%02u:%02u", hour(now()), minute(now()), second(now()));
  sprintf(dt, "%02d.%02d.%04d", day(now()), month(now()), year(now()));
  Serial.print("2. ora-data hour(now()) : "); Serial.print(dt); Serial.print("-"); Serial.println(tmnow);
  Serial.print("2. ora                  : ");Serial.println(ctime(&ora));
  }
  delay(60000); // Increase simulation performance
}

void setup() {
  Serial.begin(115200);
   
  Serial.println("Connecting to WiFi ");
  WiFi.begin("R_1_Mya_2.4", "1_Marina_0hkl");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.print("\nWiFi connected. ");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP().toString());

  // Sync time with NTP server
  configTime(MYTZ, "time.google.com", "time.windows.com", "pool.ntp.org");
}

this is the log
Code: Select all18:01:05.850 -> ..............
18:01:13.083 -> WiFi connected. IP address:
18:01:13.083 -> 192.168.2.169
18:01:13.177 ->
18:01:13.177 ->  tInfo.tm_mday      : 27
18:01:13.177 ->  tInfo.tm_mon +1    : 3
18:01:13.177 ->  tInfo.tm_year+1900 : 2023
18:01:13.177 ->  tInfo.tm_hour      : 18
18:01:13.177 ->  tInfo.tm_min       : 1
18:01:13.177 ->  tInfo.tm_sec       : 12
18:01:13.177 ->  tInfo.tm_wday      : 1
18:01:13.177 ->  tInfo.tm_isdst     : 1
18:01:13.177 ->
18:01:13.177 -> ----------  setTime(tmt.tm_hour, tmt.tm_min,......
18:01:13.177 ->
18:01:13.177 -> 1. ora                  : 1679932872
18:01:13.177 -> 1. now()                : 1679940072
18:01:13.177 -> 1. getTimeGMY_HMS(now()): 27.03.2023-20:01:12
18:01:13.177 -> 1. getTimeGMY_HMS(ora)  : 27.03.2023-18:01:12
18:01:13.177 -> 1. ora-data hour()      : 27.03.2023-18:01:12
18:01:13.177 -> 1. ora-data hour(now()) : 27.03.2023-18:01:12
18:01:13.224 -> 1. ora                  : Mon Mar 27 18:01:12 2023
18:01:13.224 ->
18:01:13.224 ->
18:01:13.224 -> ---------- setTime(ora) --------------------
18:01:13.224 ->
18:01:13.224 -> 2. ora                  : 1679932872
18:01:13.224 -> 2. now()                : 1679932872
18:01:13.224 -> 2. getTimeGMY_HMS(now()): 27.03.2023-18:01:12
18:01:13.224 -> 2. getTimeGMY_HMS(ora)  : 27.03.2023-18:01:12
18:01:13.224 -> 2. ora-data hour()      : 27.03.2023-16:01:12
18:01:13.224 -> 2. ora-data hour(now()) : 27.03.2023-16:01:12
18:01:13.224 -> 2. ora                  : Mon Mar 27 18:01:12 2023

Re: different behavior using setTime(time_t) and setTime(hou

PostPosted: Wed Apr 12, 2023 7:39 am
by rooppoorali
The difference in behavior that you are experiencing with setTime(time_t) and setTime(hour, minutes, seconds, day, month, year) is likely due to how these functions handle the timezone information. I think you need to check the documentations of time.h and timeLib.h libraries.