 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
bar Guest
|
Posted: Thu Apr 05, 2007 2:01 pm Post subject: Minutes Between |
|
|
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 |
|
| Back to top |
|
 |
Stephan Laska Guest
|
Posted: Thu Apr 05, 2007 7:21 pm Post subject: Re: Minutes Between |
|
|
"bar" <shaikakbar250281 (AT) yahoo (DOT) co.in> wrote in message
news:4614bace (AT) newsgroups (DOT) borland.com...
| 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()? |
|
| Back to top |
|
 |
Koen Guest
|
Posted: Thu Apr 05, 2007 9:24 pm Post subject: 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 http://tinyurl.com/2tfuy7
A very interesting article is "What Every Computer Scientist Should Know
About Floating-Point Arithmetic":
http://docs.sun.com/source/806-3568/ncg_goldberg.html |
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Thu Apr 05, 2007 9:47 pm Post subject: Re: Minutes Between |
|
|
"Stephan Laska" <Ask & maybe I'll Tell> wrote in message
news:461505d4$1 (AT) newsgroups (DOT) borland.com...
| 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 |
|
| Back to top |
|
 |
bar Guest
|
Posted: Mon Apr 16, 2007 6:39 pm Post subject: 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
news:461505d4$1 (AT) newsgroups (DOT) borland.com...
| Quote: |
"bar" <shaikakbar250281 (AT) yahoo (DOT) co.in> wrote in message
news:4614bace (AT) newsgroups (DOT) borland.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()?
|
|
|
| Back to top |
|
 |
Thorsten Kettner Guest
|
Posted: Tue Apr 17, 2007 8:11 am Post subject: Re: Minutes Between |
|
|
"bar" <shaikakbar250281 (AT) yahoo (DOT) co.in> 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). |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|