I have  a function that I use to round time increments. I just realized it 
fails when the time rolls over the 24 hour period.

I also found myself needing to make a second version for rounding to hours. 
Anyone care to take a stab at fixing it to handle the rollover AND hours?

def round_off(mins, secs, to_nearest=1):
    div_result, remainder = divmod(mins, to_nearest)
    
    if remainder < 7:       
        return to_nearest * div_result
    elif remainder > 7:
        return to_nearest * (div_result + 1)
    else:    #remainder == 7 
        if secs < 30:
            return to_nearest * div_result
        else:
            return to_nearest * (div_result + 1)

If the the time is 2010, 12, 16, 23, 54 and the to_nearest is 5 (round to 
nearest 5 minutes), it works. 
If the time is     2010, 12, 16, 23, 55

it errors with the message "minutes must be 0..59"

I use it like this:
finishtime = lasttime.replace(minute=round_off(lasttime.minute, 
lasttime.second, to_nearest=5), second=0)

I'm too tired & ignorant to figure it out.

-- 
Lorin Rivers
Mosasaur: Killer Technical Marketing <http://www.mosasaur.com>
<mailto:lriv...@mosasaur.com>
512/203.3198 (m)


Reply via email to