IT SOLUTIONS
Your TECHNOLOGY partner! 
-Collapse +Expand
Delphi
Search Delphi Group:

Advanced
-Collapse +Expand Delphi Group Home
-Collapse +Expand Message Board
-Collapse +Expand Delphi KB
-Collapse +Expand Delphi To/From
To/FromCODEGuides
-Collapse +Expand Delphi Study Test
PRESTWOODCERTIFIED
-Collapse +Expand Delphi Store
PRESTWOODSTORE
-Collapse +Expand Members Only

Prestwood eMagazine

May Edition
Subscribe now! It's Free!
Enter your email:

   ► MB LobbyPascal and Delphi Coding BoardDelphi Object PASCAL Topic     Print This   

Bizarre date/time logics

Bizarre date/time logics in Delphi Object PASCAL topic (part of our Pascal and Delphi Coding group).

Quick Search: Bizarre   date/time   Bizarre date/time   Bizarre date/time logics  

hyp3n0zy5
Can anyone give me an explanation as to why the following first assert fails? (i use D7)

var dt1, dt2: TDateTime;
begin
dt1 := Now;
dt2 := IncHour(dt1, 1);
Assert(HoursBetween(dt1, dt2) = 1, '1st assert!');

dt1 := Now;
dt2 := IncHour(dt1, 2);
Assert(HoursBetween(dt1, dt2) = 2, '2nd assert!');
 Posted 8 years ago (Thread Starter)
Comment Quote
Location=USA,  Joined=12 years ago   MB Posts=21  
More... -Collapse +Expand
hyp3n0zy5
USA
Rank: Cadet 2nd Year
Email A E USA
Approved member.
Member subscribes to this thread with a verified email.
Old Account!
If this is your account, sign in to activate web presence data (sign in quarterly to keep active). Alternatively, you can subscribe to our monthly eMag with a valid email address.
Web Presence Hidden.
Once above is taken care of, full Profile content will display including back links, about me, my message, custom Profile html, social networking links, message board signature, company profile, etc.

Post ID #11191, 8 replies
Thread Started 8/5/2005 2:08:00 AM
View Counter=3289
Last Reply Posted 3/31/2009 11:38:44 AM)

Rick Carter

That QC link now needs to be http://qc.embarcadero.com/wc/qcmain.aspx?d=3820

That report only had two votes, which may be part of the reason it's still not fixed. I added one vote; perhaps others can do the same?

Rick Carter
Chair, Delphi/Paradox SIG
Cincinnati PC Users Group
 Posted 50 months ago
Comment Quote
Location=Cincinnati OH USA ,  Joined=12 years ago   MB Posts=518  
More... -Collapse +Expand
Rick Carter
Cincinnati OH USA
Rank: Commander
Email A E USA
Approved member.
Member subscribes to this thread with a verified email.

Post ID #13412 (Level 1.1)  Reply to 11191
Thread Started 3/31/2009 1:01:17 AM

William Pantoja
Neither assert should fail. However, there are issues in Delphi 7 with asserts failing or succeeding when they should not.

Conditional compilation is a solid alternative that does not fail.

---
William Pantoja

Consultant/Software Engineer
ForceOne Technologies, Inc.

 Posted 8 years ago
Comment Quote
Location=Woodinville, WA, USA ,  Joined=12 years ago   MB Posts=163   KB Posts=1  
More... -Collapse +Expand
William Pantoja
Woodinville, WA, USA
Rank: Lieutenant-Junior Grade
Email A E WA USA
Approved member.
Member subscribes to this thread with a verified email.

Post ID #11192 (Level 1.2)  Reply to 11191
Reply Posted 8/5/2005 8:49:00 AM

William Pantoja
In this case, true. HoursBetween returns 0. However, Delphi 7 does have issues with it's asserts but on to the reason why HoursBetween is failing.

Keep in mind, TDateTime is a double--and floating point numbers are by definition inprecise. You are trying to increment this number by 1/24th of a day--or 0.041666666667. If you immediately look at the difference between the times after doing so, you will note that it is 0.041666666664. This effect is caused by the implict round off error that happens whenever you try to represent an inprecise number precisely.

If you want precise date and time calculations you must use an integral date and time format (such as SYSTEMTIME or FILETIME).

To illustrate this, if you create a new project, drop a memo field (with the font set to Courier New) and a button on it and insert the following code associating the OnClick event in the button with the code Button1Click below,

