JosefJezek
7/9/2014 - 10:37 AM

Python String Format

Python String Format

Python String Format

def format_timedelta(timedelta):
    s = timedelta.total_seconds()
    days = s // 86400
    day_plural = 'den', 'dny', 'dnů'

    if days == 1:
        day_name = day_plural[0]
    elif days > 1 and days <= 4:
        day_name = day_plural[1]
    else:
        day_name = day_plural[2]

    return '{:.0f} {} {:02.0f}:{:02.0f}:{:02.0f}'.format(days, day_name, s % 86400 // 3600, s % 3600 // 60, s % 60)