DelphiFAQ Home Search:

Difference between time zones

 

comments1 comments. Current rating: 4 stars (1 votes). Leave comments and/ or rate it.

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.
You don't like the formatting? Check out SourceCoder then!

Comments:

2007-01-15, 04:30:09
anonymous from Poland  
rating

 

 

Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option.
 
It seems that you are
from Los Angeles, US .

Info/ Feedback on this

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:
photo Add a picture:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity. The owner of this web site reserves the right to delete such material.