![]() |
![]() |
![]() |
Short test for time comparison? I need to check if a given hour and minute conflicts with another hour and minute (within 30 minutes of eachother, basically). So if I have 'oldHour' and 'oldMinute', how would I check if that time is within 30 minutes of 'newHour' and 'newMinute'? It has to take into account AM and PM hours. I'm sure I can do this, but haven't come up with a short and easy way to do it. All suggestions are appreciated, thanks.
Joe
What format is the time in?
Snotnose
You probably have a date library in your programming language ( which you didn't specify ). Use it instead of reinventing the wheel.
Eric V.
Well, I am doing this in C++. Currently I am making a time class, which holds an integer for the hour and an integer for the minute....
Joe
Is this homework due today, or tomorrow?
Ron
It's not homework. My brain is fried and I can't come up with an easy/short way to do this.
Joe
Why waste integers ? You might squeeze the hour and the minute into chars. Just to have some more fun.
BGP
Well, have a good night and you'll certainly find the solution tomorrow. (By the way, C++ is premature optimization ;-).
Pakter
>> Well, have a good night and you'll certainly find the solution tomorrow. (By the way, C++ is premature optimization ;-).
Joe
man time(2)
mb
>> All suggestions are appreciated, thanks
Bob
Convert both to minutes-since-midnight, then if abs(new - old) < 30 they conflict.
Ron
i just checked on paper, i'm tired but you get the idea, work well for different days (eg, 23:55 and 00:12)
protpriv
if (r1 > 1439) { r1 = 1440 + r1)
protpriv
Thanks for all the help, especially Ron and protpriv.
Joe
well maybe i'm screwed up the pseudo (i didn't sleep for >24 hrs) the idea is to map the minutes into a circle (like your wrest watch), and create a pie slice, and make sure that midnight is corrected (1440 thing), then see if the other fits in that slice.
protpriv
What are you going to work on next? File I/O classes, collection classes, etc?
free(malloc(-1))
>> What are you going to work on next? File I/O classes, collection classes, etc?
Joe
:)
Joe
datediff cannot really work here as you know only hours and minutes but not the actual day. and you know that the days can be different.
> map the minutes into a circle (like your wrest watch), and create a pie slice
Dennis Atkins
Convert hours and minutes to seconds. This gives you two integers, then subtract. If absolute value <= (30*60) then they are within 30 minutes of each other.
VoidIfRemoved
|