<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="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">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>