enigma2 20131118 (master) -> 20140214 (master)
[enigma2.git] / usr / lib / enigma2 / 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         
14         # add: date, date as string, weekday, ... 
15         # (whatever you need!)
16         
17         def __init__(self, type):
18                 Converter.__init__(self, type)
19                 if type == "WithSeconds":
20                         self.type = self.WITH_SECONDS
21                 elif type == "InMinutes":
22                         self.type = self.IN_MINUTES
23                 elif type == "Date":
24                         self.type = self.DATE
25                 elif type == "AsLength":
26                         self.type = self.AS_LENGTH
27                 elif type == "Timestamp":       
28                         self.type = self.TIMESTAMP
29                 elif str(type).find("Format") != -1:
30                         self.type = self.FORMAT
31                         self.fmt_string = type[7:]
32                 else:
33                         self.type = self.DEFAULT
34                 self.short_day = { "Mon": _("Mon"), "Tue": _("Tue"), "Wed": _("Wed"), "Thu": _("Thu"), "Fri": _("Fri"), "Sat": _("Sat"), "Sun": _("Sun") }
35                 self.full_day = { "Monday": _("Monday"), "Tuesday": _("Tuesday"), "Wednesday": _("Wednesday"), "Thursday": _("Thursday"), "Friday": _("Friday"), "Saturday": _("Saturday"), "Sunday": _("Sunday") }
36                 self.short_month = { "Jan": _("Jan"), "Feb": _("Feb"), "Mar": _("Mar"), "Apr": _("Apr"), "May": _("May"), "Jun": _("Jun"), "Jul": _("Jul"), "Aug": _("Aug"), "Sep": _("Sep"), "Oct": _("Oct"), "Nov": _("Nov"), "Dec": _("Dec") }
37                 self.full_month = { "January": _("January"), "February": _("February"), "March": _("March"), "April": _("April"), "May": _("May"), "June": _("June"), "July": _("July"), "August": _("August"), "September": _("September"), "October": _("October"), "November": _("November"), "December": _("December") }
38
39         @cached
40         def getText(self):
41                 time = self.source.time
42                 if time is None:
43                         return ""
44
45                 # handle durations
46                 if self.type == self.IN_MINUTES:
47                         return "%d min" % (time / 60)
48                 elif self.type == self.AS_LENGTH:
49                         return "%d:%02d" % (time / 60, time % 60)
50                 elif self.type == self.TIMESTAMP:
51                         return str(time)
52                 
53                 t = localtime(time)
54                 
55                 if self.type == self.WITH_SECONDS:
56                         return "%2d:%02d:%02d" % (t.tm_hour, t.tm_min, t.tm_sec)
57                 elif self.type == self.DEFAULT:
58                         return "%02d:%02d" % (t.tm_hour, t.tm_min)
59                 elif self.type == self.DATE:
60                         line = strftime("%A %B")
61                         line = line.split(" ")
62                         fDay = self.full_day[line[0]]
63                         fMonth = self.full_month[line[1]]
64                         return "%s %s %s" % (fDay, fMonth, strftime("%d, %Y", t))
65
66                 elif self.type == self.FORMAT:
67                         spos = self.fmt_string.find('%')
68                         if spos > -1:
69                                 s1 = self.fmt_string[:spos]
70                                 s2 = strftime(self.fmt_string[spos:], t)
71                                 shortDay = self.fmt_string.find('%a')
72                                 fullDay = self.fmt_string.find('%A')
73                                 shortMonth = self.fmt_string.find('%b')
74                                 fullMonth = self.fmt_string.find('%B')
75                                 line = str(s1+s2)
76                                 if shortDay > -1:
77                                         replaces = self.short_day
78                                         # http://stackoverflow.com/questions/6116978/python-replace-multiple-strings
79                                         line = reduce(lambda inline, outline: inline.replace(*outline), replaces.iteritems(), line)
80                                 if fullDay > -1:
81                                         replaces = self.full_day
82                                         line = reduce(lambda inline, outline: inline.replace(*outline), replaces.iteritems(), line)
83                                 if shortMonth > -1:
84                                         replaces = self.short_month
85                                         line = reduce(lambda inline, outline: inline.replace(*outline), replaces.iteritems(), line)
86                                 if fullMonth > -1:
87                                         replaces = self.full_month
88                                         line = reduce(lambda inline, outline: inline.replace(*outline), replaces.iteritems(), line)
89                                 return line
90                         else:
91                                 return strftime(self.fmt_string, t)
92
93                 else:
94                         return "???"
95
96         text = property(getText)