-
Notifications
You must be signed in to change notification settings - Fork 0
/
weekly_times.hs
28 lines (18 loc) · 1.22 KB
/
weekly_times.hs
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
{-# OPTIONS_GHC -XFlexibleInstances -XUndecidableInstances -XIncoherentInstances #-}
module WeeklyTimes where
data WeekDay = Sunday | Monday | Tuesday | Wednesday | Thursday | Friday | Saturday deriving (Show, Eq, Enum, Bounded, Ord)
data WeeklyTime = WeeklyTime WeekDay Int Int deriving (Eq, Show)
properWeeklyTime :: WeekDay -> Int -> Int -> Maybe WeeklyTime
properWeeklyTime d h m | h >= 24 || m >= 60 || h < 0 || m < 0 = Nothing
| otherwise = Just (WeeklyTime d h m)
minutesPastSundayMidnight :: WeeklyTime -> Int
minutesPastSundayMidnight (WeeklyTime d h m) = (fromEnum d)*1440+h*60+m
instance Ord WeeklyTime where
compare t1 t2 = compare (minutesPastSundayMidnight t1) (minutesPastSundayMidnight t2)
data WeeklyTimeInterval = WeeklyTimeInterval WeeklyTime WeeklyTime deriving (Show)
properWeeklyTimeInterval :: WeeklyTime -> WeeklyTime -> Maybe WeeklyTimeInterval
properWeeklyTimeInterval t1 t2 | t1 < t2 = Just (WeeklyTimeInterval t1 t2)
| t1 >= t2 = Nothing
intersecting :: WeeklyTimeInterval -> WeeklyTimeInterval -> Bool
intersecting (WeeklyTimeInterval firstStart firstEnd) (WeeklyTimeInterval secondStart secondEnd)
= (firstEnd > secondStart) && (secondEnd > firstStart)