summaryrefslogtreecommitdiff
path: root/week2/mart/StdT.icl
blob: 01bee7d645b62927330eeddc75bddb8301273ee1 (plain) (blame)
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
implementation module StdT

import StdEnv

::	T = {m :: Int, s :: Int}

instance ==	T where
	== a b = a.m == b.m && a.s == b.s
instance <	T where
	< a b = a.m < b.m || a.s == b.s && a.s < b.s

instance zero	T where
	zero = {m=zero, s=zero}
instance +	T where
	+ a b = fromInt (toInt a + toInt b)
instance -	T where
	- a b = fromInt (toInt a - toInt b)

instance toInt	T where
	toInt a = a.m*60 + a.s
instance fromInt	T where
	fromInt a
	| a<0 = zero
	| otherwise = {m=a/60, s=a rem 60}

instance toString	T where
	toString {m=ms, s=0} = toString ms +++ ":00"
	toString {m=ms, s=ss}
	| ss < 10 = toString ms +++ ":0" +++ toString ss
	| otherwise = toString ms +++ ":" +++ toString ss

instance fromString	T where
	fromString a
	| a.[size a - 3] == ':' = {m = toInt (a % (0, (size a) - 4)), s = toInt (a % ((size a) - 2, size a))}
	| otherwise = zero