哈斯克尔当前时间

时间:2017-01-17 18:21:52

标签: haskell

我正在尝试制作一个以日期格式提供日期的辅助函数,所以我有以下代码

localDay :: Day
localDay = do 
  now <- liftIO $ getCurrentTime
  utctDay now

但是得到以下错误

Couldn't match type ‘m0 b0’ with ‘Day’
Expected type: m0 UTCTime -> (UTCTime -> m0 b0) -> Day
  Actual type: m0 UTCTime -> (UTCTime -> m0 b0) -> m0 b0
In a stmt of a 'do' block: now <- liftIO $ getCurrentTime
In the expression:
  do { now <- liftIO $ getCurrentTime;
       utctDay now }
In an equation for ‘localDay’:
    localDay
      = do { now <- liftIO $ getCurrentTime;
             utctDay now }

在这种情况下,我真的无法理解错误的原因,因为现在应该是UTCTimẹ类型

1 个答案:

答案 0 :(得分:2)

getCurrentTime :: IO UTCTime,因此您不能简单地返回Day值;您只能返回IO Day值(忽略unsafePerformIO

localDay :: IO Day
localDay = fmap utctDay getCurrentTime