Here is my way:
time_t z = 0;
struct tm * pdt = gmtime(&z);
time_t tzlag = mktime(pdt);
Alternative with automatic, local storage of struct tm
:
struct tm dt;
memset(&dt, 0, sizeof(struct tm));
dt.tm_mday=1; dt.tm_year=70;
time_t tzlag = mktime(&dt);
tzlag
, in seconds, will be the negative of the UTC offset; lag of your timezone Standard Time compared to UTC:
LocalST + tzlag = UTC
If you want to also account for "Daylight savings", subtract tm_isdst
from tzlag
, where tm_isdst
is for a particular local time struct tm
, after applying mktime
to it (or after obtaining it with localtime
).
Why it works:
The set struct tm
is for "epoch" moment, Jan 1 1970, which corresponds to a time_t
of 0.
Calling mktime()
on that date converts it to time_t
as if it were UTC (thus getting 0), then subtracts the UTC offset from it in order to produce the output time_t
. Thus it produces negative of UTC_offset.
tm
has agmtoff
field" --> that is a non-standard C library extension. – Kobi