Following function which I have written in Delphi (however, my question is not specific to Delphi) does output the current UTC unix timestamp:
function CurrentUnixTimeUTC: int64;
var
tzi: TTimeZoneInformation;
begin
// Get the current unix timestamp in local time.
Result := DateTimeToUnix(Now);
// First, add the DST specific bias
case GetTimeZoneInformation(tzi) of
TIME_ZONE_ID_INVALID:
RaiseLastOSError;
TIME_ZONE_ID_UNKNOWN:
; // Unknown what to do. We simply don't apply any bias.
TIME_ZONE_ID_STANDARD:
Result := Result + tzi.StandardBias * 60;
TIME_ZONE_ID_DAYLIGHT:
Result := Result + tzi.DaylightBias * 60;
end;
// Now apply the region specific bias
Result := Result + tzi.Bias * 60;
end;
The function works and has the same output in Germany as in California.
While researching MSDN, I have additionally found the function GetDynamicTimeZoneInformation
.
From reading the MSDN (which has an incomplete definition of the member "Bias" btw), it is not clear to me if simply calling GetTimeZoneInformation
is sufficient to have my function CurrentUnixTimeUTC
also work in regions which have dynamic DST settings (i.e. the DST dates change from year to year). In other words, can GetTimeZoneInformation
sucessfully tell if the system is currently in DST mode or not, or do I need to call GetDynamicTimeZoneInformation
instead, if I want to be compatible with computers which are in a time zone where dynamic DST rules are applied?