Solved

UTC time getting different result during daylight savings time

Posted on 2014-11-03
7
321 Views
Last Modified: 2014-11-10
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

0
Comment
Question by:EricTaylor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 27

Expert Comment

by:Sinisa Vuk
ID: 40420986
0
 
LVL 19

Expert Comment

by:Thommy
ID: 40421012
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?
0
 

Author Comment

by:EricTaylor
ID: 40429245
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

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Expert Comment

by:Sinisa Vuk
ID: 40429271
add declaration for win api function:

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

Open in new window

0
 

Author Comment

by:EricTaylor
ID: 40429337
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
0
 
LVL 27

Accepted Solution

by:
Sinisa Vuk earned 500 total points
ID: 40432149
this is my project if it helps:
unit Unit30;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

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


implementation

{$R *.dfm}

function UTCToLocalTime(AValue: TDateTime): TDateTime;
var
  LocalSystemTime, UTCSystemTime: TSystemTime;
  TZInfo: TTimeZoneInformation;
begin
  GetTimeZoneInformation(TZInfo);
  DateTimeToSystemTime(AValue, UTCSystemTime);
  SystemTimeToTzSpecificLocalTime(@TZInfo, UTCSystemTime, LocalSystemTime);
  Result := SystemTimeToDateTime(LocalSystemTime);
end;

function LocalTimeToUTC(dtLocal: TDateTime): TDateTime;
var
  LocalSystemTime, UTCSystemTime: TSystemTime;
  TZInfo: TTimeZoneInformation;
begin
  GetTimeZoneInformation(TZInfo);
  DateTimeToSystemTime(dtLocal, LocalSystemTime);
  TzSpecificLocalTimeToSystemTime(@TZInfo, LocalSystemTime, UTCSystemTime);

  Result := SystemTimeToDateTime(UTCSystemTime);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Label1.Caption := DateTimeToStr(LocalTimeToUTC(now));
end;

end.

Open in new window

0
 

Author Closing Comment

by:EricTaylor
ID: 40433782
Thanks Sinisa. That appears to do the trick.

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to renew a Delphi rad-studio licence? 5 77
SUM 2 INTEGER ARRAYS INTO 1 10 111
Installshield for Embarcadero EX 10.1 Berlin 4 72
Delphi: sending SMS on android platform 1 65
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question