Solved

Check time between two TDateTimes

Posted on 1998-07-19
6
285 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
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 an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 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