6d1a6cb3ad2b61c8c417a85ead564d8bf58c3dda
Stefan Schuermans show current position as h:...

Stefan Schuermans authored 10 years ago

1) #! /usr/bin/env python
2) 
Stefan Schuermans comments by ST

Stefan Schuermans authored 10 years ago

3) # BlinkenArea Stage Director
Stefan Schuermans add copyright, remove statu...

Stefan Schuermans authored 10 years ago

4) # Copyright 2013 Stefan Schuermans <stefan@blinkenarea.org>
5) # Copyleft: GNU public license - http://www.gnu.org/copyleft/gpl.html
6) # a blinkenarea.org project - https://www.blinkenarea.org/
7) 
Stefan Schuermans start making player work -...

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

24)     sec100 = -sec100;
25)   sec1 = sec100 // 100
26)   sec100 = sec100 % 100
Stefan Schuermans show current position as h:...

Stefan Schuermans authored 10 years ago

27)   minu = sec1 // 60
28)   sec1 = sec1 % 60
29)   hour = minu // 60
30)   minu = minu % 60
31)   return "%s%u:%02u:%02u.%02u" % (sign, hour, minu, sec1, sec100)
32) 
Stefan Schuermans reading playlist

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

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

Stefan Schuermans authored 10 years ago

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