View Issue Details

IDProjectCategoryView StatusLast Update
0037361FPCRTLpublic2020-07-15 19:20
ReporterFr0sT Assigned ToMarco van de Voort  
PrioritynormalSeverityminorReproducibilityalways
Status resolvedResolutionfixed 
Platformx64OSWindows 
Product Version3.3.1 
Summary0037361: DaysBetween produces wrong results for dates before TDateTime(0)
DescriptionAttached test produces the following output:
20.12.1899 9
21.12.1899 8
22.12.1899 7
23.12.1899 6
24.12.1899 5
25.12.1899 4
26.12.1899 3
27.12.1899 2
28.12.1899 1 ===> bug!
29.12.1899 1
30.12.1899 0
31.12.1899 1

The same code under Delphi produces numbers from 10 to 1 (2 for 28.12.1899 and so forth).
Steps To Reproduceprocedure Test;
var
  dt: TDateTime;
  i: Integer;
begin
  for i := 0 to 11 do
  begin
    dt := EncodeDate(1899, 12, 20 + i);
    WriteLn(DateTimeToStr(dt) + ' ' + IntToStr(DaysBetween(dt, 0)));
  end;
end;
TagsNo tags attached.
Fixed in Revision45793
FPCOldBugId
FPCTarget-
Attached Files

Activities

Bart Broersma

2020-07-15 18:56

reporter   ~0124056

I can confirm this observation (tested against D7 PE).

Marco van de Voort

2020-07-15 19:20

manager   ~0124057

Now gives correct output.

Serge Anvarov

2020-07-15 19:20

reporter   ~0124058

From dateutils.inc:

[code]
Function DateTimeDiff(const ANow, AThen: TDateTime): TDateTime;
begin
  Result:= ANow - AThen;
  if (ANow>0) and (AThen<0) then
    Result:=Result-0.5
  else if (ANow<-1.0) and (AThen>-1.0) then
    Result:=Result+0.5;
end;
[/code]

If in last "if" change -1.0 to zero (as first if), then result will be as in Delphi.

More simple example:

[code]
uses SysUtils, DateUtils;
var
  D1, D2: TDateTime;
begin
  D1 := EncodeDate(1899, 12, 28);
  D2 := EncodeDate(1899, 12, 30); // or zero
  Writeln('Between ', D1:0:2, ' and ', D2:0:2, ' days: ', DaysBetween(D1, D2)); // Between -2.00 and 0.00 days: 1
end.
[/code]

Issue History

Date Modified Username Field Change
2020-07-15 14:14 Fr0sT New Issue
2020-07-15 18:56 Bart Broersma Note Added: 0124056
2020-07-15 19:20 Marco van de Voort Assigned To => Marco van de Voort
2020-07-15 19:20 Marco van de Voort Status new => resolved
2020-07-15 19:20 Marco van de Voort Resolution open => fixed
2020-07-15 19:20 Marco van de Voort Fixed in Revision => 45793
2020-07-15 19:20 Marco van de Voort FPCTarget => -
2020-07-15 19:20 Marco van de Voort Note Added: 0124057
2020-07-15 19:20 Serge Anvarov Note Added: 0124058