Dark Launch

Python Time Duration Human; Friendly Timestamp

Python
def duration_human(seconds):
    seconds = long(round(seconds))
    minutes, seconds = divmod(seconds, 60)
    hours, minutes = divmod(minutes, 60)
    days, hours = divmod(hours, 24)
    years, days = divmod(days, 365.242199)
 
    minutes = long(minutes)
    hours = long(hours)
    days = long(days)
    years = long(years)
 
    duration = []
    if years > 0:
        duration.append('%d year' % years + 's'*(years != 1))
    else:
        if days > 0:
            duration.append('%d day' % days + 's'*(days != 1))
        if hours > 0:
            duration.append('%d hour' % hours + 's'*(hours != 1))
        if minutes > 0:
            duration.append('%d minute' % minutes + 's'*(minutes != 1))
        if seconds > 0:
            duration.append('%d second' % seconds + 's'*(seconds != 1))
    return ' '.join(duration)

Example:
Python
seconds = [
    1,
    2,
    3600,
    7201,
    49020,
    86400,
    100000,
    172800,
    300000,
    31697026,
    63200000,
    95000000,
    999999999,
]
for s in seconds:
    print str(s).rjust(10, ' '), '=>', duration_human(s)

Example Output:
.        1 => 1 second
         2 => 2 seconds
     3600 => 1 hour
     7201 => 2 hours 1 second
     49020 => 13 hours 37 minutes
     86400 => 1 day
    100000 => 1 day 3 hours 46 minutes 40 seconds
    172800 => 2 days
    300000 => 3 days 11 hours 20 minutes
31697026 => 1 year
63200000 => 2 years
95000000 => 3 years
999999999 => 31 years


Example 2:
Python
import time
 
start = time.time()
 
time.sleep(1)
 
end = time.time()
elapsed = end - start
print duration_human(elapsed), 'has passed'


Example 2 Output:
1 second has passed

Comments


  1. good!!

  2. import datetime
    import time

    start = datetime.datetime.now()

    time.sleep(2)

    elapsed = datetime.datetime.now() - start
    print elapsed

  3. remove hash bangs

    #!/usr/bin/python

  4. remove hash bangs

    #!/usr/bin/python

  5. remove hash bangs

    #!/usr/bin/python

  6. remove hash bangs

    #!/usr/bin/python

  7. remove hash bangs

    #!/usr/bin/python

  8. remove hash bangs

    #!/usr/bin/python

  9. remove hash bangs

    #!/usr/bin/python

  10. remove hash bangs

    #!/usr/bin/python

  11. remove hash bangs

    #!/usr/bin/python

  12. remove hash bangs

    #!/usr/bin/python

  13. remove hash bangs

    #!/usr/bin/python

  14. remove hash bangs

    #!/usr/bin/python

  15. remove hash bangs

    #!/usr/bin/python

  16. remove hash bangs

    #!/usr/bin/python