add converter to test config entrie values
[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         
13         # add: date, date as string, weekday, ... 
14         # (whatever you need!)
15         
16         def __init__(self, type):
17                 Converter.__init__(self, type)
18                 if type == "WithSeconds":
19                         self.type = self.WITH_SECONDS
20                 elif type == "InMinutes":
21                         self.type = self.IN_MINUTES
22                 elif type == "Date":
23                         self.type = self.DATE
24                 elif type == "AsLength":
25                         self.type = self.AS_LENGTH
26                 elif str(type).find("Format") != -1:
27                         self.type = self.FORMAT
28                         self.fmt_string = type[7:]
29                 else:
30                         self.type = self.DEFAULT
31
32         @cached
33         def getText(self):
34                 time = self.source.time
35                 if time is None:
36                         return ""
37
38                 # handle durations
39                 if self.type == self.IN_MINUTES:
40                         return "%d min" % (time / 60)
41                 elif self.type == self.AS_LENGTH:
42                         return "%d:%02d" % (time / 60, time % 60)
43                 
44                 t = localtime(time)
45                 
46                 if self.type == self.WITH_SECONDS:
47                         return "%2d:%02d:%02d" % (t.tm_hour, t.tm_min, t.tm_sec)
48                 elif self.type == self.DEFAULT:
49                         return "%02d:%02d" % (t.tm_hour, t.tm_min)
50                 elif self.type == self.DATE:
51                         return strftime("%A %B %d, %Y", t)
52                 elif self.type == self.FORMAT:
53                         spos = self.fmt_string.find('%')
54                         if spos > 0:
55                                 s1 = self.fmt_string[:spos]
56                                 s2 = strftime(self.fmt_string[spos:], t)
57                                 return str(s1+s2)
58                         else:
59                                 return strftime(self.fmt_string, t)
60                 else:
61                         return "???"
62
63         text = property(getText)