ClockToText - optional parameter for adding or remove initial space when is used...
[openblackhole/openblackhole-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         FULL = 7
14         SHORT_DATE = 8
15         LONG_DATE = 9
16         VFD = 10
17         AS_LENGTHHOURS = 11
18         AS_LENGTHSECONDS = 12
19
20         # add: date, date as string, weekday, ...
21         # (whatever you need!)
22
23         def __init__(self, type):
24                 Converter.__init__(self, type)
25
26                 self.fix = ""
27                 if ';' in type:
28                         type, self.fix = type.split(';')
29
30                 if type == "WithSeconds":
31                         self.type = self.WITH_SECONDS
32                 elif type == "InMinutes":
33                         self.type = self.IN_MINUTES
34                 elif type == "Date":
35                         self.type = self.DATE
36                 elif type == "AsLength":
37                         self.type = self.AS_LENGTH
38                 elif type == "AsLengthHours":
39                         self.type = self.AS_LENGTHHOURS
40                 elif type == "AsLengthSeconds":
41                         self.type = self.AS_LENGTHSECONDS
42                 elif type == "Timestamp":
43                         self.type = self.TIMESTAMP
44                 elif type == "Full":
45                         self.type = self.FULL
46                 elif type == "ShortDate":
47                         self.type = self.SHORT_DATE
48                 elif type == "LongDate":
49                         self.type = self.LONG_DATE
50                 elif type == "VFD":
51                         self.type = self.VFD
52                 elif "Format" in type:
53                         self.type = self.FORMAT
54                         self.fmt_string = type[7:]
55                 else:
56                         self.type = self.DEFAULT
57
58         @cached
59         def getText(self):
60                 time = self.source.time
61                 if time is None:
62                         return ""
63
64                 # add/remove 1st space
65                 def fix_space(string):
66                         if "Proportional" in self.fix and t.tm_hour < 10:
67                                 return " " + string
68                         if "NoSpace" in self.fix:
69                                 return string.lstrip(' ')
70                         return string
71
72                 # handle durations
73                 if self.type == self.IN_MINUTES:
74                         return _("%d min") % (time / 60)
75                 elif self.type == self.AS_LENGTH:
76                         if time < 0:
77                                 return ""
78                         return "%d:%02d" % (time / 60, time % 60)
79                 elif self.type == self.AS_LENGTHHOURS:
80                         if time < 0:
81                                 return ""
82                         return "%d:%02d" % (time / 3600, time / 60 % 60)
83                 elif self.type == self.AS_LENGTHSECONDS:
84                         if time < 0:
85                                 return ""
86                         return "%d:%02d:%02d" % (time / 3600, time / 60 % 60, time % 60)
87                 elif self.type == self.TIMESTAMP:
88                         return str(time)
89
90                 t = localtime(time)
91
92                 if self.type == self.WITH_SECONDS:
93                         # TRANSLATORS: full time representation hour:minute:seconds
94                         return fix_space(_("%2d:%02d:%02d") % (t.tm_hour, t.tm_min, t.tm_sec))
95                 elif self.type == self.DEFAULT:
96                         # TRANSLATORS: short time representation hour:minute
97                         return fix_space(_("%2d:%02d") % (t.tm_hour, t.tm_min))
98                 elif self.type == self.DATE:
99                         # TRANSLATORS: full date representation dayname daynum monthname year in strftime() format! See 'man strftime'
100                         d = _("%A %e %B %Y")
101                 elif self.type == self.FULL:
102                         # TRANSLATORS: long date representation short dayname daynum short monthname hour:minute in strftime() format! See 'man strftime'
103                         d = _("%a %e/%m  %-H:%M")
104                 elif self.type == self.SHORT_DATE:
105                         # TRANSLATORS: short date representation short dayname daynum short monthname in strftime() format! See 'man strftime'
106                         d = _("%a %e/%m")
107                 elif self.type == self.LONG_DATE:
108                         # TRANSLATORS: long date representations dayname daynum monthname in strftime() format! See 'man strftime'
109                         d = _("%A %e %B")
110                 elif self.type == self.VFD:
111                         # TRANSLATORS: VFD hour:minute daynum short monthname in strftime() format! See 'man strftime'
112                         d = _("%k:%M %e/%m")
113                 elif self.type == self.FORMAT:
114                         d = self.fmt_string
115                 else:
116                         return "???"
117                 return strftime(d, t)
118
119         text = property(getText)