In this tutorial, you’ll learn how to properly get the time with the ESP32 for your timezone and consider daylight saving time (if that’s the case). The ESP32 will request the time from an NTP server, and the time will be automatically adjusted for your timezone with or without daylight saving time.
Quick Answer: call setenv(“TZ”, timezone, 1), where timezone is one of the timezones listed here. Then, call tzset() to update to that timezone.
If you’re getting started, we recommend taking a look at the following tutorial first to learn how to get date and time from an NTP server:
In that previous tutorial, we’ve shown an option to set up your timezone. However, that example doesn’t take into account daylight saving time. Continue reading this tutorial to learn how to set up the timezone and daylight saving time properly.
Thanks to one of our readers (Hardy Maxa) who shared this information with us.
ESP32 Setting Timezone with Daylight Saving Time
According to documentation:
“To set local timezone, use setenv and tzset POSIX functions. First, call setenv to set TZ environment variable to the correct value depending on device location. Format of the time string is described in libc documentation. Next, call tzset to update C library runtime data for the new time zone. Once these steps are done, localtime function will return correct local time, taking time zone offset and daylight saving time into account.”
You can check a list of timezone string variables here.
For example, I live in Porto. The timezone is Europe/Lisbon. From the list of timezone string variables, I see that the timezone string variable for my location is WET0WEST,M3.5.0/1,M10.5.0, so after connecting to the NTP server, to get the time for my location I need to call:
setenv("TZ","WET0WEST,M3.5.0/1,M10.5.0",1);
Followed by:
tzset();
Let’s look at a demo sketch to understand how it works and how to use it in your ESP32 project.
ESP32 Timezone and DST Example Sketch
The following example was provided by one of our followers (Hardy Maxa), we’ve just made a few modifications.
Copy the following code to your Arduino IDE.
// RTC demo 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
// Created by Hardy Maxa
// Complete project details at: https://RandomNerdTutorials.com/esp32-ntp-timezones-daylight-saving/
#include <WiFi.h>
#include "time.h"
const char * ssid="REPLACE_WITH_YOUR_SSID";
const char * wifipw="REPLACE_WITH_YOUR_PASSWORD";
void setTimezone(String timezone){
Serial.printf(" Setting Timezone to %s\n",timezone.c_str());
setenv("TZ",timezone.c_str(),1); // Now adjust the TZ. Clock settings are adjusted to show the new local time
tzset();
}
void initTime(String timezone){
struct tm timeinfo;
Serial.println("Setting up time");
configTime(0, 0, "pool.ntp.org"); // First connect to NTP server, with 0 TZ offset
if(!getLocalTime(&timeinfo)){
Serial.println(" Failed to obtain time");
return;
}
Serial.println(" Got the time from NTP");
// Now we can set the real timezone
setTimezone(timezone);
}
void printLocalTime(){
struct tm timeinfo;
if(!getLocalTime(&timeinfo)){
Serial.println("Failed to obtain time 1");
return;
}
Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S zone %Z %z ");
}
void startWifi(){
WiFi.begin(ssid, wifipw);
Serial.println("Connecting Wifi");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.print("Wifi RSSI=");
Serial.println(WiFi.RSSI());
}
void setTime(int yr, int month, int mday, int hr, int minute, int sec, int isDst){
struct tm tm;
tm.tm_year = yr - 1900; // Set date
tm.tm_mon = month-1;
tm.tm_mday = mday;
tm.tm_hour = hr; // Set time
tm.tm_min = minute;
tm.tm_sec = sec;
tm.tm_isdst = isDst; // 1 or 0
time_t t = mktime(&tm);
Serial.printf("Setting time: %s", asctime(&tm));
struct timeval now = { .tv_sec = t };
settimeofday(&now, NULL);
}
void setup(){
Serial.begin(115200);
Serial.setDebugOutput(true);
startWifi();
initTime("WET0WEST,M3.5.0/1,M10.5.0"); // Set for Melbourne/AU
printLocalTime();
}
void loop() {
int i;
// put your main code here, to run repeatedly:
Serial.println("Lets show the time for a bit. Starting with TZ set for Melbourne/Australia");
for(i=0; i<10; i++){
delay(1000);
printLocalTime();
}
Serial.println();
Serial.println("Now - change timezones to Berlin");
setTimezone("CET-1CEST,M3.5.0,M10.5.0/3");
for(i=0; i<10; i++){
delay(1000);
printLocalTime();
}
Serial.println();
Serial.println("Now - Lets change back to Lisbon and watch Daylight savings take effect");
setTimezone("WET0WEST,M3.5.0/1,M10.5.0");
printLocalTime();
Serial.println();
Serial.println("Now change the time. 1 min before DST takes effect. (1st Sunday of Oct)");
Serial.println("AEST = Australian Eastern Standard Time. = UTC+10");
Serial.println("AEDT = Australian Eastern Daylight Time. = UTC+11");
setTime(2021,10,31,0,59,50,0); // Set it to 1 minute before daylight savings comes in.
for(i=0; i<20; i++){
delay(1000);
printLocalTime();
}
Serial.println("Now change the time. 1 min before DST should finish. (1st Sunday of April)");
setTime(2021,3,28,1,59,50,1); // Set it to 1 minute before daylight savings comes in. Note. isDst=1 to indicate that the time we set is in DST.
for(i=0; i<20; i++){
delay(1000);
printLocalTime();
}
// Now lets watch the time and see how long it takes for NTP to fix the clock
Serial.println("Waiting for NTP update (expect in about 1 hour)");
while(1) {
delay(1000);
printLocalTime();
}
}
How the Code Works
First, you need to include the WiFi library to connect the ESP32 to the internet (NTP server) and the time library to deal with time.
#include <WiFi.h>
#include "time.h"
To set the timezone, we created a function called setTimezone() that accepts as an argument a timezone string.
void setTimezone(String timezone){
Inside that function, we call the setenv() function for the TZ (timezone) parameter to set the timezone with whatever timezone you pass as an argument to the setTimezone() function.
setenv("TZ",timezone.c_str(),1); // Now adjust the TZ. Clock settings are adjusted to show the new local time
After that, call the tzset() function for the changes to take effect.
tzset();
We won’t go into detail about the other functions declared in the code because those were already explained in a previous tutorial:
setup()
In the setup(), we initialize Wi-Fi so that the ESP32 can connect to the internet to connect to the NTP server.
initWifi();
Then, call the initTime() function and pass as argument the timezone String. In our case, for Lisbon/PT timezone.
initTime("WET0WEST,M3.5.0/1,M10.5.0"); // Set for Lisbon/PT
After that, print the current local time.
printLocalTime();
loop()
In the loop() show the local time for ten seconds.
Serial.println("Lets show the time for a bit. Starting with TZ set for Lisbon/Portugal");
for(i=0; i<10; i++){
delay(1000);
printLocalTime();
}
If at any time in your code you need to change the timezone, you can do it by calling the setTimezone() function and passing as an argument the timezone string. For example, the following lines change the timezone to Berlin and display the time for ten seconds.
Serial.println();
Serial.println("Now - change timezones to Berlin");
setTimezone("CET-1CEST,M3.5.0,M10.5.0/3");
for(i=0; i<10; i++){
delay(1000);
printLocalTime();
}
Then, we go back to our local time by calling the setTimezone() function again with the Lisbon/PT timezone string variable.
Serial.println();
Serial.println("Now - Lets change back to Lisbon and watch Daylight savings take effect");
setTimezone("WET0WEST,M3.5.0/1,M10.5.0");
printLocalTime();
To check if daylight saving time is taking effect, we’ll change the time to 10 seconds before the winter time takes effect. In our case, it is on the last Sunday of October (it might be different for your location).
Serial.println();
Serial.println("Now change the time. 1 min before DST takes effect. (Last Sunday of Oct)");
setTime(2021,10,31,0,59,50,0); // Set it to 1 minute before daylight savings comes in.
Then, show the time for a while to check that it is adjusting the time, taking into account DST.
for(i=0; i<20; i++){
delay(1000);
printLocalTime();
}
After this, let’s change the time again to check if it changes to summer time when it comes the time. In our case, it is on the last Sunday of March.
Serial.println("Now change the time. 1 min before DST should finish. (Last Sunday of March)");
setTime(2021,3,28,1,59,50,1); // Set it to 1 minute before daylight savings comes in. Note. isDst=1 to indicate that the time we set is in DST.
Show the time for a while to check that it is adjusting to the summer time.
for(i=0; i<20; i++){
delay(1000);
printLocalTime();
}
Demonstration
Now, let’s test the code. After inserting your network credentials, upload the code to your ESP32.
After that, open the Serial Monitor at a baud rate of 115200 and press the ESP32 RST button to start running the code.
First, it shows your local time with the timezone you’ve set on the code.
After that, it will change to the other timezone you’ve set on the code. In our case, we set to Berlin.
After that, you should see the change to winter time. In our case, when it’s 2 a.m., the clock goes back one hour.
We also check if it adjusts to summer time when it comes the time. In the example below, you can see that the clock goes forward one hour to set summer time.
Wrapping Up
This quick tutorial taught you how to set timezone with daylight saving time using the setenv() and tzset() functions.
We hope you found this tutorial useful. We have other tutorials related to time that you may like:
- ESP32 NTP Client-Server: Get Date and Time (Arduino IDE)
- Getting Date and Time with ESP32 on Arduino IDE (NTP Client)
- ESP32 Data Logging Temperature to MicroSD Card (with NTP time)
Learn more about the ESP32 with our resources:
- Build Web Servers with ESP32 and ESP8266 eBook
- Learn ESP32 with Arduino IDE (eBook + video course)
- More ESP32 tutorials and projects…
Thank you for reading.
very good tutorial as usual: thanks a lot.
I have few questions to ask:
I use NTPClient.h / NTPClient timeClient(ntpUDP); // NTP clientfor time.
I set time zone thus…
timeClient.begin(); // init NTP
timeClient.setTimeOffset(0); // 0= GMT, 3600 = GMT+1
Is there anyway to achieve daylight saving ?
Hi.
I’m sorry. I didn’t understand your question.
It is explained in the tutorial that if you use the method described, it takes into account timezone and daylight saving time.
Regards,
Sara
Is there anyway for the update for daylight saving to adjust automatically.. For @ present I have to change the code to account for.
Hi.
Yes.
With the method we described here, it is adjusted automatically.
You just need to search for the correct timezone string for your location in this link: https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv
And use it in the code provided.
Regards,
Sara
Not to sure how to replace this …
timeClient.begin(); // init NTP
timeClient.setTimeOffset(0); // 0= GMT, 3600 = GMT+1
with this…
GMT0BST,M3.5.0/1,M10.5.0
Very good project. It worked perfectly with my timezone.
I supose it work on 8266 as well !?
I receive error: ‘getLocalTime’ was not declared in this scope
when compile for 8266.
Only modify I make is use ESP8266WiFi.h instead of WIFI.h
Hi.
The ESP8266 uses different methods to get the time.
– https://randomnerdtutorials.com/epoch-unix-time-esp8266-nodemcu-arduino/
– https://randomnerdtutorials.com/esp8266-nodemcu-date-time-ntp-client-server-arduino/
Regards,
Sara
I have one question. In struct tm tm, the 7 fields are defined as integers. Yet in the print command (Serial.println(&timeinfo, “%A, %B %d %Y %H:%M:%S zone %Z %z “);) the output for “day” or “month” for example are actually strings. Where is the translation done to convert the integers to strings ?
btw, the program works fine, I am just curious and would like to abbreviate the day and month.
Thanks very much.
Never mind. I found the answer in one of your previous project tutorials using ESP32 NTP.
Thanks again.
I am tried this in visual code to use setenv but it doesnt accept the command. Is there some kind of lib that needed to be imported?
You need to add in your sketch:
#include “time.h”
To include the time library.
Regards,
Sara
The last Sunday in March and October are set manually in the program. This code calculates these numbers automatically:
yy = yy % 100; // last two digits of the year
uint8_t x1 = 31 – (yy + yy / 4 – 2) % 7; // last Sunday March
uint8_t x2 = 31 – (yy + yy / 4 + 2) % 7; // last Sunday October
Actually: No, it doesn’t
The information about when daylight savings time starts and ends is included in the TMZ string “”WET0WEST,M3.5.0/1,M10.5.0”. It is this part “M3.5.0/1,M10.5.0” and you want to use that, since the dates are not necessarily identical in different time zones. Also sometimes (not often) the rules change and then the new rules can be applied with an update to the string instead of figuring out the then relevant formula.
Let setenv handle that string. It knows all about it.
I really appreciate your tutorials!
I have the “esp32-ntp-timezones-daylight-saving” tutorial running on VS Code with PlatformIO,
but I get two errors indicated, even though it compiles and runs well.
The errors are:
Identifier “setenv” is undefined
Identifier ‘tzset” is undefined
I have #include “time.h”, as you suggested in a comment above, but that does not eliminate the errors.
I found an explanation with a solution:
https://community.platformio.org/t/identifier-is-undefined-setenv-tzset/16162/2
To remove false Intellisense error squiggles:
_VOID _EXFUN(tzset, (_VOID));
int _EXFUN(setenv, (const char *__string, const char *__value, int __overwrite));
However, I just used the following:
extern void tzset(void);
extern int setenv(const char *__string, const char *__value, int __overwrite);
Hi.
If you’re running a code on PlatformIO, make sure you ad the following line at the beginning of your sketches:
#include <Arduino.h>
Regards,
Sara
In the last rows of the main loop is a while loop, stating:
// Now lets watch the time and see how long it takes for NTP to fix the clock
Serial.println(“Waiting for NTP update (expect in about 1 hour)”);
while(1) {
delay(1000);
printLocalTime();
}
I’m wondering how this can take place, imho NTP needs a network connection ?
Regards,
Aad Beentjes
hi there
I want to setup an alarm to start if the bottom not pressed at 11PM.here is my code, can anyone help please
regards
Adam
in this page
https://randomnerdtutorials.com/esp32-ntp-timezones-daylight-saving/
there is an error in that paragraph
“Quick Answer: call setenv(“TZ”, timezone, 1), where timezone is one of the timezones listed here. Then, call tzet() to update to that timezone.”
It should be tzset()
Thanks for noticing.
It’s fixed now.
Regards,
Sara
Everything you publish, just works.
Thanks very very much.
Hello Sarah
the 2 setTimes change summer or winter time but
I would like to return to the present time how to do thank you
There’s a typo in the article “tzet()” instead of “tzset()”
Many thanks, works great, up until now I was re-uploading every 6 months 🙂
Thanks for letting us know.
It’s fixed now.
Regards,
Sara
Hello Sarah
I found the solution by redefining the setTime but it’s not easy as a beginner thank you
setTime(timeinfo.tm_year, timeinfo.tm_mon, timeinfo.tm_mday, timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec, timeinfo.tm_isdst);
initTime(“CET-1CEST,M3.5.0,M10.5.0/3”);
Code example was not completely adapted from Australia to Portugal so code, comments and output are inconsistent.
Hi Sara,
thanks.
can the NTP automatically sign ESP32 a local time based on the login location? Or it has to fill the time zone string manually?
Best
Summer
Call http://worldtimeapi.org/api/ip and lookup the POSIX TZ (“CET-1CEST,M3.5.0,M10.5.0/3”) for the received “timezone” (“Europe/Berlin”) from https://ftp.fau.de/aminet/util/time/tzinfo.txt
Hi Sara,
Thanks.
Is it possible to sign the NTP got current time to ESP32 internal RTC like this:
rtc.setTime(&timeinfo);
Best.
Summer
How can I print the Time to a LCD Monitor?
I tried lcd.print(&timeinfo), but it did not work. Which variable would be suited best?
Ich habe dieses Beispiel versucht, aber eskommt eine Fehlermeldung wegen Serial.printf(…
Was fehlt denn bei mir?
Danke und Gruß
Sorry, Fehlermeldung vergessen
class HardwareSerial’ has no member named ‘printf’; did you mean ‘print’?
Danke
Hat sich erledigt, der Compiler stand noch auf Arduino nano. Sorry
Great.
I’m glad you find the issue.
Regards,
Sara
Hi Sarah, great code, it helps alot.
Need more info on this comment in Serial.print line at the end of loop()
Serial.println(“Waiting for NTP update (expect in about 1 hour)”);
Is this an automatic/default update period for time.h and if so how can i change it?
I am trying to do a multistage clock setting system:
get NTP time, set to localtime ( working )\
if NTP is not available:
set system time from GPS ( can not know if it’s working until I get a GPS antenna installed. it’s a tin roof thing )
if GPS is not available:
set system time from the RTC
I can’t find how to set system time from RTC. can you tell us how to do that?
Hello,
i am very interested in this program to get time and date.
But there is an error I can not eliminate:
exit status 1
‘getLocalTime’ was not declared in this scope; did you mean ‘printLocalTime’?
What can I do?
Thanks in advance!
Hi.
Make sure you’re using an ESP32 and that you have an ESP32 board selected in Tools > Board.
Additionally, make sure you’re running the latest version of the ESP32 boards. Go to tools > Board > Boards Manager. Then, search for ESP32 and check the version. The latest version is 2.0.9.
Let me know if this helps.
Regards,
Sara
sorry, I use ESP8266
I read above to use another link
Yes.
That’s right. This tutorial is only compatible with the ESP8266.
Regards,
Sara
Hi, I’ve been using this structure and struggling to get the timezone to adjust to anything other than an integer number of hours offset from GMT. I’m in Adelaide (ACST-9.30ACDT,M10.1.0,M4.1.0/3). The setTimezone seems to work for a 9 or 10, but rounds to 10 when I stick in the 9.30 that I need. Is there someway I can get it to set the “correct” time zone ? I can work around it externally by just adding or subtracting from an adjoining ‘integer’ timezone, but this seems inelegant. I’m guessing there’s something internal in setenv that’s an ‘int’ when it needs to be other. Is my guess correct?
Hi all,
Very helpful code, just comment that trying to reproduce it using Europe/Roma time zone CET-1CEST,M3.5.0,M10.5.0/3 , works when you specify complete time zone rule using CET-1CEST,M3.5.0/2,M10.5.0/3.
If someone is founds that code doesn’t work with their time zone, give it a try.
Best Regards
Jordi
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
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?
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
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?
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
Yes.
This is only compatible with ESP32.
Regards.
Rui
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
Please do, Thank you
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()
{
}
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.