Solved

Check time between two TDateTimes

Posted on 1998-07-19
6
288 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

705 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