Adding seconds to UTC time

Hello,

I need to work with timestamp adding/subtracting given number of seconds from it. I chose time library and its UTFTime type to represent a timestamp. Now say I want to add 10 seconds to now. So far I came up with this:

now :: UTCTime
seconds = 10 :: Int
addUTCTime (secondsToNominalDiffTime seconds) now

This does not work becase seconds is Int but secondsToNominalDiffTime expects value of type Pico. (doclink)

I have no idea how to create value of pico type. I feel like I am missing something basic. Can you guys point me in a right direction?

Thanks!

NominalDiffTime has a Num instance that corresponds to seconds. You can use that directly. If you put an explicit time signature you can do it like this:

seconds :: NominalDiffTime
seconds = 10

In many cases the type signature isn’t even needed, e.g. 10 seconds from now:

addUTCTime 10 <$> getCurrentTime :: IO UTCTime

For converting arbitrary numbers (e.g. user input) to NominalDiffTime, you can use fromIntegral (for things like Int) or realToFrac (for things like Float).

1 Like

Being new to haskell I have not thought about that. With your hint I know have this:

translateTimeFilter now (RelTimeFilter n u) = let
  pastSeconds :: NominalDiffTime
  pastSeconds = fromIntegral $ negate $ abs $ toSeconds n u
  pastTime = addUTCTime pastSeconds now

Thanks for your help!

I am probably dumb but I am not able to find a way how to close the thread or mark the post as solution. Either or both…

No worries, Discourse isn’t a Q&A platform so there is not close a thread.

1 Like