def round_to_hh_mm(ts, round_down=False):
    """ Round a timestamp to a (h, m, s) tuple of the form hh:mm:00 """
    h, m, s = ts.hour, ts.minute, ts.second
    if round_down:
        s = 0
    elif s > 30 or s == 30 and m % 2:
        s = 0
        m += 1
        if m >= 60:
            m -= 60
            h += 1
            if h >= 24:
                h -= 24
    return h, m, s
