Created
September 30, 2017 19:47
-
-
Save develmaycare/23a662e52bd55429b1e6bcfe358ca40c to your computer and use it in GitHub Desktop.
Human Friendly Duration Display
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Avoid magic numbers. | |
seconds_per_hour = 3600 | |
minutes_per_hour = 60 | |
# Duration is a total number of seconds, but get be derived from timedelta.total_seconds(). | |
t = timedelta(hours=2, minutes=15) | |
total_seconds = t.total_seconds() | |
# Store output in a list. | |
a = list() | |
# Calculate the number of hours. | |
hours = int(round(seconds / seconds_per_hour, 0)) | |
if hours == 0: | |
pass | |
elif hours == 1: | |
a.append("%s hour" % hours) | |
else: | |
a.append("%s hours" % hours) | |
# Calculate the number of minutes. | |
minutes = int(round(seconds % seconds_per_hour / minutes_per_hour, 0)) | |
if minutes == 0: | |
pass | |
elif minutes == 1: | |
a.append("%s minute" % minutes) | |
else: | |
a.append("%s minutes" % minutes) | |
# Output the duration. | |
print(" ".join(a) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment