8ab741326ebac654317cb1db408bf0694d6feb84
Stefan Schuermans show current position as h:...

Stefan Schuermans authored 11 years ago

1) #! /usr/bin/env python
2) 
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

3) """time format converter
4) 
5) converts between time in seconds as floating point value and time as
6) human-readable string in hours, minutes and seconds
7) 
8) <time string> = ((<hours>:)?<minutes>:)?<seconds>
9) <hours> = [0-9]+
10) <minutes> = [0-9]+
11) <seconds> = [0-9]+(.[0-9]+)"""
12) 
Stefan Schuermans show current position as h:...

Stefan Schuermans authored 11 years ago

13) def sec2str(sec):
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

14)   """convert time in seconds to human-readable time string"""
Stefan Schuermans show current position as h:...

Stefan Schuermans authored 11 years ago

15)   sign = ""
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

16)   sec100 = round(sec * 100)
17)   if sec100 < 0:
Stefan Schuermans show current position as h:...

Stefan Schuermans authored 11 years ago

18)     sign = "-";
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

19)     sec100 = -sec100;
20)   sec1 = sec100 // 100
21)   sec100 = sec100 % 100
Stefan Schuermans show current position as h:...

Stefan Schuermans authored 11 years ago

22)   minu = sec1 // 60
23)   sec1 = sec1 % 60
24)   hour = minu // 60
25)   minu = minu % 60
26)   return "%s%u:%02u:%02u.%02u" % (sign, hour, minu, sec1, sec100)
27) 
Stefan Schuermans reading playlist

Stefan Schuermans authored 11 years ago

28) def str2sec(str):
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

29)   """convert a human readable time string into time in seconds"""
Stefan Schuermans reading playlist

Stefan Schuermans authored 11 years ago

30)   total = 0
31)   section = 0
32)   sign = 1
33)   decimal = 1
34)   for c in str:
35)     if c == ":":
36)       total = (total + sign * section) * 60
37)       section = 0
38)       sign = 1
39)       decimal = 1
40)     elif c == "-":
41)       sign = -sign
42)     elif c == ".":
43)       decimal = 0.1
44)     elif c >= "0" and c <= "9":
45)       if decimal < 1:
46)         section = section + int(c) * decimal
47)         decimal = decimal * 0.1
48)       else:
49)         section = section * 10 + int(c)
50)   total = total + sign * section
51)   return total