BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Minutes Between

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Students)
View previous topic :: View next topic  
Author Message
bar
Guest





PostPosted: Thu Apr 05, 2007 2:01 pm    Post subject: Minutes Between Reply with 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
Back to top
Stephan Laska
Guest





PostPosted: Thu Apr 05, 2007 7:21 pm    Post subject: Re: Minutes Between Reply with quote



"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





PostPosted: Thu Apr 05, 2007 9:24 pm    Post subject: Re: Minutes Between Reply with quote



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





PostPosted: Thu Apr 05, 2007 9:47 pm    Post subject: Re: Minutes Between Reply with quote

"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





PostPosted: Mon Apr 16, 2007 6:39 pm    Post subject: Re: Minutes Between Reply with quote

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





PostPosted: Tue Apr 17, 2007 8:11 am    Post subject: Re: Minutes Between Reply with quote

"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
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Students) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.