9fefe51f02e7be4f1f74af7fb78cda5303a43928
[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                 if type == "WithSeconds":
26                         self.type = self.WITH_SECONDS
27                 elif type == "InMinutes":
28                         self.type = self.IN_MINUTES
29                 elif type == "Date":
30                         self.type = self.DATE
31                 elif type == "AsLength":
32                         self.type = self.AS_LENGTH
33                 elif type == "AsLengthHours":
34                         self.type = self.AS_LENGTHHOURS
35                 elif type == "AsLengthSeconds":
36                         self.type = self.AS_LENGTHSECONDS
37                 elif type == "Timestamp":
38                         self.type = self.TIMESTAMP
39                 elif type == "Full":
40                         self.type = self.FULL
41                 elif type == "ShortDate":
42                         self.type = self.SHORT_DATE
43                 elif type == "LongDate":
44                         self.type = self.LONG_DATE
45                 elif type == "VFD":
46                         self.type = self.VFD
47                 elif "Format" in type:
48                         self.type = self.FORMAT
49                         self.fmt_string = type[7:]
50                 else:
51                         self.type = self.DEFAULT
52
53         @cached
54         def getText(self):
55                 time = self.source.time
56                 if time is None:
57                         return ""
58
59                 # handle durations
60                 if self.type == self.IN_MINUTES:
61                         return _("%d min") % (time / 60)
62                 elif self.type == self.AS_LENGTH:
63                         if time < 0:
64                                 return ""
65                         return "%d:%02d" % (time / 60, time % 60)
66                 elif self.type == self.AS_LENGTHHOURS:
67                         if time < 0:
68                                 return ""
69                         return "%d:%02d" % (time / 3600, time / 60 % 60)
70                 elif self.type == self.AS_LENGTHSECONDS:
71                         if time < 0:
72                                 return ""
73                         return "%d:%02d:%02d" % (time / 3600, time / 60 % 60, time % 60)
74                 elif self.type == self.TIMESTAMP:
75                         return str(time)
76
77                 t = localtime(time)
78
79                 if self.type == self.WITH_SECONDS:
80                         # TRANSLATORS: full time representation hour:minute:seconds
81                         return _("%2d:%02d:%02d") % (t.tm_hour, t.tm_min, t.tm_sec)
82                 elif self.type == self.DEFAULT:
83                         # TRANSLATORS: short time representation hour:minute
84                         if t.tm_hour < 10:
85                                 return " " + _("%2d:%02d") % (t.tm_hour, t.tm_min)
86                         return _("%2d:%02d") % (t.tm_hour, t.tm_min)
87                 elif self.type == self.DATE:
88                         # TRANSLATORS: full date representation dayname daynum monthname year in strftime() format! See 'man strftime'
89                         d = _("%A %e %B %Y")
90                 elif self.type == self.FULL:
91                         # TRANSLATORS: long date representation short dayname daynum short monthname hour:minute in strftime() format! See 'man strftime'
92                         d = _("%a %e/%m  %-H:%M")
93                 elif self.type == self.SHORT_DATE:
94                         # TRANSLATORS: short date representation short dayname daynum short monthname in strftime() format! See 'man strftime'
95                         d = _("%a %e/%m")
96                 elif self.type == self.LONG_DATE:
97                         # TRANSLATORS: long date representations dayname daynum monthname in strftime() format! See 'man strftime'
98                         d = _("%A %e %B")
99                 elif self.type == self.VFD:
100                         # TRANSLATORS: VFD hour:minute daynum short monthname in strftime() format! See 'man strftime'
101                         d = _("%k:%M %e/%m")
102                 elif self.type == self.FORMAT:
103                         d = self.fmt_string
104                 else:
105                         return "???"
106                 return strftime(d, t)
107
108         text = property(getText)