<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=us-ascii">
<META content="MSHTML 6.00.6000.16608" name=GENERATOR></HEAD>
<BODY>
<DIV dir=ltr align=left><SPAN class=656374308-06042009><FONT face=Arial
color=#0000ff size=2>Ok, well finally worked it out. I was almost close,
but your code helped out :-)</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=656374308-06042009><FONT face=Arial
color=#0000ff size=2></FONT></SPAN> </DIV>
<DIV dir=ltr align=left><SPAN class=656374308-06042009><FONT face=Arial
color=#0000ff size=2>Thanks again.</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=656374308-06042009></SPAN> </DIV>
<DIV dir=ltr align=left><SPAN class=656374308-06042009><FONT face=Arial
color=#0000ff size=2>Jeremy</FONT></SPAN></DIV><BR>
<DIV class=OutlookMessageHeader lang=en-us dir=ltr align=left>
<HR tabIndex=-1>
<FONT face=Tahoma size=2><B>From:</B> delphi-bounces@delphi.org.nz
[mailto:delphi-bounces@delphi.org.nz] <B>On Behalf Of </B>Kyley
Harris<BR><B>Sent:</B> 6 April 2009 14:51<BR><B>To:</B> NZ Borland Developers
Group - Delphi List<BR><B>Subject:</B> Re: [DUG] timezone
help<BR></FONT><BR></DIV>
<DIV></DIV>
<DIV>unit uDateTimeGMTConversion;</DIV>
<DIV><BR></DIV>
<DIV>interface</DIV>
<DIV><BR></DIV>
<DIV>uses</DIV>
<DIV> Sysutils,</DIV>
<DIV> classes, Windows, uHssShareCommon, uItem;</DIV>
<DIV><BR></DIV>
<DIV>type</DIV>
<DIV><BR></DIV>
<DIV><BR></DIV>
<DIV> TDateTimeGMTConversion = class(TObject)</DIV>
<DIV> private</DIV>
<DIV> FZoneType:DWord;</DIV>
<DIV> FTimeZoneInfo: TTimeZoneInformation;</DIV>
<DIV> FForceTimeZone: boolean;</DIV>
<DIV> FForcedTimeZoneInfo: TTimeZoneInformation;</DIV>
<DIV><BR></DIV>
<DIV> function GetTimeZoneInfo:TTimeZoneInformation;</DIV>
<DIV> public</DIV>
<DIV> constructor create;</DIV>
<DIV><BR></DIV>
<DIV> class procedure
ForceLocalTimeZoneIgnoreGMT(AValue:boolean);</DIV>
<DIV> function DateIsDaylight(ADate:TDateTime):boolean;</DIV>
<DIV> function
LocalMachineTimeToGMT(ALocalMachineTime:TDateTime):TDateTime;virtual;</DIV>
<DIV> function
GMTToLocalMachineTime(AGMTTime:TDateTime):TDateTime;virtual;</DIV>
<DIV><BR></DIV>
<DIV> procedure
SetTimeZone(Zone:THssTimeZoneInformation);</DIV>
<DIV><BR></DIV>
<DIV> property TimeZoneInfo:TTimeZoneInformation read
FTimeZoneInfo;</DIV>
<DIV><BR></DIV>
<DIV> property ForceTimeZone:boolean read FForceTimeZone write
FForceTimeZone;</DIV>
<DIV> property ForcedTimeZoneInfo:TTimeZoneInformation read
FForcedTimeZoneInfo write FForcedTimeZoneInfo;</DIV>
<DIV><BR></DIV>
<DIV><BR></DIV>
<DIV><BR></DIV>
<DIV> end;</DIV>
<DIV><BR></DIV>
<DIV> TTimeZones = class(THssTimeZoneInformations)</DIV>
<DIV> public</DIV>
<DIV> function
GetBIAS(sender:TObject;AItem:TItem;AFieldName:string;AFieldValue:Variant):variant;</DIV>
<DIV> end;</DIV>
<DIV><BR></DIV>
<DIV> TTimeZoneData = class(THssTimeZoneInformation)</DIV>
<DIV> public</DIV>
<DIV> TimeZoneInformation:TTimeZoneInformation;</DIV>
<DIV> end;</DIV>
<DIV><BR></DIV>
<DIV>var</DIV>
<DIV> GMTEngine:TDateTimeGMTConversion;</DIV>
<DIV> TimeZones:THssTimeZoneInformations;</DIV>
<DIV><BR></DIV>
<DIV><BR></DIV>
<DIV>var</DIV>
<DIV> ZeroDateThreshold:TDateTime;</DIV>
<DIV>implementation</DIV>
<DIV><BR></DIV>
<DIV>uses DateUtils , Registry;</DIV>
<DIV><BR></DIV>
<DIV>var</DIV>
<DIV> AForceLocalTimeOnly:boolean=false;</DIV>
<DIV><BR></DIV>
<DIV>{ TDateTimeGMTConversion }</DIV>
<DIV><BR></DIV>
<DIV>constructor TDateTimeGMTConversion.create;</DIV>
<DIV>begin</DIV>
<DIV> inherited;</DIV>
<DIV> FZoneType := GetTimeZoneInformation(FTimeZoneInfo);</DIV>
<DIV>end;</DIV>
<DIV><BR></DIV>
<DIV>function TDateTimeGMTConversion.DateIsDaylight(ADate: TDateTime):
boolean;</DIV>
<DIV>var</DIV>
<DIV> y,m,d,h,n,s,z:word;</DIV>
<DIV> TZI:TTimeZoneInformation;</DIV>
<DIV>begin</DIV>
<DIV> result := false;</DIV>
<DIV> TZI := GetTimeZoneInfo;</DIV>
<DIV><BR></DIV>
<DIV> if (TZI.StandardDate.wMonth = 0) or (TZI.DaylightDate.wMonth =
0) then</DIV>
<DIV> exit;</DIV>
<DIV><BR></DIV>
<DIV> DecodeDateTime(ADate,y,m,d,h,n,s,z);</DIV>
<DIV> if TZI.StandardDate.wMonth < TZI.DaylightDate.wMonth
then</DIV>
<DIV> begin</DIV>
<DIV> result := not (</DIV>
<DIV> (EncodeDateTime(y,m,d,h,m,s,z) >=</DIV>
<DIV>
EncodeDateTime(y,TZI.StandardDate.wMonth,TZI.StandardDate.wDay,TZI.StandardDate.wHour,0,0,0)
) and</DIV>
<DIV><BR></DIV>
<DIV> (EncodeDateTime(y,m,d,h,m,s,z) <=</DIV>
<DIV>
EncodeDateTime(y,TZI.DaylightDate.wMonth,TZI.DaylightDate.wDay,TZI.DaylightDate.wHour,0,0,0)
)</DIV>
<DIV> );</DIV>
<DIV> end else</DIV>
<DIV> begin</DIV>
<DIV> result := (</DIV>
<DIV> (EncodeDateTime(y,m,d,h,m,s,z) >=</DIV>
<DIV>
EncodeDateTime(y,TZI.DaylightDate.wMonth,TZI.DaylightDate.wDay,TZI.DaylightDate.wHour,0,0,0)
) and</DIV>
<DIV><BR></DIV>
<DIV> (EncodeDateTime(y,m,d,h,m,s,z) <=</DIV>
<DIV>
EncodeDateTime(y,TZI.StandardDate.wMonth,TZI.StandardDate.wDay,TZI.StandardDate.wHour,0,0,0)
)</DIV>
<DIV> );</DIV>
<DIV> end;</DIV>
<DIV><BR></DIV>
<DIV> // if TZI.StandardDate.wMonth</DIV>
<DIV><BR></DIV>
<DIV>end;</DIV>
<DIV><BR></DIV>
<DIV>procedure TDateTimeGMTConversion.SetTimeZone(</DIV>
<DIV> Zone: THssTimeZoneInformation);</DIV>
<DIV>begin</DIV>
<DIV> if Zone = nil then</DIV>
<DIV> begin</DIV>
<DIV> ForceTimeZone := false;</DIV>
<DIV><BR></DIV>
<DIV><BR></DIV>
<DIV> end else</DIV>
<DIV> begin</DIV>
<DIV> Assert(Zone is TTimeZoneData);</DIV>
<DIV> ForceTimeZone := True;</DIV>
<DIV> FForcedTimeZoneInfo := (Zone as
TTimeZoneData).TimeZoneInformation;</DIV>
<DIV> end;</DIV>
<DIV>end;</DIV>
<DIV><BR></DIV>
<DIV>function TDateTimeGMTConversion.GetTimeZoneInfo:
TTimeZoneInformation;</DIV>
<DIV>begin</DIV>
<DIV> if FForceTimeZone then</DIV>
<DIV> result := FForcedTimeZoneInfo else</DIV>
<DIV> result := FTimeZoneInfo;</DIV>
<DIV>end;</DIV>
<DIV><BR></DIV>
<DIV>function TDateTimeGMTConversion.GMTToLocalMachineTime(</DIV>
<DIV> AGMTTime: TDateTime): TDateTime;</DIV>
<DIV>begin</DIV>
<DIV> if AGMTTime < ZeroDateThreshold then</DIV>
<DIV> begin</DIV>
<DIV> result := 0.0;</DIV>
<DIV> end else</DIV>
<DIV> begin</DIV>
<DIV> if AForceLocalTimeOnly and (not FForceTimeZone)
then</DIV>
<DIV> begin</DIV>
<DIV> result := RecodeMilliSecond(AGMTTime,0);</DIV>
<DIV> exit;</DIV>
<DIV> end;</DIV>
<DIV><BR></DIV>
<DIV> result := IncMinute(AGMTTime,GetTimeZoneInfo.Bias *
-1);</DIV>
<DIV> if DateIsDaylight(result) then</DIV>
<DIV> result :=
IncMinute(result,GetTimeZoneInfo.DaylightBias * -1);</DIV>
<DIV><BR></DIV>
<DIV> result := RecodeMilliSecond(result,0);</DIV>
<DIV><BR></DIV>
<DIV> end;</DIV>
<DIV>end;</DIV>
<DIV><BR></DIV>
<DIV>function TDateTimeGMTConversion.LocalMachineTimeToGMT(</DIV>
<DIV> ALocalMachineTime: TDateTime): TDateTime;</DIV>
<DIV>begin</DIV>
<DIV> if ALocalMachineTime <= ZeroDateThreshold then</DIV>
<DIV> begin</DIV>
<DIV> result := 0.0;</DIV>
<DIV> end else</DIV>
<DIV> begin</DIV>
<DIV> if AForceLocalTimeOnly and (not FForceTimeZone)
then</DIV>
<DIV> begin</DIV>
<DIV> result :=
RecodeMilliSecond(ALocalMachineTime,0);</DIV>
<DIV> exit;</DIV>
<DIV> end;</DIV>
<DIV><BR></DIV>
<DIV> result :=
IncMinute(ALocalMachineTime,GetTimeZoneInfo.Bias);</DIV>
<DIV> if DateIsDaylight(ALocalMachineTime) then</DIV>
<DIV> result :=
IncMinute(result,GetTimeZoneInfo.DaylightBias);</DIV>
<DIV><BR></DIV>
<DIV> result := RecodeMilliSecond(result,0);</DIV>
<DIV><BR></DIV>
<DIV> end;</DIV>
<DIV>end;</DIV>
<DIV><BR></DIV>
<DIV><BR></DIV>
<DIV>type</DIV>
<DIV> REGTzInfo = record</DIV>
<DIV> Bias: Longint;</DIV>
<DIV> StandardBias: Longint;</DIV>
<DIV> DaylightBias: Longint;</DIV>
<DIV> StandardDate: TSystemTime;</DIV>
<DIV> DaylightDate: TSystemTime;</DIV>
<DIV> end;</DIV>
<DIV><BR></DIV>
<DIV>var</DIV>
<DIV> Keys:TStringList;</DIV>
<DIV> i:integer;</DIV>
<DIV> TZ:TTimeZoneData;</DIV>
<DIV> rTZ:REGTzInfo;</DIV>
<DIV>class procedure TDateTimeGMTConversion.ForceLocalTimeZoneIgnoreGMT(</DIV>
<DIV> AValue: boolean);</DIV>
<DIV>begin</DIV>
<DIV> AForceLocalTimeOnly := AValue;</DIV>
<DIV>end;</DIV>
<DIV><BR></DIV>
<DIV>{ TTimeZones }</DIV>
<DIV><BR></DIV>
<DIV>function TTimeZones.GetBIAS(sender: TObject; AItem: TItem;</DIV>
<DIV> AFieldName: string; AFieldValue: Variant): variant;</DIV>
<DIV>var</DIV>
<DIV> z:TTimeZoneData absolute AITem;</DIV>
<DIV>begin</DIV>
<DIV> result := z.TimeZoneInformation.Bias * -1;</DIV>
<DIV>end;</DIV>
<DIV><BR></DIV>
<DIV>initialization</DIV>
<DIV> GMTEngine := TDateTimeGMTConversion.create;</DIV>
<DIV><BR></DIV>
<DIV> TimeZones := TTimeZones.Create(nil);</DIV>
<DIV><BR></DIV>
<DIV> TimeZones.BeginUpdate;</DIV>
<DIV> with TRegistry.Create(KEY_READ) do</DIV>
<DIV> try</DIV>
<DIV> RootKey := HKEY_LOCAL_MACHINE;</DIV>
<DIV> if OpenKey('SOFTWARE\Microsoft\Windows
NT\CurrentVersion\Time Zones',false) then</DIV>
<DIV> begin</DIV>
<DIV> Keys := TStringList.Create();</DIV>
<DIV> try</DIV>
<DIV> GetKeyNames(Keys);</DIV>
<DIV> CloseKey;</DIV>
<DIV> for i := 0 to Keys.Count -1 do</DIV>
<DIV> begin</DIV>
<DIV> if
OpenKey('SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time
Zones\'+Keys[i],false) then</DIV>
<DIV> begin</DIV>
<DIV> TZ :=
TimeZones.Add(TTimeZoneData) as TTimeZoneData;</DIV>
<DIV> with TZ do</DIV>
<DIV> begin</DIV>
<DIV> Description :=
ReadString('Std');</DIV>
<DIV> DisplayData :=
ReadString('Display');</DIV>
<DIV>
ReadBinaryData('TZI',rTZ,SizeOf(RegTZInfo)) ;</DIV>
<DIV><BR></DIV>
<DIV>
TimeZoneInformation.Bias := rtZ.Bias;</DIV>
<DIV>
TimeZoneInformation.StandardDate := rtZ.StandardDate;</DIV>
<DIV>
TimeZoneInformation.StandardBias := rtZ.StandardBias;</DIV>
<DIV>
TimeZoneInformation.DaylightDate := rtZ.DaylightDate;</DIV>
<DIV>
TimeZoneInformation.DaylightBias := rtZ.DaylightBias;</DIV>
<DIV> end;</DIV>
<DIV> CloseKey;</DIV>
<DIV> end;</DIV>
<DIV><BR></DIV>
<DIV> end;</DIV>
<DIV><BR></DIV>
<DIV> finally</DIV>
<DIV> FreeAndNil(Keys);</DIV>
<DIV> end;</DIV>
<DIV><BR></DIV>
<DIV><BR></DIV>
<DIV><BR></DIV>
<DIV> end;</DIV>
<DIV><BR></DIV>
<DIV> finally</DIV>
<DIV> Free;</DIV>
<DIV> end;</DIV>
<DIV><BR></DIV>
<DIV> TimeZones.EndUpdate;</DIV>
<DIV> TimeZones.NewIndex('BIAS','GETBIAS',TTimeZones
(TimeZones).GetBias);</DIV>
<DIV><BR></DIV>
<DIV><BR></DIV>
<DIV> ZeroDateThreshold := EncodeDate(1901,01,01);</DIV>
<DIV>finalization</DIV>
<DIV> FreeAndNil(GMTEngine);</DIV>
<DIV> FreeAndNil(TimeZones);</DIV>
<DIV>end.</DIV>
<DIV><BR></DIV><BR>
<DIV class=gmail_quote>On Mon, Apr 6, 2009 at 2:44 PM, Jeremy Coulter <SPAN
dir=ltr><<A
href="mailto:jscoulter@gmail.com">jscoulter@gmail.com</A>></SPAN> wrote:<BR>
<BLOCKQUOTE class=gmail_quote
style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid">Hi
all.<BR>I want to write something that will take a time in a different
timezone and convert it to another time zone.<BR><BR>i.e. If its say
11pm Sunday in say New York what time is that here in NZ? I thought of NY
since it can be "yesterday" their time.<BR><BR>I have some code that tells me
what the GMT offset is for NY, but how do I calculate the time to NZ
time?<BR><FONT
color=#888888><BR><BR>Jeremy<BR><BR></FONT><BR>_______________________________________________<BR>NZ
Borland Developers Group - Delphi mailing list<BR>Post: <A
href="mailto:delphi@delphi.org.nz">delphi@delphi.org.nz</A><BR>Admin: <A
href="http://delphi.org.nz/mailman/listinfo/delphi"
target=_blank>http://delphi.org.nz/mailman/listinfo/delphi</A><BR>Unsubscribe:
send an email to <A
href="mailto:delphi-request@delphi.org.nz">delphi-request@delphi.org.nz</A>
with Subject: unsubscribe<BR></BLOCKQUOTE></DIV><BR><BR clear=all><BR>--
<BR>Kyley Harris<BR>Harris Software<BR>+64-21-671-821<BR></BODY></HTML>