Board index » cppbuilder » Minutes Between

Minutes Between


2007-04-05 05:01:26 PM
cppbuilder9
Hello all
The following code
MinutesBetween("30/04/2007 08:00","30/04/2007 08:30")
is returning 29 minutes,which is supposed to 30.
Thanks for suggestion
SA
 
 

Re:Minutes Between

"bar" < XXXX@XXXXX.COM >wrote in message
Quote
Hello all

The following code
MinutesBetween("30/04/2007 08:00","30/04/2007 08:30")
is returning 29 minutes,which is supposed to 30.

Thanks for suggestion
what are you doing inside MinutesBetween()?
 

Re:Minutes Between

bar wrote:
Quote
Hello all

The following code
MinutesBetween("30/04/2007 08:00","30/04/2007 08:30")
is returning 29 minutes,which is supposed to 30.

Thanks for suggestion
SA


A TDateTime uses a floating point value to store it's date,
since floating points can't store every real number, it has to round the
value. Your experience is an effect of that.
Also see tinyurl.com/2tfuy7
A very interesting article is "What Every Computer Scientist Should Know
About Floating-Point Arithmetic":
docs.sun.com/source/806-3568/ncg_goldberg.html
 

{smallsort}

Re:Minutes Between

"Stephan Laska" <Ask & maybe I'll Tell>wrote in message
Quote
what are you doing inside MinutesBetween()?
MinutesBetween() is a Borland function that was introduced in BCB 6.
There is a new DateUtils unit that contains all sorts of date/time
related functions.
Gambit
 

Re:Minutes Between

long int TotalMinutes=MinutesBetween(D2->DateTime,D1->DateTime);
where D1, D2 are datetime picker controls
Thanks
"Stephan Laska" <Ask & maybe I'll Tell>wrote in message
Quote

"bar" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...
>Hello all
>
>The following code
>MinutesBetween("30/04/2007 08:00","30/04/2007 08:30")
>is returning 29 minutes,which is supposed to 30.
>
>Thanks for suggestion

what are you doing inside MinutesBetween()?

 

Re:Minutes Between

"bar" < XXXX@XXXXX.COM >wrote:
Quote
The following code
MinutesBetween("30/04/2007 08:00","30/04/2007 08:30")
is returning 29 minutes,which is supposed to 30.
Koen has answered the question: The VCL uses the type double
to represent a datetime internally. And as he pointed out, one
has to be quite careful when working with floating point types.
It is certainly a very bad design to use double for datetimes.
Well, usually it is exact enough to represent a date, but when
it comes to precise calculations it fails. Subtracting one
TDateTime from another gives you the fraction of days. Simply
multiplying it with 24 (hours) and 60 (minutes) gives you the
elapsed time in minutes, in your case 29,9999999930151. As
MinutesBetween is designed to only count completely elapsed
minutes, it gives you 29. The solution is to do the calculation
yourself. The following gives you the elapsed minutes rounded
to the nearest integer:
TDateTime DateTime1 = EncodeDateTime(2007, 4, 30, 8, 0, 0, 0);
TDateTime DateTime2 = EncodeDateTime(2007, 4, 30, 8, 30, 0, 0);
int Minutes = RoundTo( static_cast<double>(DateTime2-DateTime1)*24.0*60.0 ,0);
If you don't want say 10.6 minutes counted as 11 but just 10
however, then you would have to do the desired rounding by
yourself too (e.g. by adding 0.1 and then cast to int, thus
making 10.9==11 and 10.8==10).