Solved

Check time between two TDateTimes

Posted on 1998-07-19
6
279 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

747 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

14 Experts available now in Live!

Get 1:1 Help Now