def timestr(s):
    """
    Takes a value in seconds (s) and converts it into a string of the
    format hh:mm:ss in 24 hour time.
    If the number of seconds exceeds a day, it starts over from the
    beginning. This assumes that days is the largest unit of time in the
    context.
    """

    # Number of days (if any)
    days = int(s / 86400)

    if days > 0:
        s -= days * 86400

    h = int(s / 3600)
    m = int((s - 3600 * h) / 60)
    s = int(s - 3600 * h - 60 * m)

    if h < 10:
        hstr = "0%i" % h
    else:
        hstr = "%i" % h

    if m < 10:
        mstr = "0%i" % m
    else:
        mstr = "%i" % m

    if s < 10:
        sstr = "0%i" % s
    else:
        sstr = "%i" % s

    ts = hstr + ":" + mstr + ":" + sstr

    return ts