【问题标题】:Text friendly way of parsing text timestamps in Haskell在 Haskell 中解析文本时间戳的文本友好方式
【发布时间】: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 &lt;- ptime myDate。当您在 do 块中时,您只能使用 &lt;- 表示法,从 monad 中提取值(在这种情况下,ghci 提示符是 do 中的隐式 do 块@ monad )。而是尝试let myUTC = ptime myDate
  • 谢谢,我也很困惑。我在 jupyter 中使用 ihaskell 笔记本。

标签: haskell time


【解决方案1】:

@DarthFennec 是对的。替换

myUTC <- ptime myDate

myUTC = ptime myDate

我不会从上面的错误中理解这一点。这与 Text 或 Data.Time 无关。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-07
    • 2019-05-31
    • 2012-07-31
    • 2011-10-10
    • 1970-01-01
    • 1970-01-01
    • 2016-03-05
    相关资源
    最近更新 更多