No title

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
data DepartureTime = DayOnly Day | DayWithTime LocalTime deriving (Show)

data DateWidget = DateWidget {
  dwWeekDay :: String
  , dwDate :: String
  , dwTime :: String
}

class DateWidgetable a where
  toDateWidget :: a -> DateWidget 
  toDateWidgetFull :: a -> DateWidget 

instance DateWidgetable DepartureTime where
  toDateWidget dt = toDateWidget' False dt
  toDateWidgetFull dt = toDateWidget' True dt

toDateWidget' :: Bool -> DepartureTime -> DateWidget
toDateWidget' isFull (DayOnly day) = DateWidget (dayString isFull day) (dateString day) ""
toDateWidget' isFull (DayWithTime lt) = DateWidget (dayString isFull lt) (dateString lt) (timeString lt)
dayString :: (FormatTime a) => Bool -> a -> String
dayString isFull = formatTime defaultTimeLocale (if isFull then "%A" else "%a")
dateString :: (FormatTime a) => a -> String
dateString = formatTime defaultTimeLocale "%e %b"
timeString :: LocalTime -> String
timeString = formatTime defaultTimeLocale "%I %M %p"