haskell / time

A time library
http://hackage.haskell.org/package/time
Other
118 stars 78 forks source link

instance Random DiffTime #250

Closed domenkozar closed 7 months ago

domenkozar commented 7 months ago
instance Random DiffTime where
  random :: (RandomGen g) => g -> (DiffTime, g)
  random g =
    let (i, g') = random g
     in (picosecondsToDiffTime $ fromIntegral (i :: Integer), g')

  randomR :: RandomGen g => (DiffTime, DiffTime) -> g -> (DiffTime, g)
  randomR (a, b) g =
    let (i, g') = randomR (diffTimeToPicoseconds a, diffTimeToPicoseconds b) g
     in (picosecondsToDiffTime i, g')
AshleyYakeley commented 7 months ago

Hello, what's the issue here?

domenkozar commented 7 months ago

I'm in a need of such instance, so I figured I'd open an issue.

AshleyYakeley commented 7 months ago

That this should be added to the time library, is that correct?

This means taking a package dependency on random -- this might be OK but I need to check.

AshleyYakeley commented 7 months ago

Currently random depends on splitmix which depends on time, so the code can't go here. You'll have to put in the random package instead.

Daniel-Diaz commented 7 months ago

Currently random depends on splitmix which depends on time, so the code can't go here. You'll have to put in the random package instead.

I believe this is only the case for non-ghc compilers. I think they are trying to avoid depending on time there.

domenkozar commented 7 months ago

https://github.com/haskell/random/issues/156

Daniel-Diaz commented 7 months ago

For reference:

https://github.com/haskellari/splitmix/blob/799700374ec214bf9a87ede1758ec73be676ed22/splitmix.cabal#L89-L108

Bodigrim commented 7 months ago

time is a GHC boot package and should not depend on non-boot packages (such as random).