[Tutor] Comparing Time.

alan.gauld@bt.com alan.gauld@bt.com
Tue, 30 Jul 2002 12:23:37 +0100


> "As a second exercise, write a boolean function after that takes two
> Time objects, t1 and t2, as arguments, and returns true (1) if t1
> follows t2 chronologically and false (0) otherwise. "
> 
> And the Time object is a very simple object with 3 attributes: hours,
> minutes, seconds. As said I did the above exercise but in a
> non-elegant way using 9 if statements (3 * if >, <, ==).

def isAfter(t1,t2):
    " return true if t1 is later than t2 "
    t1secs = t1.hours * 3600 + t1.minutes * 60 + t1.seconds
    t2secs = t2.hours * 3600 + t2.minutes * 60 + t2.seconds
    return t1secs < t2secs

Should do it. If you wanbt to be absolutely sure that 
its 1,0 rather than any other non zero value then use 
an if/else with explicit returns.

Alan g.