Post-Cancellation, Pre-Nisei Netrunner Rotation
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.7KB

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