haskellxmonad

Using Haskell to put timestamp in filenames


Long story short: I am using XMonad and trying to put dynamic timestamps on screen-recordings.

I could have cheated and solved this by making it a bash script that easily lets you use changing timestamps, but I figured it was a good opportunity to dig into meat of Haskell.

The code I have tried to use:

import Data.Time
timeStamp = formatTime defaultTimeLocale "%Y-%m-%d—%H:%M:%S" <$> getCurrentTime

then in keybindings I have put

        , ("M-<Print>", spawn $ "giph -f 60 -y -s  ~/recordings/" ++ timeStamp ++ ".mp4")

which give me the error of

 • Couldn't match expected type ‘[Char]’ with actual type ‘IO String’

I figured this is related to Haskell not letting its variables change value, but I have no idea how to work around this or how to rewrite it so that I wouldn't need to work around it.


Solution

  • With = you have just defined another function. Use <- to bind the result of a monadic action to a variable:

    timeStamp <- formatTime defaultTimeLocale "%Y-%m-%d—%H:%M:%S" <$> getCurrentTime
    
    -- ...
    
    "~/recordings/" ++ timeStamp ++ ".mp4"