aboutsummaryrefslogtreecommitdiff
path: root/src/server/Utils/Time.hs
blob: 4a247e9a2b3d91faadabca2eccc9bfb734e15942 (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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
module Utils.Time
  ( belongToCurrentMonth
  , belongToCurrentWeek
  , timeToDay
  , monthToKey
  ) where

import Data.Time.Clock (UTCTime, getCurrentTime)
import Data.Time.LocalTime
import Data.Time.Calendar
import Data.Time.Calendar.WeekDate (toWeekDate)

import Model.Message.Key (Key)
import qualified Model.Message.Key as K

belongToCurrentMonth :: UTCTime -> IO Bool
belongToCurrentMonth time = do
  (timeYear, timeMonth, _) <- toGregorian <$> timeToDay time
  (actualYear, actualMonth, _) <- toGregorian <$> (getCurrentTime >>= timeToDay)
  return (actualYear == timeYear && actualMonth == timeMonth)

belongToCurrentWeek :: UTCTime -> IO Bool
belongToCurrentWeek time = do
  (timeYear, timeWeek, _) <- toWeekDate <$> timeToDay time
  (actualYear, actualWeek, _) <- toWeekDate <$> (getCurrentTime >>= timeToDay)
  return (actualYear == timeYear && actualWeek == timeWeek)

timeToDay :: UTCTime -> IO Day
timeToDay time = localDay . (flip utcToLocalTime time) <$> getTimeZone time

monthToKey :: Int -> Maybe Key
monthToKey 1  = Just K.January
monthToKey 2  = Just K.February
monthToKey 3  = Just K.March
monthToKey 4  = Just K.April
monthToKey 5  = Just K.May
monthToKey 6  = Just K.June
monthToKey 7  = Just K.July
monthToKey 8  = Just K.August
monthToKey 9  = Just K.September
monthToKey 10 = Just K.October
monthToKey 11 = Just K.November
monthToKey 12 = Just K.December
monthToKey _ = Nothing