diff options
author | Micah Lee <micah@micahflee.com> | 2017-05-30 11:44:44 -0700 |
---|---|---|
committer | Micah Lee <micah@micahflee.com> | 2017-05-30 11:44:44 -0700 |
commit | da0a2d19301b1476b2ba2656ac02f054c8ddfce7 (patch) | |
tree | 09ae81875415ea6805b14b279654a79adcfa23fb | |
parent | 3c1a568a6ace9f5614b4d80e37e6dfbde5277697 (diff) | |
parent | 5880741c9da625ac4a3528f4c71444785a3bc5a0 (diff) | |
download | onionshare-da0a2d19301b1476b2ba2656ac02f054c8ddfce7.tar.gz onionshare-da0a2d19301b1476b2ba2656ac02f054c8ddfce7.zip |
Merge branch 'format_seconds' of https://github.com/delirious-lettuce/onionshare into delirious-lettuce-format_seconds
-rw-r--r-- | onionshare/common.py | 33 |
1 files changed, 11 insertions, 22 deletions
diff --git a/onionshare/common.py b/onionshare/common.py index adeae06b..e5aab895 100644 --- a/onionshare/common.py +++ b/onionshare/common.py @@ -175,30 +175,19 @@ def human_readable_filesize(b): def format_seconds(seconds): """Return a human-readable string of the format 1d2h3m4s""" - seconds_in_a_minute = 60 - seconds_in_an_hour = seconds_in_a_minute * 60 - seconds_in_a_day = seconds_in_an_hour * 24 - - days = math.floor(seconds / seconds_in_a_day) - - hour_seconds = seconds % seconds_in_a_day - hours = math.floor(hour_seconds / seconds_in_an_hour) - - minute_seconds = hour_seconds % seconds_in_an_hour - minutes = math.floor(minute_seconds / seconds_in_a_minute) - - remaining_seconds = minute_seconds % seconds_in_a_minute - seconds = math.ceil(remaining_seconds) + days, seconds = divmod(seconds, 86400) + hours, seconds = divmod(seconds, 3600) + minutes, seconds = divmod(seconds, 60) human_readable = [] - if days > 0: - human_readable.append("{}d".format(int(days))) - if hours > 0: - human_readable.append("{}h".format(int(hours))) - if minutes > 0: - human_readable.append("{}m".format(int(minutes))) - if seconds > 0: - human_readable.append("{}s".format(int(seconds))) + if days: + human_readable.append("{:.0f}d".format(days)) + if hours: + human_readable.append("{:.0f}h".format(hours)) + if minutes: + human_readable.append("{:.0f}m".format(minutes)) + if seconds or not human_readable: + human_readable.append("{:.0f}s".format(seconds)) return ''.join(human_readable) |