python格式化时间段

def td_format(td_object):
    seconds = int(td_object.total_seconds())
    periods = [
        ('year', 60*60*24*365),
        ('month', 60*60*24*30),
        ('day', 60*60*24),
        ('hour', 60*60),
        ('minute', 60),
        ('second', 1)
    ]
    result = {}
    for period_name,period_seconds in periods:
        if seconds> period_seconds:
            period_value, seconds = divmod(seconds,period_seconds)
            result[period_name] = period_value
        else:
            result[period_name] = 0
    return result

你可能感兴趣的:(python格式化时间段)