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
Solved

UTC time getting different result during daylight savings time

Posted on 2014-11-03
7
307 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
  • 3
  • 3
7 Comments
 
LVL 26

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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 26

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 26

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi - replicating a form 8 81
tidtcpserver connection lost handle 2 101
how to resize animated Gif image in delphi ? 1 34
CheckListBox usage 3 71
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

809 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