BlinkenArea - GitList
Repositories
Blog
Wiki
stage_director
Code
Commits
Branches
Tags
Search
Tree:
6b79f33
Branches
Tags
master
stage_director
time_fmt.py
allow names for STOP playlist entries avoid crashing when file to open cannot be read get rid of last PyGtk2 remains
Stefan Schuermans
commited
6b79f33
at 2014-01-15 21:52:48
time_fmt.py
Blame
History
Raw
#! /usr/bin/env python # BlinkenArea Stage Director # Copyright 2013-2014 Stefan Schuermans <stefan@blinkenarea.org> # Copyleft: GNU public license - http://www.gnu.org/copyleft/gpl.html # a blinkenarea.org project - https://www.blinkenarea.org/ """time format converter converts between time in seconds as floating point value and time as human-readable string in hours, minutes and seconds <time string> = ((<hours>:)?<minutes>:)?<seconds> <hours> = [0-9]+ <minutes> = [0-9]+ <seconds> = [0-9]+(.[0-9]+)""" def sec2str(sec): """convert time in seconds to human-readable time string""" sign = "" sec100 = round(sec * 100) if sec100 < 0: sign = "-"; sec100 = -sec100; sec1 = sec100 // 100 sec100 = sec100 % 100 minu = sec1 // 60 sec1 = sec1 % 60 hour = minu // 60 minu = minu % 60 return "%s%u:%02u:%02u.%02u" % (sign, hour, minu, sec1, sec100) def str2sec(str): """convert a human readable time string into time in seconds""" total = 0 section = 0 sign = 1 decimal = 1 for c in str: if c == ":": total = (total + sign * section) * 60 section = 0 sign = 1 decimal = 1 elif c == "-": sign = -sign elif c == ".": decimal = 0.1 elif c >= "0" and c <= "9": if decimal < 1: section = section + int(c) * decimal decimal = decimal * 0.1 else: section = section * 10 + int(c) total = total + sign * section return total