将当前时间转换为Int(Haskell)

Joe*_*Joe 2 io time haskell

我一直在寻找年龄来尝试找到一种简单的方法来获取当前时间(可能是从午夜,或小时,分钟,秒的秒数)到int.有人能指出我正确的方向吗?我查看了图书馆并没有成功.

Dan*_*zer 6

为了获得从午夜开始的时间Int非常简单:

import Data.Time.Clock
main = do
  currTime <- getCurrentTime
  let timed = floor $ utctDayTime currTime :: Int
  print timed
Run Code Online (Sandbox Code Playgroud)

对于踢球和咯咯笑,这里有一个快速的功能

integralTime :: (Integral a) => IO a
integralTime = getCurrentTime >>= return.floor.utctDayTime
Run Code Online (Sandbox Code Playgroud)

这是有效的,因为TimeDiff(它是一个RealFrac实例)可以floor编辑成一个整数.

  • 此外,`toRational`不是必需的:`floor`在任何`RealFrac`参数上已经是多态的. (2认同)