Fog Creek Software
Discussion Board




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
Tuesday, July 06, 2004

What format is the time in?

Methinks easiest is to ensure your time is 24 hour format, then just add 30 minutes to old time and verify it's still less than new time.  But it really depends on the data format.

Snotnose
Tuesday, July 06, 2004

You probably have a date library in your programming language ( which you didn't specify ). Use it instead of reinventing the wheel.

Eric V.
Tuesday, July 06, 2004

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
Tuesday, July 06, 2004

Is this homework due today, or tomorrow?

Ron
Tuesday, July 06, 2004

It's not homework.  My brain is fried and I can't come up with an easy/short way to do this.

Joe
Tuesday, July 06, 2004

Why waste integers ? You might squeeze the hour and the minute into chars. Just to have some more fun.

BGP
Tuesday, July 06, 2004

Well, have a good night and you'll certainly find the solution tomorrow. (By the way, C++ is premature optimization ;-).

Pakter
Tuesday, July 06, 2004

>> Well, have a good night and you'll certainly find the solution tomorrow. (By the way, C++ is premature optimization ;-).

Whatever, dude.

Joe
Tuesday, July 06, 2004

man time(2)

mb
Tuesday, July 06, 2004

>> All suggestions are appreciated, thanks

Whatever, dude.

Bob
Tuesday, July 06, 2004

Convert both to minutes-since-midnight, then if abs(new - old) < 30 they conflict.

If you can have multiple days (11:50PM and 00:05AM the next day), convert to minutes since midnight on day 0.

Ron
Tuesday, July 06, 2004

i just checked on paper, i'm tired but you get the idea, work well for different days (eg, 23:55 and 00:12)
------
a = AHour*60 + AMinute
i = BHour*60 + BMinute
i = 30
r1 = 0
r2 = 0

r1 = a1+i
r2 = a1-i

if (r1 > 1439) { r1 = 1440 + r1)
if (r2 < 0)    { r2 = 1440 - r2)

if (b>=r1) && (b<=r2) { YES }

protpriv
Tuesday, July 06, 2004

if (r1 > 1439) { r1 = 1440 + r1)

should be

if (r1 > 1439) { r1 = r1 - 1440)

protpriv
Tuesday, July 06, 2004

Thanks for all the help, especially Ron and protpriv.

Joe
Tuesday, July 06, 2004

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
Tuesday, July 06, 2004

What are you going to work on next? File I/O classes, collection classes, etc?

Good lord, use a date library.

free(malloc(-1))
Tuesday, July 06, 2004

>> What are you going to work on next? File I/O classes, collection classes, etc?

>> Good lord, use a date library.

Thanks, mom.

Joe
Tuesday, July 06, 2004

:)

Joe
Tuesday, July 06, 2004

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.


Tuesday, July 06, 2004

> map the minutes into a circle (like your wrest watch), and create a pie slice

This will help:

man arctan

Dennis Atkins
Tuesday, July 06, 2004

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
Tuesday, July 06, 2004

*  Recent Topics

*  Fog Creek Home