summaryrefslogtreecommitdiff
path: root/src/Util.hs
blob: 95cc4cef9f0fa95d9cd8462b92b41a2903d2e681 (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
45
46
47
48
49
50
51
52
53
54
{-# LANGUAGE DeriveFoldable #-}
module Util (module Util, toList) where

import Data.Foldable (toList)
import Data.Maybe (fromMaybe)
import Data.Time (Day, toGregorian)
import Data.Word (Word8)


data Snoc a = SnocNil | Snoc (Snoc a) a
  deriving (Show, Foldable)

-- | Time-of-day, in unspecified time zone
data YMDHMS = YMDHMS {-# UNPACK #-} !YMD
                     {-# UNPACK #-} !HMS
  deriving (Show)

-- | Calendar day
data YMD = YMD {-# UNPACK #-} !Int
               {-# UNPACK #-} !Word8  -- ^ 1-based
               {-# UNPACK #-} !Word8
  deriving (Show, Eq, Ord)

-- | Time-of-day in seconds, in unspecified time zone
data HMS = HMS {-# UNPACK #-} !Word8
               {-# UNPACK #-} !Word8
               {-# UNPACK #-} !Word8
  deriving (Show)

pad :: Show a => Char -> Int -> a -> String
pad c w val =
  let s = show val
  in replicate (w - length s) c ++ s

uncurry3 :: (a -> b -> c -> d) -> (a, b, c) -> d
uncurry3 f (x, y, z) = f x y z

dropWhileEnd :: (a -> Bool) -> [a] -> [a]
dropWhileEnd p = fromMaybe [] . go
  where
    go [] = Nothing
    go (x:xs) =
      case go xs of
        Nothing | p x -> Nothing
                | otherwise -> Just [x]
        Just xs' -> Just (x : xs')

ymdToString :: YMD -> String
ymdToString (YMD y m d) = pad '0' 4 y ++ '-' : pad '0' 2 m ++ '-' : pad '0' 2 d

dayToYMD :: Day -> YMD
dayToYMD day =
  let (y, m, d) = toGregorian day
  in YMD (fromIntegral y) (fromIntegral m) (fromIntegral d)