Browse Source

* check for frac(dt) being slightly lower than a whole number. Patch by Werner P
Resolves #40140

marcoonthegit 2 years ago
parent
commit
8d0b822a27
1 changed files with 14 additions and 4 deletions
  1. 14 4
      rtl/objpas/sysutils/dati.inc

+ 14 - 4
rtl/objpas/sysutils/dati.inc

@@ -1005,6 +1005,14 @@ var
 var
 var
   Year, Month, Day, DayOfWeek, Hour, Minute, Second, MilliSecond: word;
   Year, Month, Day, DayOfWeek, Hour, Minute, Second, MilliSecond: word;
 
 
+  function FullDays(ADateTime: TDateTime): Integer;
+  begin
+    if ADateTime < 0 then ADateTime := -ADateTime;
+    Result := trunc(ADateTime);
+    if (frac(ADateTime) > 0.9) and (Hour = 0) and (Minute = 0) and (Second = 0) and (Millisecond = 0) then
+      inc(Result);
+  end;
+
   procedure StoreFormat(const FormatStr: string; Nesting: Integer; TimeFlag: Boolean);
   procedure StoreFormat(const FormatStr: string; Nesting: Integer; TimeFlag: Boolean);
   var
   var
     Token, lastformattoken, prevlasttoken: char;
     Token, lastformattoken, prevlasttoken: char;
@@ -1110,7 +1118,7 @@ var
             end;
             end;
             'M': begin
             'M': begin
               if isInterval and ((prevlasttoken = 'H') or TimeFlag) then
               if isInterval and ((prevlasttoken = 'H') or TimeFlag) then
-                StoreInt(Minute + (Hour + trunc(abs(DateTime))*24)*60, 0)
+                StoreInt(Minute + (Hour + FullDays(DateTime)*24)*60, 0)
               else
               else
               if (lastformattoken = 'H') or TimeFlag then
               if (lastformattoken = 'H') or TimeFlag then
               begin
               begin
@@ -1143,7 +1151,7 @@ var
             end ;
             end ;
             'H':
             'H':
               if isInterval then
               if isInterval then
-                StoreInt(Hour + trunc(abs(DateTime))*24, Count)
+                StoreInt(Hour + FullDays(DateTime)*24, Count)
               else
               else
               if Clock12 then
               if Clock12 then
               begin
               begin
@@ -1161,14 +1169,14 @@ var
                   StoreInt(Hour, 2);
                   StoreInt(Hour, 2);
               end;
               end;
             'N': if isInterval then
             'N': if isInterval then
-                   StoreInt(Minute + (Hour + trunc(abs(DateTime))*24)*60, Count)
+                   StoreInt(Minute + (Hour + FullDays(DateTime)*24)*60, Count)
                  else
                  else
                  if Count = 1 then
                  if Count = 1 then
                    StoreInt(Minute, 0)
                    StoreInt(Minute, 0)
                  else
                  else
                    StoreInt(Minute, 2);
                    StoreInt(Minute, 2);
             'S': if isInterval then
             'S': if isInterval then
-                   StoreInt(Second + (Minute + (Hour + trunc(abs(DateTime))*24)*60)*60, Count)
+                   StoreInt(Second + (Minute + (Hour + FullDays(DateTime)*24)*60)*60, Count)
                  else
                  else
                  if Count = 1 then
                  if Count = 1 then
                    StoreInt(Second, 0)
                    StoreInt(Second, 0)
@@ -1250,6 +1258,8 @@ begin
     StoreFormat('C', 0, False);
     StoreFormat('C', 0, False);
   ResultBuffer[ResultLen] := #0;
   ResultBuffer[ResultLen] := #0;
   result := StrPas(@ResultBuffer[0]);
   result := StrPas(@ResultBuffer[0]);
+  if (DateTime < 0) and (fdoInterval in Options) then
+    result := '-' + result;
 end ;
 end ;