How to get offset time.h?

String zone = "CET-1CEST,M3.5.0,M10.5.0/3";
String ntpServer = "time.google.com";

configTime(zone, ntpServer);

int gmtOffset_sec = ?
int daylightOffset_sec = ?

Thanks!

offset is based on your zone definition
STD offset [ DST [ dstoffset ] [ , rule ] ]

CET your timezone
-1 the offset to UTC
CEST the zone abbrevation to be used for DST
as the dstoffset is omitted, it is 1 h

if your current time is in DST, is in tm.tm_isdst

I am looking for a zone-independent solution.

as already mentioned, the offset is defined by the posix timezone.

if for what ever reason you insist to "calculate", take the UTC time in HHMM and substract the local time HHMM

That makes no sense. Local time offset from UTC is, by definition, time zone-dependent.

To get the offset time in C using the time.h library, you can use the struct tm and time() functions. Here's an example:
#include <stdio.h>
#include <time.h>

int main() {
time_t t = time(NULL);
struct tm *local_time = localtime(&t);
int offset = local_time->tm_gmtoff;
printf("Offset from UTC is %d seconds\n", offset);
return 0;
}

In this example, we first get the current time using time(NULL). We then use the localtime() function to convert this time to a struct tm representing the local time zone. Finally, we get the offset from UTC in seconds using the tm_gmtoff member of the struct tm, and print it to the console.

Note that the tm_gmtoff member may not be available on all platforms or may have different behavior, so you should check the documentation for your specific system.
i resolve same issue on my website Knowledgeforu.info

Of course, for that to work the local time zone must be set. But @blikkk wants a "zone-independent solution". As I pointed out, that's nonsensical. So perhaps he'll get back to us with what he really wants.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.