Skip to content

Instantly share code, notes, and snippets.

@tfausak
Last active October 5, 2017 14:31
Show Gist options
  • Save tfausak/3cb0eef697726f302adb599120c7988a to your computer and use it in GitHub Desktop.
Save tfausak/3cb0eef697726f302adb599120c7988a to your computer and use it in GitHub Desktop.
Adds the time to every line.
#!/usr/bin/env stack
-- stack --resolver ghc-8.2.1 script --package time
import Data.Time
import Data.IORef
import Text.Printf
main :: IO ()
main = do
start <- getCurrentTime
before <- newIORef start
contents <- getContents
mapM_ (decorate start before) (lines contents)
decorate :: UTCTime -> IORef UTCTime -> String -> IO ()
decorate start before line = do
now <- getCurrentTime
let elapsed = diffUTCTime now start
step <- atomicModifyIORef before (getStep now)
printf "%s %f %f %s\n" (iso8601 now) (toDouble elapsed) (toDouble step) line
getStep :: UTCTime -> UTCTime -> (UTCTime, NominalDiffTime)
getStep now before = (now, diffUTCTime now before)
iso8601 :: UTCTime -> String
iso8601 = formatTime defaultTimeLocale "%Y-%m-%dT%H:%M:%S.%q%z"
toDouble :: NominalDiffTime -> Double
toDouble = realToFrac
$ echo -e "hello\nworld" | ./chrono-legionnaire.hs
2017-10-05T14:30:11.137545000000+0000 0.00024 0.00024 hello
2017-10-05T14:30:11.137874000000+0000 0.000569 0.000329 world
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment