aboutsummaryrefslogtreecommitdiff
path: root/lib/python/Components/Converter/ClockToText.py
blob: 085ebc1a3a8dc9db95fa0c2063ad150281813838 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from Components.Converter.Converter import Converter
from time import localtime

class ClockToText(Converter, object):
	DEFAULT = 0
	WITH_SECONDS = 1
	IN_MINUTES = 2
	
	# add: date, date as string, weekday, ... 
	# (whatever you need!)
	
	def __init__(self, type, *args, **kwargs):
		Converter.__init__(self)
		if type == "WithSeconds":
			self.type = self.WITH_SECONDS
		elif type == "InMinutes":
			self.type = self.IN_MINUTES
		else:
			self.type = self.DEFAULT

	def getText(self):
		time = self.source.time
		if time is None:
			return ""

		# handle durations
		if self.type == self.IN_MINUTES:
			return "%d min" % (time / 60)
		
		t = localtime(time)
		
		if self.type == self.WITH_SECONDS:
			return "%2d:%02d:%02d" % (t.tm_hour, t.tm_min, t.tm_sec)
		elif self.type == self.DEFAULT:
			return "%02d:%02d" % (t.tm_hour, t.tm_min)
		else:
			return "???"

	text = property(getText)