3abfffc507c093412549fc14583b2ebce84aa594
[enigma2.git] / lib / python / Components / Converter / ClockToText.py
1 from Converter import Converter
2 from time import localtime, strftime
3
4 class ClockToText(Converter, object):
5         DEFAULT = 0
6         WITH_SECONDS = 1
7         IN_MINUTES = 2
8         DATE = 3
9         
10         # add: date, date as string, weekday, ... 
11         # (whatever you need!)
12         
13         def __init__(self, type, *args, **kwargs):
14                 Converter.__init__(self)
15                 if type == "WithSeconds":
16                         self.type = self.WITH_SECONDS
17                 elif type == "InMinutes":
18                         self.type = self.IN_MINUTES
19                 elif type == "Date":
20                         self.type = self.DATE
21                 else:
22                         self.type = self.DEFAULT
23
24         def getText(self):
25                 time = self.source.time
26                 if time is None:
27                         return ""
28
29                 # handle durations
30                 if self.type == self.IN_MINUTES:
31                         return "%d min" % (time / 60)
32                 
33                 t = localtime(time)
34                 
35                 if self.type == self.WITH_SECONDS:
36                         return "%2d:%02d:%02d" % (t.tm_hour, t.tm_min, t.tm_sec)
37                 elif self.type == self.DEFAULT:
38                         return "%02d:%02d" % (t.tm_hour, t.tm_min)
39                 elif self.type == self.DATE:
40                         return strftime("%A %B %d, %Y", t)
41                 else:
42                         return "???"
43
44         text = property(getText)