module Data.Microwave (Microwave) where
newtype Microwave = Microwave {fromMicrowave :: Integer}
instance Show Microwave where
show (Microwave n) = let (mins, secs) = n `quotRem` 100
secs' = case (show secs) of
[c] -> '0':[c]
s -> s
in show mins ++ ":" ++ secs'
instance Read Microwave where
readsPrec n s = let s' = filter (/= ':') s in map (\(n, r) -> (fixSecs n, r)) $ readsPrec n s'
fixSecs n = let (mins, secs) = n `quotRem` 100
(carry, secs') = secs `quotRem` 60
in Microwave (100 * (mins + carry) + secs')
normalize (Microwave n) = let (mins, secs) = n `quotRem` 100 in 60 * mins + secs
denormalize n = let (mins, secs) = n `quotRem` 60 in Microwave (100 * mins + secs)
instance Eq Microwave where
n == m = normalize n == normalize m
instance Ord Microwave where
n `compare` m = normalize n `compare` normalize m
instance Enum Microwave where
fromEnum = fromInteger . normalize
toEnum = denormalize . toInteger
instance Num Microwave where
fromInteger = fixSecs
n + m = denormalize (normalize n + normalize m)
n * m = denormalize (normalize n * normalize m)
abs = Microwave . abs . fromMicrowave
signum = Microwave . signum . fromMicrowave
instance Real Microwave where
toRational = toRational . normalize
instance Integral Microwave where
toInteger = normalize
quotRem n m = let (q, r) = quotRem (normalize n) (normalize m) in (denormalize q, denormalize r)