【发布时间】:2019-04-16 18:27:52
【问题描述】:
我正在解析一些时间戳(周围有其他文本)。我知道我 can parse time strings generally 使用 Data.Time。
但是,在我通过解包将日期时间从文本转换为字符串后,我不明白如何解析此错误:
{-# LANGUAGE DeriveGeneric, OverloadedStrings #-}
import Data.Time.Parse
import Data.Text as T
myDate = "D2017/01/01"
:t parseTimeOrError
ptime :: Text -> UTCTime
ptime dt = parseTimeOrError True defaultTimeLocale "D%Y/%d/%M" (T.unpack dt) :: UTCTime
-- here the T.unpack is because the :t tells us parseTimeOrError takes a string
-- not a Text.
myUTC <- ptime myDate
print myUTC
我明白了
parseTimeOrError :: forall t. ParseTime t => Bool -> TimeLocale -> String -> String -> t
<interactive>:1:10: error:
• Couldn't match expected type ‘IO a0’ with actual type ‘UTCTime’
• In the first argument of ‘GHC.GHCi.ghciStepIO :: forall a. IO a -> IO a’, namely ‘(ptime myDate)’
In a stmt of an interactive GHCi command: myUTC <- GHC.GHCi.ghciStepIO :: forall a. IO a -> IO a (ptime myDate)
我做错了什么?
【问题讨论】:
-
您的代码有点混乱......看起来这部分来自
.hs文件,其他部分来自ghci会话。无论如何,我认为问题是myUTC <- ptime myDate。当您在do块中时,您只能使用<-表示法,从 monad 中提取值(在这种情况下,ghci提示符是do中的隐式do块@ monad )。而是尝试let myUTC = ptime myDate。 -
谢谢,我也很困惑。我在 jupyter 中使用 ihaskell 笔记本。