Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Calculates a diagram of average usage per day from timestamp/value pairs
import qualified Data.ByteString.Char8 as C8
import Data.UnixTime
csvTableToTuple :: String -> (Int, Int)
csvTableToTuple line =
let date = takeWhile (/= ';') line
val = tail $ dropWhile (/= ';') line
in (fromEnum (utSeconds (parseUnixTime (C8.pack "%Y-%m-%d") (C8.pack date))), read val)
secondsInDay :: Int
secondsInDay = 60 * 60 * 24
valDiff :: (Int, Int) -> (Int, Int) -> (Int, Int)
valDiff (a,b) (c,d) =
let dd = (a-c) `div` secondsInDay
in
(dd,(b-d) `div` dd)
outputM :: Int -> Int -> Int -> IO()
outputM _ 0 _ = return()
outputM act repeat diff = do
putStrLn $ (show ts) ++ ", " ++ (show diff)
outputM ts (repeat - 1) diff
where ts = act + secondsInDay
perDay :: (Int, Int) -> [ (Int, Int) ] -> IO()
perDay _ [] = return ()
perDay last remain = do
let next = head remain
(days, diffv) = valDiff next last
outputM (fst last) days diffv
perDay next (tail remain)
main = do
l <- fmap lines getContents
let dat = map csvTableToTuple l
perDay (head dat) (tail dat)
return ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment