diff options
author | Lukas Metzger | 2020-07-02 12:30:15 +0200 |
---|---|---|
committer | Lukas Metzger | 2020-07-02 12:30:15 +0200 |
commit | def8d90e3b7ca51e2a14f0f631b84e4842f04c49 (patch) | |
tree | b8ddc32c784d729a7bf6ec9597cc50a3d8bccad6 /dash/prettytime.py | |
parent | Remove unneccessary debug output (diff) | |
download | bwlp-statistics-def8d90e3b7ca51e2a14f0f631b84e4842f04c49.tar.gz bwlp-statistics-def8d90e3b7ca51e2a14f0f631b84e4842f04c49.tar.xz bwlp-statistics-def8d90e3b7ca51e2a14f0f631b84e4842f04c49.zip |
Added prettytime function and use it
Diffstat (limited to 'dash/prettytime.py')
-rw-r--r-- | dash/prettytime.py | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/dash/prettytime.py b/dash/prettytime.py new file mode 100644 index 0000000..6e9e562 --- /dev/null +++ b/dash/prettytime.py @@ -0,0 +1,17 @@ +#!/usr/bin/env python3 + +def prettytime(seconds, prec = 2): + seconds = int(seconds) + output = [] + + for div, unit in [(365*86400, 'year'), (30*86400, 'month'), (86400, 'day'), (3600, 'hour'), (60, 'minute'), (1, 'second')]: + time = seconds // div + seconds = seconds % div + if time > 0: + if time > 1: + output.append('{} {}s'.format(time, unit)) + else: + output.append('{} {}'.format(time, unit)) + + return ', '.join(output[:prec]) + |