p7/Timestamp.hs

60 lines
1.7 KiB
Haskell
Raw Normal View History

2018-06-24 17:26:43 -04:00
module Timestamp where
2018-10-16 18:30:33 -04:00
{---
- Timestamp Module
-
- Functions related to getting and using timestamps.
-
- Shaun Kerr
-}
import Data.Time.LocalTime
import Data.Time.Format
-- Internal Date Representation
2018-06-24 19:21:29 -04:00
data Timestamp = Ts Integer Integer Integer deriving Show
2018-10-16 18:30:33 -04:00
-- Previews Start on the 20th
2018-06-24 17:26:43 -04:00
isPreviewSeason :: Timestamp -> Bool
isPreviewSeason (Ts x _ _) = x >= 20
2018-10-16 18:30:33 -04:00
-- True // False if first date is in the future
-- relative to the second date.
2018-06-24 17:26:43 -04:00
inFuture :: Timestamp -> Timestamp -> Bool
inFuture (Ts d1 m1 y1) (Ts d2 m2 y2)
| y1 /= y2 = y1 > y2
| m1 /= m2 = m1 > m2
| d1 /= d2 = d1 > d2
| otherwise = False
2018-10-16 18:30:33 -04:00
-- How many months in the future the first date is
-- relative from the second date.
-- returns 0 otherwise.
2018-06-24 17:26:43 -04:00
monthsSince :: Timestamp -> Timestamp -> Integer
monthsSince (Ts d1 m1 y1) (Ts d2 m2 y2)
| t1 `inFuture` t2 = (12 * (y1 - y2)) + (m1 - m2)
| otherwise = 0
where
t1 = Ts d1 m1 y1
t2 = Ts d2 m2 y2
2018-07-01 16:47:13 -04:00
2018-10-16 18:30:33 -04:00
-- Helper, format Integer representation to Timestamp
2018-07-01 18:41:33 -04:00
toTS :: (Integer, Integer, Integer) -> Timestamp
toTS (y,m,d) = Ts d m y
2018-09-16 22:39:15 -04:00
2018-10-16 18:30:33 -04:00
-- IO Function, get the current date as a [Char]
2018-09-16 22:39:15 -04:00
getCurrentTime :: IO [Char]
getCurrentTime = getZonedTime
>>= return . (formatTime defaultTimeLocale "%Y %m %d")
2018-10-16 18:30:33 -04:00
-- Helper, format the [Char] as triple Integer
2018-09-16 22:39:15 -04:00
fmtCurrentTime :: [Char] -> (Integer, Integer, Integer)
fmtCurrentTime n = (\[a,b,c] -> (a,b,c)) iTime
where
iTime = map (\x -> read x :: Integer) $ words n
2018-10-16 18:30:33 -04:00
-- Hide away some ugly steps. Not sure we ever
-- use the other two so can probably compact this.
getTimestamp :: [Char] -> Timestamp
getTimestamp x = toTS . fmtCurrentTime $ x