Solved - Using an ESP32, how to put a time.h derived week number into a variable

Hello, I'm actually wanting to automate the change from Day Light Saving time to ordinary New Zealand time as part of an ESP32 program, but there doesn't seem to be an easy way to do it. Then I had the idea that since I can get an NTP Server time off the internet which includes a week of the year, then that would suffice because Day Light Saving time finishes on Week 13 (First Week = 0) and commences again on Week 35 most years. I can simulate this on a spreadsheet and is accurate enough for my purposes.
Using the time.h library program, I found there is a variable called %U which will give me the week number, and it prints out on the Serial Monitor. So my thought was to adjust the Greenwich Mean Time offset appropriately for those weeks. My most humble of apologies, but I just can't find a way to get week number result into a variable. I can print it out using Serial.println(&timeinfo, "%U"); but there doesn't seem to be a way to get the result e.g. 49, into a variable. I've attached the complete code (excluding my Wifi login). Thanks in advance.

ESP32-DateandTime-ForPosting.ino (3.18 KB)

getLocalTime return a struct of tm (as you have it declared). Therefore you can access any of the struct members.

for example in your code you would reference day in the year as timeinfo.tm_yday. You could use day in the year to calculate week of the year.

struct tm {
   int tm_sec;         /* seconds,  range 0 to 59          */
   int tm_min;         /* minutes, range 0 to 59           */
   int tm_hour;        /* hours, range 0 to 23             */
   int tm_mday;        /* day of the month, range 1 to 31  */
   int tm_mon;         /* month, range 0 to 11             */
   int tm_year;        /* The number of years since 1900   */
   int tm_wday;        /* day of the week, range 0 to 6    */
   int tm_yday;        /* day in the year, range 0 to 365  */
   int tm_isdst;       /* daylight saving time             */
};

FYI, "%U" is not a variable but a format specifier for printing.

You could also use

size_t strftime(char *s, size_t max, const char *format, const struct tm *tm);

To extract the week number using the "%U" format specifier and then convert it to an integer for computations.

Thanks for the helpful replies, but you're talking way over my head. I can understand the reply from the NTP server is stored as a string, but I how do get to look at it, especially the bit with the week number?

Hello, Just to let you all know I think I've managed, with Todd's help (@ToddL1962) and the help of other people, to resolve the problem I had. I found some code that used the strftime command to print out some time and date information via a buffer. After some dabbling around I found I could use the atoi command to get week number information from the buffer to into an integer variable called iWeekNum. From there it became much easier. I apply that number to an IF command with a logic AND to test if the Week is 13 and above AND 35 or less. If it is then I use the winter GMT offset and if the week number fails the logic test then I use the Daylight Saving time GMT offset.
I've attached the code if anyone is interested.

ESP32-DateandTime2-ForPosting.ino (1.93 KB)

1 Like

One last point: I found the C++ website (http://www.cplusplus.com) was a great assistance to getting a resolution.

I'm very sorry, but I can't find any way to mark this question as resolved.

drycrust:
I'm very sorry, but I can't find any way to mark this question as resolved.

Under your opening post, more -> modifiy. Next change the title to include the something like [Solved] in the beginning of the title or at the end.

drycrust:
Thanks for the helpful replies, but you're talking way over my head. I can understand the reply from the NTP server is stored as a string, but I how do get to look at it, especially the bit with the week number?

Sorry about that. Actually the reply is not stored as a string. It is the format specifiers in the print that convert it to a string and print it. It sounds like you have found a solution but let me know if you have any other questions and I will try to answer with more specific answers.

I'm afraid I have almost no idea why what I did works, but it does, or at least it seems to. The important thing is I've got a resolution to my problem. My thanks to all those who helped.