aboutsummaryrefslogtreecommitdiff
path: root/src/Date.hs
blob: efbef8cb117ad08c7a74c46d54335f429d980ed2 (plain)
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
module Date
  ( Date(..)
  , getCurrentDate
  , sameDayAndMonth
  , yearsGap
  ) where

import Data.Time.Clock
import Data.Time.Calendar
import Data.Time.LocalTime

data Date = Date
  { year :: Int
  , month :: Int
  , day :: Int
  } deriving (Eq, Show)

getCurrentDate :: IO Date
getCurrentDate = do
  now <- getCurrentTime
  timezone <- getCurrentTimeZone
  let zoneNow = utcToLocalTime timezone now
  let (year, month, day) = toGregorian $ localDay zoneNow
  return $ Date (fromIntegral year) month day

sameDayAndMonth :: Date -> Date -> Bool
sameDayAndMonth (Date _ m1 d1) (Date _ m2 d2) = m1 == m2 && d1 == d2

yearsGap :: Date -> Date -> Int
yearsGap (Date y1 _ _) (Date y2 _ _) = abs (y2 - y1)