I'm trying to learn IHP by following the guide. And what I've noticed, that there aren't any functions and other stuff to deal with dates.
For example:
I wanted to change a date of a post to 'NOW()' when updating it
action UpdatePostAction { postId } = do
post <- fetch postId
post
|> buildPost
|> validateIsUnique #title
>>= ifValid \case
Left post -> render EditView { .. }
Right post -> do
post <- post
|> set #created_at '...' -- change '...' to something :)
|> updateRecord
let t = get #title post
setSuccessMessage $ "Post \'" <> t <> "\' updated"
redirectTo PostsAction
So that is the question: What i can do with this 'set-statement' to actually change the date ? Or is there any 'already-specified' functions to do it ?
CodePudding user response:
To handle dates and times in IHP we use the time
package.
To get the current time, there's a simple function from Data.Time.Clock
named getCurrentTime :: IO UTCTime
.
Right post -> do
currentTime <- getCurrentTime
post <- post
|> set #createdAt currentTime
|> updateRecord
...
William Yao has an excellent cheatsheet to the time package which I would highly recommend if you wish to dive deeper into the topic. I pretty much always have a tab to this open!
Also, in calls to set
make sure you use camelCase, IHP converts this into the snake_case database columns for you :)