From 50b4b4989a8a6ec9ecaad32dda23c6dd8347af4f Mon Sep 17 00:00:00 2001 From: mathieui Date: Tue, 15 Nov 2011 21:21:01 +0100 Subject: Some utility functions to go from/to seconds/strings --- src/common.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'src') 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 + -- cgit v1.2.3