如何使用 Data.Time.Clock 在 Haskell 中获取系统时间?
我需要一些 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 Int
s 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的。有许多关于 monad 的教程解释了如何操作。它们都基于这样的想法:您编写一个函数,该函数接受 DiffTime 并执行某些操作(例如返回 IO () )或仅返回一个
回答
。因此,如果您有f :: DiffTime ->回答
,你写的是一些人更喜欢写的内容
,如果你有
continue :: DiffTime - > IO ()
你有或者你可能更喜欢
do
表示法:有关更多信息,请参阅有关 monad 的众多优秀教程之一。
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 returningIO ()
) or just returns anAnswer
. So if you havef :: DiffTime -> Answer
, you writewhich some people would prefer to write
and if you have
continue :: DiffTime -> IO ()
you haveOr you might prefer
do
notation:For more, consult one of the many fine tutorals on monads.
a) 当然可以获取
DiffTime
值;否则这个功能就没有意义了。您需要阅读有关 monad 的内容。 本章和Real World Haskell 的下一个 有很好的介绍。b)
DiffTime
的文档说它是Real
类的实例,即它可以被视为实数,在本例中为秒数。因此,将其转换为秒是链接转换函数的简单问题: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 theReal
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:如果您计划使用标准 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 callinggetStdGen :: IO StdGen
. (Of course, you still need the answer to part (a) of your question to use the result.)这个功能并不完全是OP所要求的。但它很有用:
This function is not exactly what the OP asks. But it's useful: