Link to home
Start Free TrialLog in
Avatar of EricTaylor
EricTaylor

asked on

UTC time getting different result during daylight savings time

I have written two functions to convert to and from UTC time (see below). The problem is that events created when we were in daylight savings time have shifted by an hour now that we are not in daylight time. e.g. an event was saved on Friday for 8:00 on Monday is now showing at 7:00, presumably because the offset is different. (I'm presuming I'll have the opposite problem when events saved during a non-daylight time are shown during daylight time.) I'm puzzled as to why this isn't handling automatically since EST should have a different offset from UTC than EDT. Appreciate you help in understanding what's going on here so my "fix" doesn't break something else in the spring.

Did the functions that I wrote do something incorrect, or am I supposed to know to add/subtract an hour when in/not in daylight time.  Also, if I have to figure out if I'm in daylight time or not, is there a standard Delphi (2010) call to the system to figure this out.

Here's the code I'm currently using:
function UTCToLocal(var dtUTC: TDateTime): TDateTime;
var
  LocalSystemTime, UTCSystemTime: TSystemTime;
  LocalFileTime, UTCFileTime: TFileTime;
begin
  DateTimeToSystemTime(dtUTC, UTCSystemTime);
  SystemTimeToFileTime(UTCSystemTime, UTCFileTime);
  if FileTimeToLocalFileTime(UTCFileTime, LocalFileTime)
  and FileTimeToSystemTime(LocalFileTime, LocalSystemTime) then begin
    Result := SystemTimeToDateTime(LocalSystemTime);
  end
  else begin
    Result := dtUTC;  // Use time originally passed if conversion fails.
  end;
end;

function LocalToUTC(var dtLocal: TDateTime): TDateTime;
var
  LocalSystemTime, UTCSystemTime: TSystemTime;
  LocalFileTime, UTCFileTime: TFileTime;
begin
  DateTimeToSystemTime(dtLocal, LocalSystemTime);
  SystemTimeToFileTime(LocalSystemTime, LocalFileTime);
  if LocalFileTimeToFileTime(LocalFileTime, UTCFileTime)
  and FileTimeToSystemTime(UTCFileTime, UTCSystemTime) then begin
    Result := SystemTimeToDateTime(UTCSystemTime);
  end
  else begin
    Result := dtLocal; // Use time originally passed if conversion fails.
  end;
end;

Open in new window

Avatar of Sinisa Vuk
Sinisa Vuk
Flag of Croatia image

Your conversion treats daylight saving time as it is now and not as it was at the time being converted!!!

You should use System.DateUtils.TTimeZone to account for daylight saving times...
How to convert Local time to UTC time in Delphi XE2? and how to convert it back from UTC to local time?
Avatar of EricTaylor
EricTaylor

ASKER

Sinisa, the info you posted seems helpful, but when I try to do this,TzSpecificLocalTimeToSystemTime is an undeclared identifier. What am I missing?  Thanks.

function LocalToUTC(var dtLocal: TDateTime): TDateTime;
var
  LocalSystemTime, UTCSystemTime: TSystemTime;
  TZInfo: TTimeZoneInformation;
begin
  GetTimeZoneInformation(TZInfo);
  DateTimeToSystemTime(dtLocal, LocalSystemTime);
  TzSpecificLocalTimeToSystemTime (@TZInfo, LocalSystemTime, UTCSystemTime);
  Result := SystemTimeToDateTime(UTCSystemTime);
end;

Open in new window

add declaration for win api function:

function TzSpecificLocalTimeToSystemTime(lpTimeZoneInformation: PTimeZoneInformation;
  var lpLocalTime, lpUniversalTime: TSystemTime): BOOL; stdcall; external kernel32 name 'TzSpecificLocalTimeToSystemTime';

Open in new window

The Win api is not something I've generally had to deal with; could you point me to something that will give guidance on how to use it for something like this. Sorry for the ignorance... but that's where I'm at on this one.  Thanks
ASKER CERTIFIED SOLUTION
Avatar of Sinisa Vuk
Sinisa Vuk
Flag of Croatia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Sinisa. That appears to do the trick.

Thommy, thanks to you for your input as well...