diff options
author | Joris | 2019-09-05 21:48:06 +0200 |
---|---|---|
committer | Joris | 2019-09-05 21:48:06 +0200 |
commit | 91419f240b788339c202e0d35ef1df3cae64216b (patch) | |
tree | 99da65a72dee71e993d30ff967fa60c4b3fa0c04 /src/lib | |
parent | 317e7b1e7319182e5caa5169119aea9fc8d660b6 (diff) |
Add noise to sleep duration
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/haskell/Utils/Time.hs | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/src/lib/haskell/Utils/Time.hs b/src/lib/haskell/Utils/Time.hs index 88aeeb6..ce36293 100644 --- a/src/lib/haskell/Utils/Time.hs +++ b/src/lib/haskell/Utils/Time.hs @@ -1,6 +1,7 @@ module Utils.Time ( getCurrentFormattedTime , asleepDuration + , addNoise ) where import Data.Text (Text) @@ -9,6 +10,7 @@ import Data.Time.Clock (DiffTime) import qualified Data.Time.Clock as Clock import qualified Data.Time.Format as Format import qualified Data.Time.LocalTime as LocalTime +import qualified System.Random as Random getCurrentFormattedTime :: IO Text getCurrentFormattedTime = do @@ -27,3 +29,13 @@ asleepDuration from to t = Nothing where day = (realToFrac Clock.nominalDay) :: DiffTime + +-- |Add noise to a duration. +-- > t - (n / 2) <= addNoise t n <= t + (n / 2) +addNoise :: DiffTime -> DiffTime -> IO DiffTime +addNoise t n = do + r <- Random.randomIO :: IO Float + return $ t + (Clock.secondsToDiffTime . round $ (r * (fromIntegral . toSeconds $ n))) - n / 2 + + where + toSeconds dt = Clock.diffTimeToPicoseconds dt `div` 10^(12 :: Integer) |