Τετάρτη 8 Μαΐου 2013

Convering UTC to Local Time and Vice Versa

If you want to convert a dateTime stored in UTC time to Local Time and vice versa you can use this 2 functions
function UTCToLocalTime(AValue: TDateTime): TDateTime;
function LocalTimeToUTC(AValue: TDateTime): TDateTime;


function UTCToLocalTime(AValue: TDateTime): TDateTime;
var
  SysTime1, SysTime2: TSystemTime;
  TZinfo: TTimeZoneInformation;
begin
  GetTimeZoneInformation(TZinfo); 
  DateTimeToSystemTime(AValue, SysTime1); 
  SystemTimeToTzSpecificLocalTime(@TZinfo, SysTime1, SysTime2); 
  Result := SystemTimeToDateTime(SysTime2);
end;
function LocalTimeToUTC(AValue: TDateTime): TDateTime;
var
  SysTime1, SysTime2: TSystemTime;
  TZinfo: TTimeZoneInformation;
begin
  GetTimeZoneInformation(TZinfo);  
  TZinfo.Bias := -TZinfo.Bias;
  TZinfo.StandardBias := -TZinfo.StandardBias;
  TZinfo.DaylightBias := -TZinfo.DaylightBias;
  DateTimeToSystemTime(AValue, SysTime1); 
  SystemTimeToTzSpecificLocalTime(@TZinfo, SysTime1, SysTime2);
   Result := SystemTimeToDateTime(SysTime2);
end;

Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου