Comments on: ESP32 NTP Time – Setting Up Timezones and Daylight Saving Time https://randomnerdtutorials.com/esp32-ntp-timezones-daylight-saving/ Learn ESP8266, ESP32, Arduino, and Raspberry Pi Wed, 23 Oct 2024 06:11:06 +0000 hourly 1 https://wordpress.org/?v=6.8.2 By: Jacques Lemaire https://randomnerdtutorials.com/esp32-ntp-timezones-daylight-saving/#comment-975254 Wed, 23 Oct 2024 06:11:06 +0000 https://randomnerdtutorials.com/?p=107914#comment-975254 In reply to Bert.

Oh sorry, in my last code, I observed an hour value incrementation when dst is active, because I omitted to define correctly the tm_isdst parameter.
Then I added an instruction in my getCustomTime function :
void getCustomTime(int year, int month, int day, int hour, int minute, int second, tm *timePtr)
{
timePtr->tm_year = year – 1900;
timePtr->tm_mon = month-1;
timePtr->tm_mday = day;
timePtr->tm_hour = hour;
timePtr->tm_min = minute;
timePtr->tm_sec = second;
timePtr->tm_isdst = 0;
time_t t = mktime(timePtr);
memcpy(timePtr, localtime(&t), sizeof(tm));
if (timePtr->tm_isdst==1) timePtr->tm_hour–;
}
In your code, the tm_isdst field is a parameter of the printLocalTime() function ; so no problem ! Except that it’s not obvious to know what is its correct value…
I would also reprecise that I don’t want to modify the RTC value : just to obtain a custom POSIX time in the “YYYY-MM-DDT00:00:00zzzzzz” format, because required in the RTE’s API Tempo Like Supply Contract.

]]>
By: Jacques Lemaire https://randomnerdtutorials.com/esp32-ntp-timezones-daylight-saving/#comment-975063 Tue, 22 Oct 2024 21:14:35 +0000 https://randomnerdtutorials.com/?p=107914#comment-975063 In reply to Bert.

You will find below the complete code for PlatformIO (main.cpp).
I just changed the name of the function to get a custom time : getCustomTime (analog to getLocalTime)
In comments, the suggested code by Random Nerd tutorial, completed to restore the RTC current time.
Thanks to say if may proposition is correct.
Best regards.
Jac
NB : in Paris, the time offset changed on the 31/03/2024 : +1h because summer time.

// Time – main.cpp

/***********************************************************************************

Objective
Time utilities for ESP32, that includes TZ and DST adjustments.
Get the POSIX style TZ format string from https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv
Inspired from Hardy Maxa and Random Nerd tutorials.

Output
IP=192.168.1.19 RSSI=-76
Current date-time : 2024-10-21 09:14:06+0200
Custom date-time : 2024-03-31 00:00:00+0100
Custom date-time : 2024-04-01 01:00:00+0200

References
– Wifi :
. https://randomnerdtutorials.com/esp32-useful-wi-fi-functions-arduino/
– Current time :
. https://randomnerdtutorials.com/esp32-date-time-ntp-client-server-arduino/
. https://randomnerdtutorials.com/esp32-ntp-timezones-daylight-saving/
. sourceware.org/newlib/libc.html#Timefns
. cplusplus.com/reference/ctime/
. github.com/espressif/arduino-esp32/blob/master/cores/esp32/esp32-hal-time.c#L47

/***********************************************************************************
Libraries and types
***********************************************************************************/

#include <WiFi.h>
#include <time.h>

/***********************************************************************************
Constants
***********************************************************************************/

// Local network access point
const char *SSID = “Livebox-486c”;
const char *PWD = “FAAD971F372AA5CCE13D25969E”;

// NTP server (=>UTC time) and Time zone
const char* NTP_SERVER = “pool.ntp.org”; // Server address (or “ntp.obspm.fr”, “ntp.unice.fr”, …)
const char* TIME_ZONE = “CET-1CEST,M3.5.0,M10.5.0/3”; // Europe/Paris time zone

/***********************************************************************************
Tool functions
***********************************************************************************/

void initTime(const char *timeZone)
{
struct tm t;
configTime(0, 0, NTP_SERVER);
while (!getLocalTime(&t)); // Here use NTP server
setenv(“TZ”, timeZone, 1);
tzset();
}

void getCustomTime(int year, int month, int day, int hour, int minute, int second, tm timePtr)
{
timePtr->tm_year = year – 1900;
timePtr->tm_mon = month-1;
timePtr->tm_mday = day;
timePtr->tm_hour = hour;
timePtr->tm_min = minute;
timePtr->tm_sec = second;
timePtr->tm_isdst = 0;
time_t t = mktime(timePtr);
memcpy(timePtr, localtime(&t), sizeof(tm));
/

tm savTime;
getLocalTime(&savTime);
struct timeval now = {.tv_sec = t};
settimeofday(&now, NULL);
getLocalTime(timePtr);
t = mktime(&savTime);
now = {.tv_sec = t};
settimeofday(&now, NULL);
*/
}

/***********************************************************************************
setup and loop functions
***********************************************************************************/

