Solved

Check time between two TDateTimes

Posted on 1998-07-19
6
281 Views
Last Modified: 2010-08-05
OK, I've started making a simple alarm program and I've run into a fairly major problem - I'm saving all my alarms OK and successfully recalling them as TDateTimes, but there's a difficulty.  Are you sitting comfortably?

Say I have alarms at 08:30, 11:15 and 16:30.  This is fine if the app starts before the earliest alarm - it can progress through the sequence easily enough.  But since the app runs on a network, it is getting started and stopped all the time, so if a user logs on and the app runs at 10:13, it needs to reorganize the order in which the alarms come.

Bear in mind that this has to be flexible: a user might stay logged on for over 24 hours...

I've tried doing stuff like Now-AlarmOne but mathematical operators seem to go haywire with this!

Good Luck!

Ed
0
Comment
Question by:edmoore
6 Comments
 
LVL 3

Expert Comment

by:Matvey
ID: 1358201
The TDateTime format allows you to compare times. You can write a thing like this: if time1> time2 then... Do you understand? It will compare the first time and the seccond.

Can you exaplin better what is your problem and what you don't understand?
0
 
LVL 10

Expert Comment

by:Jacco
ID: 1358202
Most mathematical operators should work fine with TDateTime.

The most commonly used is -
But you can also / * and + with it.

(Time2-Time1)*24*60 is the amount of minutes passed between Time1 and Time2.

A TDateTime is just a Double.

The whole part are the days since 1899.
The fraction is the part of the day.

1.5 =

1 day
12 hours

1 hour is 1/24
1 minute 1/(24*60)
1 second 1/(24*60*60)

You can also use DecodeDate EncodeDate DecodeTime and EncodeTime.

Regards Jacco
0
 

Expert Comment

by:nileq
ID: 1358203
Could you define your problem more?
Lets say a user logs on at 10:13, he had an alarm set at 08:30 do you wont that alarm to go off?
Should the 08:30 alarm go off every day or only on the set date?

Let me know and I will help you.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 8

Expert Comment

by:ZifNab
ID: 1358204
? Time and date are stored in a float type (or is it double?). Heck doesn't mind, either way the date is defined with the numbers before the decimal separator and time is defined with these after the decimal sperator. Now when you compare two times it is fairly simple isn't it? the number later on the day will always be greater then the other....

Or am I missing the question here?

Zif.
0
 
LVL 6

Accepted Solution

by:
Holger101497 earned 70 total points
ID: 1358205
ok, several people so far have said that the date is in the integer part and the time in the frac-part.
The reason I'm posting this as an answer is because I think I see your problem: These are DAILY alarms, right? 8:30 every day?
You can "solve" your problems by using the frac-part of your times, thereby ignoring the date/day.

IF (Frac(Now)>Frac(Alarm)) // later than alarm
AND (Frac(Now)<Frac(Alarm)+5/24/60) //alarm time less than 5 minutes ago
THEN SoundAlarm;
// btw: "now" is a function - instead of calling it several times, store result in a variable. Also reduces strange effects if "Now" changes from line to line *g* - it rarely hits a critical value, but if it only happens once in a thousand times, it's almost impossible to track down :-))

Of course you have to "remember"/store which alarms were used and which ones weren't...

I don't really see a need to "sort" the alarms, but if you really want to:
SecsRemaining:=Round( (Frac(Alarm)-Frac(Now)+1)*24*60*60) MOD (24*60*60);
// if you are using 16-bit Delphi, you might need to typecast to avoid overflows
// this should return the seconds remaining before the alarm should be triggered the next time. Use this value to sort.


Good luck! Let me know if that helped and ask for details before rejecting the answer.
:o))

It's ( '-)
:-))
0
 

Author Comment

by:edmoore
ID: 1358206
OK, thanks everyone for helping me with this - I didn't realise how quickly it would fill up or I would have checked back more often.

I'll have to award the points to the only actual formal answer, but all of you have been very helpful, thanks!

Ed :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PDF library for Delphi 2 104
Sending Gmail through Delphi 3 91
build html 5 client that connect to indy Tidtcpserver 3 350
how can i search if string exist in array ? 3 49
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now