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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
{-# LANGUAGE OverloadedStrings #-}
module Model.Date
( Date(..)
, getCurrentDate
, getNextWeek
, plusDays
, sameDayAndMonth
, isBeforeOrEqualDayAndMonth
, isAfterOrEqualDayAndMonth
, yearsGap
) where
import Data.Time.Clock
import Data.Time.Calendar
import Data.Time.LocalTime
import qualified Data.Text as T
import Time (formatCurrentLocale)
data Date = Date
{ day :: Int
, month :: Int
, year :: Int
} deriving (Eq, Show)
getCurrentDate :: IO Date
getCurrentDate = do
now <- getCurrentTime
timezone <- getCurrentTimeZone
let zoneNow = utcToLocalTime timezone now
return . dateFromDay $ localDay zoneNow
getNextWeek :: IO (Date, Date)
getNextWeek = do
currentDate <- getCurrentDate
currentDayNumberOfWeek <- (read . T.unpack <$> formatCurrentLocale "%u") :: IO Int
let begin = currentDate `plusDays` (8 - currentDayNumberOfWeek)
let end = begin `plusDays` 6
return (begin, end)
plusDays :: Date -> Int -> Date
plusDays (Date d m y) n =
dateFromDay . addDays (toInteger n) $ fromGregorian (toInteger y) m d
dateFromDay :: Day -> Date
dateFromDay dayTime =
let (y, m, d) = toGregorian dayTime
in Date d m (fromIntegral y)
sameDayAndMonth :: Date -> Date -> Bool
sameDayAndMonth d1 d2 =
( day d1 == day d2
&& month d1 == month d2
)
isBeforeOrEqualDayAndMonth :: Date -> Date -> Bool
isBeforeOrEqualDayAndMonth d1 d2 =
( month d1 < month d2
|| ( month d1 == month d2
&& day d1 <= day d2
)
)
isAfterOrEqualDayAndMonth :: Date -> Date -> Bool
isAfterOrEqualDayAndMonth d1 d2 =
( month d1 > month d2
|| ( month d1 == month d2
&& day d1 >= day d2
)
)
yearsGap :: Date -> Date -> Int
yearsGap d1 d2 = abs (year d2 - year d1)
|