【问题标题】:converting a String to type UTCTime将字符串转换为 UTCTime 类型
【发布时间】:2011-11-20 19:23:54
【问题描述】:

我正在编写一个 getSitemapR 处理程序,它使用 yesod-sitemap 来生成站点地图文件。我遇到的问题是将String 转换为UTCTime,如Data.Time.Clock 中所定义。黑线鳕文档说UTCTimeRead 类型类的一个实例,所以这就是我正在尝试的。这是我的代码。

module Handler.Root where

import Import
import Yesod.Goodies.Gravatar
import Data.Time.Format
import System.Locale
-- This is a handler function for the GET request method on the RootR
-- resource pattern. All of your resource patterns are defined in
-- config/routes
--
-- The majority of the code you will write in Yesod lives in these handler
-- functions. You can spread them across multiple files if you are so
-- inclined, or create a single monolithic file.
getRootR :: Handler RepHtml
getRootR = do
    defaultLayout $ do
        h2id <- lift newIdent
        setTitle "Cloudrr homepage"
        $(widgetFile "homepage")

gravatar :: Text -> String
gravatar email = 
  gravatarImg email go
  where
    go = GravatarOptions {
      gSize = Just (Size 140)
      , gDefault = Just (Identicon)
      , gForceDefault = ForceDefault False
      , gRating = Just (PG)
      }

getSitemapR :: Handler RepXml
getSitemapR = do
  sitemap [smo RootR]
  where
    smo = SitemapUrl  SitemapR{
      sitemapLoc = "http://www.cloudrr.me/sitemap.xml"
      , sitemapLastMod = (read "2011-11-19 18:28:r52.607875 UTC")::UTCTime
      , sitemapChangeFreq = Weekly
      , priority = 0.7
      }

我在关于系统编程的第 20 章中查看了我的 Real World Haskell 副本,但它的代码示例中没有涵盖 UTCTime,我用这个词搜索了 google 'haskell "Convert a String to UTCTime"' 没有结果。我在haskell-cafe 邮件列表中发现了以下Thread,因为SitemapLastMod 没有Maybe UTCTime,所以它不起作用。我想我在这里犯了一个非常愚蠢的错误,但我不确定,有人能指出我正确的方向吗?

感谢您的时间和考虑。

【问题讨论】:

  • 您使用的是read,它只是崩溃了,还是代码无法编译?你能清楚地说明你卡在哪里了吗?

标签: types haskell


【解决方案1】:
, sitemapLastMod = (read "2011-11-19 18:28:r52.607875 UTC")::UTCTime

小写的r 不应该在那里。试试

, sitemapLastMod = (read "2011-11-19 18:28:52.607875 UTC")::UTCTime

【讨论】:

    猜你喜欢
    • 2020-05-21
    • 1970-01-01
    • 2010-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多