如何使用 Data.Time.Clock 在 Haskell 中获取系统时间?

发布于 2024-08-19 02:27:14 字数 525 浏览 5 评论 0原文

我需要一些 Int 来用作随机数生成的种子,因此我想使用使用系统时间作为种子的老技巧。

因此,我尝试使用 Data.Time 包,并设法执行以下操作:

import Data.Time.Clock

time = getCurrentTime >>= return . utctDayTime

当我运行时,我得到如下信息:

Prelude Data.Time.Clock> time
55712.00536s

time 的类型是 IO DiffTime。我期望看到一个 IO Something 类型,因为这取决于程序外部的事物。所以我有两个问题:

a)是否有可能以某种方式解开 IO 并获取底层 DiffTime 值?

b) 如何将 DiffTime 转换为整数(以秒为单位)?有一个函数 secondsToDiffTime 但我找不到它的逆函数。

I'm needing some Ints to use as seed to random number generation and so I wanted to use the old trick of using the system time as seed.

So I tried to use the Data.Time package and I managed to do the following:

import Data.Time.Clock

time = getCurrentTime >>= return . utctDayTime

When I run time I get things like:

Prelude Data.Time.Clock> time
55712.00536s

The type of time is IO DiffTime. I expected to see an IO Something type as this depends on things external to the program. So I have two questions:

a) Is it possible to somehow unwrap the IO and get the underlying DiffTime value?

b) How do I convert a DiffTime to an integer with it's value in seconds? There's a function secondsToDiffTime but I couldn't find its inverse.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

南城旧梦 2024-08-26 02:27:14

是否可以以某种方式解开 IO 并获取底层 DiffTime 值?

是的。有许多关于 monad 的教程解释了如何操作。它们都基于这样的想法:您编写一个函数,该函数接受 DiffTime 并执行某些操作(例如返回 IO () )或仅返回一个回答。因此,如果您有 f :: DiffTime ->回答,你写的

time >>= \t -> return (f t)

是一些人更喜欢写的内容

time >>= (return . f) 

,如果你有continue :: DiffTime - > IO () 你有

time >>= continue

或者你可能更喜欢 do 表示法:

do { t <- time
   ; continue t  -- or possibly return (f t)
   }

有关更多信息,请参阅有关 monad 的众多优秀教程之一。

Is it possible to somehow unwrap the IO and get the underlying DiffTime value?

Yes. There are dozens of tutorials on monads which explain how. They are all based on the idea that you write a function that takes DiffTime and does something (say returning IO ()) or just returns an Answer. So if you have f :: DiffTime -> Answer, you write

time >>= \t -> return (f t)

which some people would prefer to write

time >>= (return . f) 

and if you have continue :: DiffTime -> IO () you have

time >>= continue

Or you might prefer do notation:

do { t <- time
   ; continue t  -- or possibly return (f t)
   }

For more, consult one of the many fine tutorals on monads.

度的依靠╰つ 2024-08-26 02:27:14

a) 当然可以获取DiffTime值;否则这个功能就没有意义了。您需要阅读有关 monad 的内容。 本章Real World Haskell 的下一个 有很好的介绍。

b) DiffTime 的文档说它是 Real 类的实例,即它可以被视为实数,在本例中为秒数。因此,将其转换为秒是链接转换函数的简单问题:

diffTimeToSeconds :: DiffTime -> Integer
diffTimeToSeconds = floor . toRational

a) Of course it is possible to get the DiffTime value; otherwise, this function would be rather pointless. You'll need to read up on monads. This chapter and the next of Real World Haskell has a good introduction.

b) The docs for DiffTime say that it's an instance of the Real class, i.e. it can be treated as a real number, in this case the number of seconds. Converting it to seconds is thus a simple matter of chaining conversion functions:

diffTimeToSeconds :: DiffTime -> Integer
diffTimeToSeconds = floor . toRational
小耗子 2024-08-26 02:27:14

如果您计划使用标准 System.Random 模块来生成随机数,那么已经有一个为您初始化了时间相关种子的生成器:您可以通过调用 getStdGen 来获取它:: IO 标准生成。 (当然,您仍然需要问题(a)部分的答案才能使用结果。)

If you are planning to use the standard System.Random module for random number generation, then there is already a generator with a time-dependent seed initialized for you: you can get it by calling getStdGen :: IO StdGen. (Of course, you still need the answer to part (a) of your question to use the result.)

有深☉意 2024-08-26 02:27:14

这个功能并不完全是OP所要求的。但它很有用:

λ: import Data.Time.Clock
λ: let getSeconds = getCurrentTime >>= return . fromRational . toRational . utctDayTime
λ: :i getSeconds 
getSeconds :: IO Double     -- Defined at <interactive>:56:5
λ: getSeconds 
57577.607162
λ: getSeconds 
57578.902397
λ: getSeconds 
57580.387334

This function is not exactly what the OP asks. But it's useful:

λ: import Data.Time.Clock
λ: let getSeconds = getCurrentTime >>= return . fromRational . toRational . utctDayTime
λ: :i getSeconds 
getSeconds :: IO Double     -- Defined at <interactive>:56:5
λ: getSeconds 
57577.607162
λ: getSeconds 
57578.902397
λ: getSeconds 
57580.387334
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文