Recent

Author Topic: Time Difference (Minutes Between)  (Read 7763 times)

pipingae

  • New Member
  • *
  • Posts: 11
Time Difference (Minutes Between)
« on: November 20, 2014, 09:24:11 am »
Hai ...

I'm trying to use MinutesBetween in lazarus, but kind of confused since the result (according to me) is inaccurate. Each time the minutes difference is 240 minutes the result is 239 minutes. Whats wrong ? any one help ?

this is the simple code that i write

procedure TForm1.FormCreate(Sender: TObject);
var timeAwal,timeAkhir:TDateTime;
begin
  timeAwal:=StrToDateTime('2014-11-01 06:00:00');
  timeAkhir:=StrToDateTime('2014-11-01 10:02:00');
  showMessage(IntToStr(MinutesBetween(timeAkhir,timeAwal)));
end;         

the correct minutes should have ben 240 minutes, i include the screen shot attachment

JZS

  • Full Member
  • ***
  • Posts: 184
Re: Time Difference (Minutes Between)
« Reply #1 on: November 20, 2014, 10:08:36 am »
Hi pipingae,

I see it's your first post here so, welcome.

About your code:
I tested your code and it gives the expected result 240.

BTW you mentioned "timeAkhir" to be 10:02:00 in your post, but I corrected it to be 10:00:00 to match the screenshot and it works OK. Even when it was 10:02:00 it gives 242 which is correct also.

Which version of Lazarus you are using?
I use recent stable release

wp

  • Hero Member
  • *****
  • Posts: 7636
Re: Time Difference (Minutes Between)
« Reply #2 on: November 20, 2014, 10:26:18 am »
This could be due to rounding errors. As far as I know MinutesBetween uses the trunc function which is off by one if the value to be rounded turns out like 239.9999999998.

I'd use an own function:

Code: [Select]
function MinutesBetween(ADateTime1, ADateTime2:TDateTime): Integer;
const
  MINUTES_PER_DAY = 24*60;
  EPS = 1E-6;  // adjust value
var
  minutes: Double;
begin
  minutes := (ATime2 - ATime1) / MINUTES_PER_DAY;
  if frac(minutes) > (1.0-EPS) then  // detect rounding error; EPS may require some adjustment
    Result := round(minutes)
  else
    Result := trunc(minutes);  // consider only full minutes
end
;
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

pipingae

  • New Member
  • *
  • Posts: 11
Re: Time Difference (Minutes Between)
« Reply #3 on: November 20, 2014, 11:05:10 am »
I'm very very sorry, the correct timeAkhir was 10:00:00.

I'm using Lazarus 1.2.4 Win 32 version on Windows 8.1 64 Bit

Still i'm getting 239 minutes result, other time difference is good.


Any help

pipingae

  • New Member
  • *
  • Posts: 11
Re: Time Difference (Minutes Between)
« Reply #4 on: November 20, 2014, 11:09:33 am »
i've try changing the timeAkhir, i include the screenshot, only when the time begin at 07:00:00 the result is correct, other than that it give me 239 minutes ....

JZS

  • Full Member
  • ***
  • Posts: 184
Re: Time Difference (Minutes Between)
« Reply #5 on: November 20, 2014, 11:21:38 am »
I tried on Laz1.2.4, and as was expected, the result is correct same as on Laz1.2.6.

What do you get if you try MinuteSpan()? Try it please:
Code: [Select]
showMessage('Min. Span: '+ floattostr(MinuteSpan(timeAkhir, timeAwal)));
What about setting the time part only:
Code: [Select]
procedure TForm1.FormCreate(Sender: TObject);
var
  timeAwal,
  timeAkhir      :TTime;
begin
  timeAwal:= StrToTime('06:00:00');
  timeAkhir:= StrToTime('10:00:00');

  showMessage('MinutesBetween '+ IntToStr(MinutesBetween(timeAkhir, timeAwal))
  +#13#10+ 'TimeSpan '+ floattostr(MinuteSpan(timeAkhir, timeAwal)));
end;
I use recent stable release

howardpc

  • Hero Member
  • *****
  • Posts: 3553
Re: Time Difference (Minutes Between)
« Reply #6 on: November 20, 2014, 12:02:10 pm »
All floating-point calculations are inherently approximate. Identical TDateTime code run on different computers always has the possibility that its results might differ by some tiny increment. Rounding tries to eliminate such differences, but they depend on factors that are not always known or controllable (floating point computations done in FPU or by emulation, for instance).
Using strings for datetime values and integer arithmetic for operations is a better way to guarantee consistent precision within a stated increment. TDateTime as a double has approximation built in to it.

A further point is what "MinutesBetween" is understood to mean. The docs for this function say
    "Calculate the number of whole minutes between two DateTime values."

Some might argue that the number of whole minutes between
  01/11/2014 10:00:00 and
  01/11/2014 10:01:00
is 0 and not 1 (some mathematicians are like that).

pipingae

  • New Member
  • *
  • Posts: 11
Re: Time Difference (Minutes Between)
« Reply #7 on: November 20, 2014, 12:05:05 pm »
Hi

I've the minutespan, and i think the result is the same 239 if using the TDateTime variable type, but if we only use TTime variable, the result is accurate (240 minutes)

i've include the screenshot

 

TinyPortal © 2005-2018