2013-03-24 137 views
2

我一直在寻找年龄段,试图找到一种简单的方法将当前时间(可能是从午夜或小时,分钟,秒开始的秒数)转换为int。 任何人都可以指向正确的方向吗?我看过图书馆,没有成功。将当前时间转换为Int(Haskell)

+0

我不计划在Haskell,但一直在寻找其他语言的类似信息。如何到秒? http://hackage.haskell.org/packages/archive/datetime/0.1/doc/html/Data-DateTime.html – Tim 2013-03-24 16:43:42

回答

5

为了获取时间从午夜开始为Int是很容易的:

import Data.Time.Clock 
main = do 
    currTime <- getCurrentTime 
    let timed = floor $ utctDayTime currTime :: Int 
    print timed 

踢和笑声,这里是它

integralTime :: (Integral a) => IO a 
integralTime = getCurrentTime >>= return.floor.utctDayTime 

快速功能这工作,因为TimeDiff(这是一个RealFrac实例)可以将floor编为一个整数。

+1

也许值得一提的是,它使用'Data.Time.Clock'(尽管这很容易被[Hoogled] (http://www.haskell.org/hoogle/?hoogle=utctDayTime))。 – leftaroundabout 2013-03-24 17:25:08

+0

噢,是的,我将编辑 – jozefg 2013-03-24 17:26:25

+2

另外,'toRational'不是必需的:'floor'在任何'RealFrac'参数上已经是多态的。 – leftaroundabout 2013-03-24 17:27:32