void setup()
{
// Open serial port
Serial.begin(115200);
while (!Serial) {}

// Connect to the Wifi access point
WiFi.begin(SSID, PWD);
while (WiFi.status() != WL_CONNECTED); // delay(500);
Serial.printf(“IP=%s RSSI=%d\n”, WiFi.localIP().toString(), WiFi.RSSI());

// Init time
initTime(TIME_ZONE);

// Current date-time using RTC
struct tm time;
char buf[30];
getLocalTime(&time);
strftime(buf, sizeof(buf), “%Y-%m-%d %H:%M:%S%z\n”, &time);
Serial.printf(“Current date-time : %s”, buf);

// Custom date-time when offset changes
getCustomTime(2024, 3, 31, 0, 0, 0, &time);
strftime(buf, sizeof(buf), “%Y-%m-%d %H:%M:%S%z\n”, &time);
Serial.printf(“Custom date-time : %s”, buf);
getCustomTime(2024, 4, 1, 0, 0, 0, &time);
strftime(buf, sizeof(buf), “%Y-%m-%d %H:%M:%S%z\n”, &time);
Serial.printf(“Custom date-time : %s”, buf);
}

void loop()
{
}

]]>
By: Bert https://randomnerdtutorials.com/esp32-ntp-timezones-daylight-saving/#comment-974923 Tue, 22 Oct 2024 13:11:33 +0000 https://randomnerdtutorials.com/?p=107914#comment-974923 In reply to Jacques Lemaire.

Please do, Thank you

]]>
By: Jacques Lemaire https://randomnerdtutorials.com/esp32-ntp-timezones-daylight-saving/#comment-974809 Tue, 22 Oct 2024 05:55:52 +0000 https://randomnerdtutorials.com/?p=107914#comment-974809 Hi Sara and Rui,
Very good and useful work !! In a photovoltaic system, I used it to obtain in France the Tempo day color from the “RTE API Tempo Like Supply Contract”, wich requires time parameter in POSIX format (with offset indication).
But I wonder if one can simplify your setTime function, like this :
void setCustomTime(int year, int month, int day, int hour, int minute, int second, tm *timePtr)
{
timePtr->tm_year = year – 1900;
timePtr->tm_mon = month-1;
timePtr->tm_mday = day;
timePtr->tm_hour = hour;
timePtr->tm_min = minute;
timePtr->tm_sec = second;
timePtr->tm_isdst = 0;
time_t t = mktime(timePtr);
memcpy(timePtr, localtime(&t), sizeof(tm));
}
It looks like working correctly and avoids to change the RTC clock value.
Do you think so ?
Best regards.
Jac
NB : if you want, I can send you my complete code

]]>
By: Rui Santos https://randomnerdtutorials.com/esp32-ntp-timezones-daylight-saving/#comment-935829 Tue, 09 Jul 2024 20:28:59 +0000 https://randomnerdtutorials.com/?p=107914#comment-935829 In reply to Vladimir Gershman.

Yes.
This is only compatible with ESP32.
Regards.
Rui

]]>
By: Vladimir Gershman https://randomnerdtutorials.com/esp32-ntp-timezones-daylight-saving/#comment-934502 Sat, 06 Jul 2024 02:04:42 +0000 https://randomnerdtutorials.com/?p=107914#comment-934502 Hello,

i tried running this tutorial for ESp8266 and i get a compilation error “error: no matching function for call to ‘println(tm*, const char [34])'” on line :
Serial.println(&timeinfo, “%A, %B %d %Y %H:%M:%S zone %Z %z “);

Is that because of ESp8266?

Thanks

]]>
By: Gabriele https://randomnerdtutorials.com/esp32-ntp-timezones-daylight-saving/#comment-919372 Wed, 29 May 2024 16:18:58 +0000 https://randomnerdtutorials.com/?p=107914#comment-919372 I didn’t under stand very well how to obtain our timezone. When i insert the “code” near TX i obtain a standard hour (or I think so). How can I have the timezone where i’m in and doing anything else?

]]>
By: Bert https://randomnerdtutorials.com/esp32-ntp-timezones-daylight-saving/#comment-914184 Fri, 10 May 2024 18:21:28 +0000 https://randomnerdtutorials.com/?p=107914#comment-914184 Maybe a stupid question, but is the time notation also available in a language other than English? And if so, how should that be done?
Thanks in advance

]]>
By: Laurens https://randomnerdtutorials.com/esp32-ntp-timezones-daylight-saving/#comment-906973 Wed, 17 Apr 2024 19:11:48 +0000 https://randomnerdtutorials.com/?p=107914#comment-906973 Nice code.
But i am building a clock where i am using this code.
After 14 days the time gets off.
How can i force to get the time from the NTP-server again, so the time is right again?

]]>
By: Wolfgang https://randomnerdtutorials.com/esp32-ntp-timezones-daylight-saving/#comment-872534 Fri, 17 Nov 2023 10:33:48 +0000 https://randomnerdtutorials.com/?p=107914#comment-872534 In reply to Jordi Bosch.

Hello,

for europe use this timeserver:
String ntp = “de.pool.ntp.org”;
and
configTzTime(“CET-1CEST,M3.5.0/03,M10.5.0/03”, ntp.c_str());

then it works perfect with summer and winter time

Regards

]]>