Strange things using NTP DST of the ESP8266 core
Posted: Thu Mar 23, 2023 11:17 am
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). I've always used the NTP Client to manage the date and time zone and I've never encountered the problem I am describing (I am also attaching the sketch and the log).
If I use setTime (date in seconds) the value of now() and hour() are different: now() is in line with the current time, hour() is one hour behind.
If I use 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
the code is
the log is
00:42:17.021 ->
00:42:17.021 -> Test NTP TZ DST
00:42:17.212 -> ...............
00:42:20.832 -> time was updated!
00:42:20.832 ->
00:42:20.832 -> local: Thu Mar 23 00:42:20 2023
00:42:20.878 ->
00:42:20.878 -> ---------- setTime(local)
00:42:20.878 -> 1. local : 1679528540
00:42:20.878 -> 1. now() : 1679528540
00:42:20.878 -> 1. getTimeGMY_HMS(now()): 23.03.2023-00:42:20
00:42:20.878 -> 1. ora-data hour() : 22.03.2023-23:42:20
00:42:20.878 ->
00:42:20.878 -> ---------- setTime(tmt.tm_hour, tmt.tm_min,......
00:42:20.878 -> 2. local : 1679528540
00:42:20.878 -> 2. now() : 1679532140
00:42:20.878 -> 2. getTimeGMY_HMS(now()): 23.03.2023-01:42:20
00:42:20.878 -> 2. ora-data hour() : 23.03.2023-00:42:20
00:42:20.878 ->
00:42:20.878 -> wday4 - standard - Fuso orario = 1
If I use setTime (date in seconds) the value of now() and hour() are different: now() is in line with the current time, hour() is one hour behind.
If I use 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
the code is
Code: Select all
#ifndef STASSID
#define STASSID "xxxxxxxx" // set your SSID
#define STAPSK "xxxxxxxx" // set your wifi password
#endif
/* Configuration of NTP */
#define MY_NTP_SERVER "europe.pool.ntp.org"
#define MY_TZ "CET-1CEST,M3.5.0/02,M10.5.0/03"
#include <ESP8266WiFi.h> // we need wifi to get internet access
#include <TimeLib.h>
#include <time.h> // time() ctime()
/* Globals */
time_t dtora;
tm tmt; // the structure tm holds time information in a more convient way
time_t ora;
char tmnow[9] = "00:00:00"; // "hh:mm:ss"
char dt[11] = "00.00.0000";
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);
}
/*------------------------------------------------------*/
uint32_t sntp_update_delay_MS_rfc_not_less_than_15000 () {
Serial.println(F("\ntime was updated! "));
upd_DateTime();
while ((tmt.tm_year + 1900) == 1970) {
upd_DateTime();
}
// return 12 * 60 * 60 * 1000UL; // 12 hours
return 1 * 60 * 1000UL; // 1 minuti
}
/*------------------------------------------------------*/
void upd_DateTime() {
Serial.println(" ");
time(&dtora); // read the current time
localtime_r(&dtora, &tmt); // update the structure tm with the current time
if ((tmt.tm_year + 1900) == 1970) {
return;
}
printf("local: %s", asctime(localtime(&dtora)));
Serial.println("\n---------- setTime(local)");
setTime(dtora);
Serial.print("1. local : ");Serial.println(dtora);
Serial.print("1. now() : ");Serial.println(now());
Serial.print("1. getTimeGMY_HMS(now()): ");Serial.println(getTimeGMY_HMS(now()));
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);
Serial.println(" ");
Serial.println("---------- setTime(tmt.tm_hour, tmt.tm_min,...... ");
time(&dtora); // read the current time
localtime_r(&dtora, &tmt); // update the structure tm with the current time
setTime(tmt.tm_hour, tmt.tm_min, tmt.tm_sec, tmt.tm_mday, tmt.tm_mon + 1, tmt.tm_year + 1900);
Serial.print("2. local : ");Serial.println(dtora);
Serial.print("2. now() : ");Serial.println(now());
Serial.print("2. getTimeGMY_HMS(now()): ");Serial.println(getTimeGMY_HMS(now()));
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);
Serial.print("\nwday");
Serial.print(tmt.tm_wday); // days since Sunday 0-6
if (tmt.tm_isdst == 1) { // Daylight Saving Time flag
Serial.print(" - DST");
Serial.println(" - Fuso orario = 2");
}else{
Serial.print(" - standard");
Serial.println(" - Fuso orario = 1");
}
}
void setup() {
Serial.begin(115200);
delay(2000);
Serial.println("\nTest NTP TZ DST ");
configTime(MY_TZ, MY_NTP_SERVER); // --> Here is the IMPORTANT ONE LINER needed in your sketch!
// start network
WiFi.persistent(false);
WiFi.mode(WIFI_OFF);
WiFi.mode(WIFI_STA);
WiFi.begin(STASSID, STAPSK);
while (WiFi.status() != WL_CONNECTED) {
delay(200);
Serial.print ( "." );
}
Serial.println("\nWiFi connected");
// by default, the NTP will be started after 60 secs
}
void loop() {
}
the log is
00:42:17.021 ->
00:42:17.021 -> Test NTP TZ DST
00:42:17.212 -> ...............
00:42:20.832 -> time was updated!
00:42:20.832 ->
00:42:20.832 -> local: Thu Mar 23 00:42:20 2023
00:42:20.878 ->
00:42:20.878 -> ---------- setTime(local)
00:42:20.878 -> 1. local : 1679528540
00:42:20.878 -> 1. now() : 1679528540
00:42:20.878 -> 1. getTimeGMY_HMS(now()): 23.03.2023-00:42:20
00:42:20.878 -> 1. ora-data hour() : 22.03.2023-23:42:20
00:42:20.878 ->
00:42:20.878 -> ---------- setTime(tmt.tm_hour, tmt.tm_min,......
00:42:20.878 -> 2. local : 1679528540
00:42:20.878 -> 2. now() : 1679532140
00:42:20.878 -> 2. getTimeGMY_HMS(now()): 23.03.2023-01:42:20
00:42:20.878 -> 2. ora-data hour() : 23.03.2023-00:42:20
00:42:20.878 ->
00:42:20.878 -> wday4 - standard - Fuso orario = 1