Recent

Author Topic: ¿EncodeDateTime or TDateTime problem?  (Read 5025 times)

silvestre

  • New Member
  • *
  • Posts: 45
¿EncodeDateTime or TDateTime problem?
« on: May 22, 2012, 11:42:07 pm »
Hello Friends,

Is normal case C? In the first 2 cases compare a date less than another and the result is correct. In case C do the same, but gives the opposite result ... %)
thanks!


Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var
 a,b:TDateTime;
begin

 // Uses DateUtils
 // CASE A
 a := EncodeDateTime (1999,3,5,22,1,1,1);
 b := EncodeDateTime (1999,3,5,23,1,1,1);

 if a < b then ShowMessage (DateTimeToSTr (a) + ' is less than '+DateTimeToSTr (b));
 if a > b then ShowMessage (DateTimeToSTr (a) + ' is greater than '+DateTimeToSTr (b));
 // a is an hour less than b, so expect a result a <b, and so is.

 // CASE B
 a := EncodeDateTime (1899,3,4,23,1,1,1);
 b := EncodeDateTime (1899,3,5,23,1,1,1);

 if a < b then ShowMessage (DateTimeToSTr (a) + ' is less than '+DateTimeToSTr (b));
 if a > b then ShowMessage (DateTimeToSTr (a) + ' is greater than '+DateTimeToSTr (b));
 // a is an day less than b, so expect a result a <b, and so is.

 // CASE C
 a := EncodeDateTime (1899,3,5,22,1,1,1);
 b := EncodeDateTime (1899,3,5,23,1,1,1);

 if a < b then ShowMessage (DateTimeToSTr (a) + ' is less than '+DateTimeToSTr (b));
 if a > b then ShowMessage (DateTimeToSTr (a) + ' is greater than '+DateTimeToSTr (b));
 // Here also, a is one hour is less than b, so expect a result a <b,  but not so!
 end;
end.
« Last Edit: May 23, 2012, 12:11:08 am by silvestre »

Blaazen

  • Hero Member
  • *****
  • Posts: 2782
  • POKE 54296,15
    • Eye-Candy Controls
Re: ¿EncodeDateTime or TDateTime problem?
« Reply #1 on: May 23, 2012, 12:19:16 am »
// EJEMPLO C
 a := EncodeDateTime (1899,3,5,22,1,1,1);
 b := EncodeDateTime (1899,3,5,23,1,1,1);

It seems correct. In fact, TDateTime is Double, where integer part is date and fraction is time.
In your example C, both values are negative because this format calculates from 30-th Dec of 1899.

Values are:
a -300,917372696759
b -300,959039363426

So a>b here even if it is "more distant in history".
That day ends at -300.999999 and the next day (6.3.1899) begins at -299.000001

There is a function in DateUtils:
Code: [Select]
Function CompareDateTime(const A, B: TDateTime): TValueRelationship;
begin
  If SameDateTime(A,B) then
    Result:=EqualsValue
  else If A>B then
    Result:=GreaterThanValue
  else
    Result:=LessThanValue
end;         

but it seems this function also does not care the negative dates.
« Last Edit: May 23, 2012, 12:36:10 am by Blaazen »
Lazarus 2.1.0 r61214:62238 FPC 3.3.1 r40507 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

silvestre

  • New Member
  • *
  • Posts: 45
Re: ¿EncodeDateTime or TDateTime problem?
« Reply #2 on: May 23, 2012, 01:05:41 am »
Dear Blaazen,

Thank you very much for your explanation, and also for letting me know the function that can resolve the issue "CompareDate" or "CompareDateTime".  :) :) :)

I am very Thanked. 

Greetings from Barcelona!

Silvestre

KpjComp

  • Hero Member
  • *****
  • Posts: 680
Re: ¿EncodeDateTime or TDateTime problem?
« Reply #3 on: May 23, 2012, 11:25:53 am »
To cope with dates that go further back than 1899 then this version of CompareDateTime I think should be Ok.

Code: [Select]
Function CompareDateTime(const A, B: TDateTime): TValueRelationship;
begin
  If SameDateTime(A,B) then
    Result:=EqualsValue
  else if trunc(a)=trunc(b) then
  begin
    if abs(frac(a)) > abs(frac(b)) then result := GreaterThanValue
    else result := LessThanValue;
  end else begin
    if a > b then result := GreaterThanValue
    else result := LessThanValue;
  end;
end;         

Here it will cope with dates from year 1.

asdf

  • Sr. Member
  • ****
  • Posts: 310
Re: ¿EncodeDateTime or TDateTime problem?
« Reply #4 on: November 15, 2012, 01:01:05 pm »
Using
Code: [Select]
d1:=EncodeDateTime (2012,11,15,8,30,36,0);

The error in my code is

Error: Identifier not found "EncodeDateTime"

What have I missed in 'uses' ?
Lazarus 1.2.4 / Win 32 / THAILAND

Leledumbo

  • Hero Member
  • *****
  • Posts: 8140
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: ¿EncodeDateTime or TDateTime problem?
« Reply #5 on: November 15, 2012, 01:15:58 pm »
Quote
What have I missed in 'uses' ?
DateUtils;