I am trying to get the local time in seconds (unix epoch) when a timezone is set. For e.g.
1643371200 is 2022-01-28 12:00:00 with UTC0
this can be read via gettimeofday(tv, NULL) as seconds with the timeval struct.
Now let's say I set my local timezone to Europe/Berlin via setenv("TZ","CET-1CEST,M3.5.0,M10.5.0/3",1).
When I use
localtime_r(&now, &local_tv);
char strftime_buf[64];
strftime(strftime_buf, sizeof(strftime_buf), "%c", &local_tv);
printf("get local time: %s", strftime_buf);
this prints 2022-01-28 13:00:00 as expected (+1h).
Now I used mktime(&local_tv) as I thought this will give me my local time in seconds. But it's still 1643371200 (12:00:00). And when I use mktime() with gettimeofday() I get 1 hour less, so 11:00:00 ...
2022-01-28T13:00:00+01:00 is 1643371200 seconds later than 1970-01-01T00:00:00+00:00, the epoch used on Linux.
mktimeis literally the inverse operation oflocaltime, so of course mktime(localtime(time)) gives time.You appear to want the number of seconds since 1970-01-01T00:00:00 local time.
If you want this, you are doing something wrong. There is no need for this value.[1]
To find the number of seconds between two times, start by converting them to epoch times if they aren't already. Then simply subtract one from the other.