5cc709f223a041db5c02839fe2c5c4253dd2eeb6
[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         
11         # add: date, date as string, weekday, ... 
12         # (whatever you need!)
13         
14         def __init__(self, type):
15                 Converter.__init__(self, type)
16                 if type == "WithSeconds":
17                         self.type = self.WITH_SECONDS
18                 elif type == "InMinutes":
19                         self.type = self.IN_MINUTES
20                 elif type == "Date":
21                         self.type = self.DATE
22                 else:
23                         self.type = self.DEFAULT
24
25         @cached
26         def getText(self):
27                 time = self.source.time
28                 if time is None:
29                         return ""
30
31                 # handle durations
32                 if self.type == self.IN_MINUTES:
33                         return "%d min" % (time / 60)
34                 
35                 t = localtime(time)
36                 
37                 if self.type == self.WITH_SECONDS:
38                         return "%2d:%02d:%02d" % (t.tm_hour, t.tm_min, t.tm_sec)
39                 elif self.type == self.DEFAULT:
40                         return "%02d:%02d" % (t.tm_hour, t.tm_min)
41                 elif self.type == self.DATE:
42                         return strftime("%A %B %d, %Y", t)
43                 else:
44                         return "???"
45
46         text = property(getText)