diff options
author | mathieui <mathieui@mathieui.net> | 2011-11-15 21:21:01 +0100 |
---|---|---|
committer | mathieui <mathieui@mathieui.net> | 2011-11-15 21:21:01 +0100 |
commit | 50b4b4989a8a6ec9ecaad32dda23c6dd8347af4f (patch) | |
tree | f4f4efab1760f1bf002fde019a21dff496bb23b8 | |
parent | 5c47b735b360323421b43c71761c90de74c73389 (diff) | |
download | poezio-50b4b4989a8a6ec9ecaad32dda23c6dd8347af4f.tar.gz poezio-50b4b4989a8a6ec9ecaad32dda23c6dd8347af4f.tar.bz2 poezio-50b4b4989a8a6ec9ecaad32dda23c6dd8347af4f.tar.xz poezio-50b4b4989a8a6ec9ecaad32dda23c6dd8347af4f.zip |
Some utility functions to go from/to seconds/strings
-rw-r--r-- | src/common.py | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/common.py b/src/common.py index 35fd08da..226780bc 100644 --- a/src/common.py +++ b/src/common.py @@ -16,6 +16,7 @@ import mimetypes import hashlib import subprocess import time +import string import shlex from config import config @@ -191,3 +192,34 @@ def replace_key_with_bound(key): return config.get(key, key, 'bindings') else: return key + +def parse_str_to_secs(duration=''): + values = {'m': 60, 'h': 3600, 'd': 86400} + result = 0 + tmp = '0' + for char in duration: + if char in string.digits: + tmp += char + elif char in values: + tmp_i = int(tmp) + result += tmp_i * values[char] + tmp = '0' + else: + result += int(tmp) + return result + +def parse_secs_to_str(duration=0): + values = {60: 'm', 3600:'h', 86400:'d'} + secs, mins, hours, days = 0, 0, 0, 0 + result = '' + secs = duration % 60 + mins = (duration % 3600) // 60 + hours = (duration % 86400) // 3600 + days = duration // 86400 + + result += '%sd' % days if days else '' + result += '%sh' % hours if hours else '' + result += '%sm' % mins if mins else '' + result += '%ss' % secs if secs else '' + return result + |