function Pad (S : string; L : Integer) : string;

begin
Result := S;
while Length(Result) < L do
begin
Result := Result+' ';
end;
end;

procedure TForm1.Button1Click(Sender: TObject);

var
dt1 : TDateTime;
dt2 : TDateTime;
I : Integer;

begin
Memo1.Clear;
Memo1.Lines.Add('Hours MillisecondsBewteen Result Expected Result');
Memo1.Lines.Add('----- -------------------------- ---------------');
for I := 1 to 60 do
begin
dt1 := Now;
dt2 := IncHour(dt1,I);
Memo1.Lines.Add(Pad(IntToStr(I),5)+' '+Pad(IntToStr(MillisecondsBetween(dt1,dt2)),26)
        +' '+IntToStr(I*3600000));
end;
end;
You receive the following results:

Hours MillisecondsBewteen Result Expected Result
----- -------------------------- ---------------
1 3599999 3600000
2 7200000 7200000
3 10800000 10800000
4 14399999 14400000
5 18000000 18000000
6 21600000 21600000
7 25199999 25200000
8 28800000 28800000
9 32400000 32400000
10 35999999 36000000
11 39600000 39600000
12 43200000 43200000
13 46799999 46800000
14 50400000 50400000
15 54000000 54000000
16 57599999 57600000
17 61200000 61200000
18 64800000 64800000
19 68399999 68400000
20 72000000 72000000
21 75600000 75600000
22 79199999 79200000
23 82800000 82800000
24 86400000 86400000
25 89999999 90000000
26 93600000 93600000
27 97200000 97200000
28 100799999 100800000
29 104400000 104400000
30 108000000 108000000
31 111599999 111600000
32 115200000 115200000
33 118800000 118800000
34 122399999 122400000
35 126000000 126000000
36 129600000 129600000
37 133199999 133200000
38 136800000 136800000
39 140400000 140400000
40 143999999 144000000
41 147600000 147600000
42 151200000 151200000
43 154799999 154800000
44 158400000 158400000
45 162000000 162000000
46 165599999 165600000
47 169200000 169200000
48 172800000 172800000
49 176399999 176400000
50 180000000 180000000
51 183600000 183600000
52 187199999 187200000
53 190800000 190800000
54 194400000 194400000
55 197999999 198000000
56 201600000 201600000
57 205200000 205200000
58 208799999 208800000
59 212400000 212400000
60 216000000 216000000

---
William Pantoja

Consultant/Software Engineer
ForceOne Technologies, Inc.

 Posted 8 years ago
Comment Quote
Location=Woodinville, WA, USA ,  Joined=12 years ago   MB Posts=163   KB Posts=1  
More... -Collapse +Expand
William Pantoja
Woodinville, WA, USA
Rank: Lieutenant-Junior Grade
Email A E WA USA
Approved member.
Member subscribes to this thread with a verified email.

Post ID #11195 (Level 1.3)  Reply to 11191
Reply Posted 8/8/2005 10:39:00 AM
Last Updated 7/15/2007 12:47:23 PM

hyp3n0zy5
It has nothing to do with the Assert. I just use this Assert to show what's wrong. What this post is about is that if i do a IncHour on a date, then i expect that the HoursBetween this new and old date would be 1. But it isn't. However, if i do a IncHour by 2, then the HoursBetween function results 2. So then it does work good. I think it's a bug. But maybe somebody has an explanation for it.
 Posted 8 years ago (Thread Starter)
Comment Quote
Location=USA,  Joined=12 years ago   MB Posts=21  
More... -Collapse +Expand
hyp3n0zy5
USA
Rank: Cadet 2nd Year
Email A E USA
Approved member.
Member subscribes to this thread with a verified email.
Old Account!
If this is your account, sign in to activate web presence data (sign in quarterly to keep active). Alternatively, you can subscribe to our monthly eMag with a valid email address.
Web Presence Hidden.
Once above is taken care of, full Profile content will display including back links, about me, my message, custom Profile html, social networking links, message board signature, company profile, etc.

Post ID #11194 (Level 1.4)  Reply to 11191
Reply Posted 8/8/2005 12:28:00 AM

