Browse Source

* Fixed rounding errors in DateTimeToTimeStamp, causing errors in conversions

git-svn-id: trunk@23334 -
michael 12 years ago
parent
commit
2a9e12c709
1 changed files with 12 additions and 10 deletions
  1. 12 10
      rtl/objpas/sysutils/dati.inc

+ 12 - 10
rtl/objpas/sysutils/dati.inc

@@ -52,10 +52,15 @@ end;
 {   DateTimeToTimeStamp converts DateTime to a TTimeStamp   }
 {   DateTimeToTimeStamp converts DateTime to a TTimeStamp   }
 
 
 function DateTimeToTimeStamp(DateTime: TDateTime): TTimeStamp;
 function DateTimeToTimeStamp(DateTime: TDateTime): TTimeStamp;
+
+Var
+  D : Double;
+
 begin
 begin
-  result.Time := Round(abs(Frac(DateTime)) * MSecsPerDay);
-  result.Date := DateDelta + trunc(DateTime);
-end ;
+  D:=DateTime * Single(MSecsPerDay);
+  result.Time := Abs(Round(D)) Mod MSecsPerDay;
+  result.Date := DateDelta + Round(D) div MSecsPerDay;
+end;
 
 
 {   TimeStampToDateTime converts TimeStamp to a TDateTime value   }
 {   TimeStampToDateTime converts TimeStamp to a TDateTime value   }
 
 
@@ -190,7 +195,7 @@ procedure DecodeTime(Time: TDateTime; out Hour, Minute, Second, MilliSecond: wor
 Var
 Var
   l : cardinal;
   l : cardinal;
 begin
 begin
- l := Round(abs(Frac(time)) * MSecsPerDay);
+ l := DateTimeToTimeStamp(Time).Time;
  Hour   := l div 3600000;
  Hour   := l div 3600000;
  l := l mod 3600000;
  l := l mod 3600000;
  Minute := l div 60000;
  Minute := l div 60000;
@@ -894,12 +899,9 @@ var
     StoreStr(pchar(@S[1]), Length(S));
     StoreStr(pchar(@S[1]), Length(S));
   end ;
   end ;
 
 
-Const
-  msec2 = 1 / (24*60*60*1000*2); // Half a millisecond, for rounding.
-
 var
 var
   Year, Month, Day, DayOfWeek, Hour, Minute, Second, MilliSecond: word;
   Year, Month, Day, DayOfWeek, Hour, Minute, Second, MilliSecond: word;
-
+  DT : TDateTime;
 
 
   procedure StoreFormat(const FormatStr: string; Nesting: Integer; TimeFlag: Boolean);
   procedure StoreFormat(const FormatStr: string; Nesting: Integer; TimeFlag: Boolean);
   var
   var
@@ -1110,8 +1112,8 @@ begin
 {$ifdef MSWindows}
 {$ifdef MSWindows}
   eastasiainited:=false;
   eastasiainited:=false;
 {$endif MSWindows}
 {$endif MSWindows}
-  DecodeDateFully(DateTime+Msec2, Year, Month, Day, DayOfWeek);
-  DecodeTime(DateTime+Msec2, Hour, Minute, Second, MilliSecond);
+  DecodeDateFully(DateTime, Year, Month, Day, DayOfWeek);
+  DecodeTime(DateTime, Hour, Minute, Second, MilliSecond);
   ResultLen := 0;
   ResultLen := 0;
   ResultCurrent := @ResultBuffer[0];
   ResultCurrent := @ResultBuffer[0];
   if FormatStr <> '' then
   if FormatStr <> '' then