Unit TimeConversion; { Internally we always use TDateTime for dates and times. This is always in the local time zone, except a few specially noted cases. } Interface Function DateTimeToJava(Local : TDateTime) : Int64; Function JavaToDateTime(J : Int64) : TDateTime; Function DateTimeToC(Local : TDateTime) : Integer; Function CToDateTime(C : Integer) : TDateTime; Function FromUTCToLocal(UTC : TDateTime) : TDateTime; { This is what the Now function would return if this computer was in the GMT time zone. } Function GmtNow : TDateTime; Implementation Uses Windows, DateUtils, SysUtils; Var Bias : Integer; // UTC = local time + bias // Unit is minutes Function FromUTCToLocal(UTC : TDateTime) : TDateTime; Begin Result := IncMinute(UTC, -Bias) End; Function DateTimeToJava(Local : TDateTime) : Int64; Begin Result := DateTimeToUnix(IncMinute(Local, Bias)) * 1000 + MilliSecondOfTheSecond(Local) End; Function JavaToDateTime(J : Int64) : TDateTime; Begin Result := IncMilliSecond(FromUTCToLocal(UnixToDateTime(J Div 1000)), J Mod 1000) End; Function DateTimeToC(Local : TDateTime) : Integer; Var Temp : Int64; Begin Temp := DateTimeToUnix(IncMinute(Local, Bias)); Result := Temp; Assert(Result = Temp) End; Function CToDateTime(C : Integer) : TDateTime; Begin If C = 0 Then Result := 0 Else Result := FromUTCToLocal(UnixToDateTime(C)) End; Function GmtNow : TDateTime; Begin Result := IncMinute(Now, Bias) End; Procedure InitBias; Var Info : TIME_ZONE_INFORMATION; MoreInfo : Integer; Begin MoreInfo := GetTimeZoneInformation(Info); Case MoreInfo Of TIME_ZONE_ID_STANDARD : Bias := Info.Bias + Info.StandardBias; TIME_ZONE_ID_DAYLIGHT : Bias := Info.Bias + Info.DaylightBias; Else Bias := 0 End End; Initialization InitBias; end.