hyp3n0zy5
Thanks for your answer.
I already thought that it would be caused by rounding errors, but still i find the way these specific routines work not satisfactory. These methods should take the roundoff errors into account. I program to the interface, not the implementation. If the interface says "this method increments a date/time with one hour", and another method says "i tell you the difference between two date/times in hours", i expect them to behave according to this interface. So when i call the second method after using the first, it should tell me that 1 hour has passed. But this is ofcourse my point of view, and who am i? ;]
 Posted 8 years ago (Thread Starter)
Comment Quote
Location=USA,  Joined=12 years ago   MB Posts=21  
More... -Collapse +Expand
hyp3n0zy5
USA
Rank: Cadet 2nd Year
Email A E USA
Approved member.
Member subscribes to this thread with a verified email.
Old Account!
If this is your account, sign in to activate web presence data (sign in quarterly to keep active). Alternatively, you can subscribe to our monthly eMag with a valid email address.
Web Presence Hidden.
Once above is taken care of, full Profile content will display including back links, about me, my message, custom Profile html, social networking links, message board signature, company profile, etc.

Post ID #11196 (Level 1.5)  Reply to 11191
Reply Posted 8/9/2005 7:33:00 AM

hyp3n0zy5
I see it has been submitted to QualityCentral as a bug.

"HoursBetween fails to return the good value"
http://qc.borland.com/wc/qcmain.aspx?d=3820

A replacement method can be found here as well.
 Posted 8 years ago (Thread Starter)
Comment Quote
Location=USA,  Joined=12 years ago   MB Posts=21  
More... -Collapse +Expand
hyp3n0zy5
USA
Rank: Cadet 2nd Year
Email A E USA
Approved member.
Member subscribes to this thread with a verified email.
Old Account!
If this is your account, sign in to activate web presence data (sign in quarterly to keep active). Alternatively, you can subscribe to our monthly eMag with a valid email address.
Web Presence Hidden.
Once above is taken care of, full Profile content will display including back links, about me, my message, custom Profile html, social networking links, message board signature, company profile, etc.

Post ID #11197 (Level 1.6)  Reply to 11191
Reply Posted 8/10/2005 5:59:00 AM
Most Recent Post

Wes Peterson

I cast a vote, too.

The bug(s) are very well documented in this QC report, and good fixes have been suggested.  This is difinitely an area that needs attention.

Wes Peterson
Senior Software Engineer
Prestwood IT Solutions
http://www.prestwood.com

 Posted 50 months ago
Comment Quote
Location=Prestwood IT office in Citrus Heights, CA,  Joined=9 years ago   MB Posts=156   KB Posts=133  
More... -Collapse +Expand
Moderator
Wes Peterson
Prestwood IT
Prestwood IT office in Citrus Heights, CA
Rank: Admiral
Email A E CA USA
Approved member.
Member subscribes to this thread with a verified email.
About Wes Peterson

Wes Peterson is a Senior Programmer Analyst with Prestwood IT Solutions where he develops custom Windows software and custom websites. When Wes is not coding for clients, he participates in this online community. Prior to his 10-year love-affair with Delphi, he worked with several other tools and databases. Currently he specializes in VS.Net using C# and VB.Net. To Wes, the .NET revolution is as exciting as the birth of Delphi.


Post ID #13415 (Level 1.7)  Reply to 11191
Reply Posted 3/31/2009 11:36:56 AM

Daniel Kram

I voted. It should be fixed.

 Posted 50 months ago
Comment Quote
Location=?,  Joined=55 months ago   MB Posts=25   KB Posts=4  
More... -Collapse +Expand
Daniel Kram
?
Rank: Cadet 4th Year
Email A E Unk
Approved member.
Member subscribes to this thread with a verified email.

Post ID #13414 (Level 1.8)  Reply to 11191
Reply Posted 3/31/2009 9:24:40 AM

Revive Thread!

Add a comment to revive this old thread and make this archived thread more useful.

Write a Comment...
Full Editor
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post:

Enter your name and security key.

Your Name:
Today's security key = P235A
Enter key:
Icon: A Post    Thread    Idea    Important!    Cool    Sad    No    Yes    Includes a Link...   
Thread #11191 Counter
3289
Since 4/2/2008
-
  Load Time=1 seconds.
 
Print This
-
 
Have a question? Need our services? Contact us now.
--Mike Prestwood

Call: 916-726-5675

email: info@prestwood.com


813 People Online Now!!  
Online Now: Sign In to see who's online now!  Not a member? Join now. It's free!
Show More...


©1995-2013 PrestwoodBoards  [Security & Privacy]
Professional IT Services: Coding | Websites | Computer Tech