Mega Code Archive

 
Categories / Delphi / Examples
 

Difference between time zones

To calculate the difference between two timezones e.g. between your computer's local time and Greenwich Mean Time (GMT) you can use GetTimeZoneInformation. There are two ways to convert a TDateTime value between local time and GMT: GetTimeZoneInformation and LocalFileTimeToFileTime. The code below bases on GetTimeZoneInformation. unit TimeZoneConversion; interface uses Windows, SysUtils; function LocaleDateTimeToGMTDateTime(const Value: TDateTime) : TDateTime; function GMTDateTimeToLocaleDateTime(const Value: TDateTime) : TDateTime; implementation const MinsPerDay = 24*60; function GetGMTBias : Integer; var info: TTimeZoneInformation; Mode: DWord; begin Mode := GetTimeZoneInformation(info); Result := info.Bias; case Mode of TIME_ZONE_ID_INVALID: begin RaiseLastWin32Error end; TIME_ZONE_ID_STANDARD: begin Result := Result+info.StandardBias end; TIME_ZONE_ID_DAYLIGHT: begin Result := Result+info.DaylightBias end; end; end; function LocaleDateTimeToGMTDateTime(const Value: TDateTime) : TDateTime; begin Result := Value+(GetGMTBias/MinsPerDay); end; function GMTDateTimeToLocaleDateTime(const Value: TDateTime) : TDateTime; begin Result := Value-(GetGMTBias/MinsPerDay); end; end.