servicemp3.cpp: more simple/flexible streaming detection
[enigma2.git] / lib / python / Components / Converter / ClockToText.py
1 from Converter import Converter
2 from time import localtime, strftime
3 from Components.Element import cached
4
5 class ClockToText(Converter, object):
6         DEFAULT = 0
7         WITH_SECONDS = 1
8         IN_MINUTES = 2
9         DATE = 3
10         FORMAT = 4
11         AS_LENGTH = 5
12         TIMESTAMP = 6
13         
14         # add: date, date as string, weekday, ... 
15         # (whatever you need!)
16         
17         def __init__(self, type):
18                 Converter.__init__(self, type)
19                 if type == "WithSeconds":
20                         self.type = self.WITH_SECONDS
21                 elif type == "InMinutes":
22                         self.type = self.IN_MINUTES
23                 elif type == "Date":
24                         self.type = self.DATE
25                 elif type == "AsLength":
26                         self.type = self.AS_LENGTH
27                 elif type == "Timestamp":       
28                         self.type = self.TIMESTAMP
29                 elif str(type).find("Format") != -1:
30                         self.type = self.FORMAT
31                         self.fmt_string = type[7:]
32                 else:
33                         self.type = self.DEFAULT
34
35         @cached
36         def getText(self):
37                 time = self.source.time
38                 if time is None:
39                         return ""
40
41                 # handle durations
42                 if self.type == self.IN_MINUTES:
43                         return "%d min" % (time / 60)
44                 elif self.type == self.AS_LENGTH:
45                         return "%d:%02d" % (time / 60, time % 60)
46                 elif self.type == self.TIMESTAMP:
47                         return str(time)
48                 
49                 t = localtime(time)
50                 
51                 if self.type == self.WITH_SECONDS:
52                         return "%2d:%02d:%02d" % (t.tm_hour, t.tm_min, t.tm_sec)
53                 elif self.type == self.DEFAULT:
54                         return "%02d:%02d" % (t.tm_hour, t.tm_min)
55                 elif self.type == self.DATE:
56                         return strftime("%A %B %d, %Y", t)
57                 elif self.type == self.FORMAT:
58                         spos = self.fmt_string.find('%')
59                         if spos > 0:
60                                 s1 = self.fmt_string[:spos]
61                                 s2 = strftime(self.fmt_string[spos:], t)
62                                 return str(s1+s2)
63                         else:
64                                 return strftime(self.fmt_string, t)
65                 
66                 else:
67                         return "???"
68
69         text = property(getText)