将String转换为UTCTime类型

时间:2011-11-20 19:23:54

标签: types haskell

我正在编写一个使用yesod-sitemap生成站点地图文件的getSitemapR处理程序。我遇到的问题是将String转换为UTCTime,如Data.Time.Clock中所定义。 haddock文档说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的代码示例,我搜索了谷歌术语'haskell'将字符串转换为UTCTime“'没有结果。我在haskell-cafe邮件列表中找到了以下Thread,但由于SitemapLastMod没有Maybe UTCTime,因此无效。我想我在这里犯了一个非常愚蠢的错误,但我不确定,有人可以指出我正确的方向吗?

感谢您的时间和考虑。

1 个答案:

答案 0 :(得分:7)

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

小写r不应该存在。尝试

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