December 29, 2004

fuzzytimepy

Fuzzy Time Python Function

Here is the Python function I've been using to fuzzy up time estimates, loosely inspired from the fuzzying function used in Camino.

def time(seconds):
    # check for seconds first
    if seconds < 60:
        if seconds < 13:
            if seconds < 7: seconds = 5
            else: seconds = 10
            return "under %d seconds" % seconds
        return "under a minute"

    # seconds becomes minutes and we keep checking
    minutes = seconds/60.0
    if minutes < 55:
        if minutes < 2: return "about a minute"
        return "about %d minutes" % minutes

    # this will never seemingly never end. now seconds become hours
    hours = minutes/60.0
    if hours < 48:
        if hours < 1.08: return "about an hour"
        elif hours < 1.92: return "over an hour"
        elif hours % 1 < 0.08 or hours % 1 > 0.92:
            if hours % 1 > 0.92: hours += 1
            return "about %d hours" % hours
        else: return "over %d hours" % hours

    # and the beat goes on, hours turn to days
    return "over %d days" % (hours / 24)

On a related note (and a shameless attempt to register both words with Google), when searching it still differentiates between the British spelling, humanise, and the American spelling, humanize. Not sure why you'd want to not get the results from both spellings, unless maybe you were looking for an entirely British opinion on the subject.

0 Comments:

Post a Comment

<< Home