Solved

UTC time getting different result during daylight savings time

Posted on 2014-11-03
7
331 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 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

691 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