Browse Source

* disabled the use of samevalue() when comparing float variants, because
a) it's Delphi-incompatible
b) when a tdatetime property is obtained via GetPropValue(), then
it will be a varfloat rather than a vardate, and there full
precision certainly is required (mantis #15296)
- since DoVarCmpFloat() is now identical to DoVarCmpDate(), removed
DoVarCmpFloat

git-svn-id: trunk@14472 -

Jonas Maebe 15 years ago
parent
commit
2d2465aac4
3 changed files with 52 additions and 14 deletions
  1. 1 0
      .gitattributes
  2. 2 14
      rtl/inc/variants.pp
  3. 49 0
      tests/webtbs/tw15296.pp

+ 1 - 0
.gitattributes

@@ -10148,6 +10148,7 @@ tests/webtbs/tw15207.pp svneol=native#text/plain
 tests/webtbs/tw15274.pp svneol=native#text/plain
 tests/webtbs/tw15274.pp svneol=native#text/plain
 tests/webtbs/tw15293.pp svneol=native#text/plain
 tests/webtbs/tw15293.pp svneol=native#text/plain
 tests/webtbs/tw15293a.pp svneol=native#text/plain
 tests/webtbs/tw15293a.pp svneol=native#text/plain
+tests/webtbs/tw15296.pp svneol=native#text/plain
 tests/webtbs/tw15304.pp svneol=native#text/plain
 tests/webtbs/tw15304.pp svneol=native#text/plain
 tests/webtbs/tw1532.pp svneol=native#text/plain
 tests/webtbs/tw1532.pp svneol=native#text/plain
 tests/webtbs/tw15364.pp svneol=native#text/plain
 tests/webtbs/tw15364.pp svneol=native#text/plain

+ 2 - 14
rtl/inc/variants.pp

@@ -1127,19 +1127,7 @@ end;
 {$ifndef FPUNONE}
 {$ifndef FPUNONE}
 function DoVarCmpFloat(const Left, Right: Double; const OpCode: TVarOp): ShortInt;
 function DoVarCmpFloat(const Left, Right: Double; const OpCode: TVarOp): ShortInt;
 begin
 begin
-  if SameValue(Left, Right) then
-    Result := 0
-  else if (OpCode in [opCmpEq, opCmpNe]) or (Left < Right) then
-    Result := -1
-  else
-    Result := 1;
-end;
-
-
-function DoVarCmpDate(const Left, Right: TDateTime; const OpCode: TVarOp): ShortInt;
-begin
-  { dates have to match exactly, all bits encode time information }
-  if(Left = Right) then
+  if Left = Right then
     Result := 0
     Result := 0
   else if (OpCode in [opCmpEq, opCmpNe]) or (Left < Right) then
   else if (OpCode in [opCmpEq, opCmpNe]) or (Left < Right) then
     Result := -1
     Result := -1
@@ -1275,7 +1263,7 @@ begin
       else
       else
         Result := DoVarCmpWStr(vl, vr, OpCode);
         Result := DoVarCmpWStr(vl, vr, OpCode);
 {$ifndef FPUNONE}
 {$ifndef FPUNONE}
-    ctDate:     Result := DoVarCmpDate(VariantToDate(vl), VariantToDate(vr), OpCode);
+    ctDate:     Result := DoVarCmpFloat(VariantToDate(vl), VariantToDate(vr), OpCode);
     ctCurrency: Result := DoVarCmpCurr(VariantToCurrency(vl), VariantToCurrency(vr));
     ctCurrency: Result := DoVarCmpCurr(VariantToCurrency(vl), VariantToCurrency(vr));
 {$endif}
 {$endif}
     ctString:
     ctString:

+ 49 - 0
tests/webtbs/tw15296.pp

@@ -0,0 +1,49 @@
+program TestProject;
+
+{$mode objfpc}{$H+}
+
+uses
+  SysUtils, TypInfo;
+
+type
+  TTestObject = class(TObject)
+  private
+    FDate1: TDateTime;
+    FDate2: TDateTime;
+  published
+    property Date1: TDateTime read FDate1 write FDate1;
+    property Date2: TDateTime read FDate2 write FDate2;
+  end;
+
+var
+  VarDate1: Variant;
+  VarDate2: Variant;
+  TestObject: TTestObject;
+
+begin
+  TestObject := TTestObject.Create;
+  TestObject.Date1 := EncodeDate(1999, 02, 06) + EncodeTime(20, 0, 0, 0);
+  TestObject.Date2 := EncodeDate(1999, 02, 06) + EncodeTime(20, 0, 0, 1);
+
+  // works ok
+  //VarDate1 := TestObject.Date1;
+  //VarDate2 := TestObject.Date2;
+
+  // rounding occurs
+  // variants are interpreted as floats
+  // but this code works in delphi as expected (different when comparing)
+  // so don't know is this comparisson problem or values are rounded too much.
+  VarDate1 := GetPropValue(TestObject, 'Date1');
+  VarDate2 := GetPropValue(TestObject, 'Date2');
+
+  if VarDate1 = VarDate2 then
+    begin
+      WriteLn('Dates are equal.');
+      halt(1);
+    end
+  else
+    WriteLn('Dates are not equal.');
+
+  TestObject.Free;
+end.
+