Home > front end >  Using Haskell to put timestamp in filenames
Using Haskell to put timestamp in filenames

Time:12-29

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.

CodePudding user response:

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"

CodePudding user response:

In Haskell, you cannot "mix" IO with generic types. There are ways to get around this, though. For example, you could use the following to utilize an IO String as a generic String

myIOThing >>= \regular -> <do_something>

You can find more about it here https://wiki.haskell.org/How_to_get_rid_of_IO#Using_I.2FO_actions_more_directly

  • Related