Permalink
Browse files

Fixed dates to be both RFC2822 and RFC3339 compliant.

  • Loading branch information...
1 parent 3abf5c6 commit bf59938351692cce6bf79a95fbfdb4da140d9057 koral committed Mar 4, 2012
Showing with 31 additions and 28 deletions.
  1. +8 −7 Imm/Core.hs
  2. +6 −6 Imm/Mail.hs
  3. +4 −3 Imm/Types.hs
  4. +6 −6 Imm/Util.hs
  5. +7 −6 imm.cabal
View
@@ -15,9 +15,8 @@ import Control.Monad hiding(forM_)
import Data.Foldable
--import Data.Functor
--import Data.Maybe
-import Data.Time.Clock
+import Data.Time
import Data.Time.Clock.POSIX
-import Data.Time.Format
import Network.HTTP hiding(Response)
import Network.URI
@@ -129,9 +128,10 @@ processFeed parameters (uri, Right feed) = do
--
oldTime <- try $ readFile (directory </> fileName)
+ let timeZero = posixSecondsToUTCTime $ 0
let threshold = either
- (const $ posixSecondsToUTCTime 0)
- (maybe (posixSecondsToUTCTime 0) id . parseTime defaultTimeLocale "%F %T %Z")
+ (const timeZero)
+ (maybe timeZero id . parseDate)
oldTime
lastTime <- foldlM (\acc item -> processItem parameters threshold item >>= (return . (max acc))) threshold (feedItems feed)
@@ -147,6 +147,7 @@ processFeed parameters (uri, Right feed) = do
processItem :: Parameters -> UTCTime -> Item -> IO UTCTime
processItem parameters@Parameters{ mMailDirectory = directory } threshold item = do
currentTime <- getCurrentTime :: IO UTCTime
+ timeZone <- getCurrentTimeZone
let time = getItemDate item
whenLoud . putStr . unlines $ ["",
@@ -155,14 +156,14 @@ processItem parameters@Parameters{ mMailDirectory = directory } threshold item =
" Item URI: " ++ (maybe "" id $ getItemLink item),
" Item date: " ++ (maybe "" id $ time)]
- case time >>= stringToUTC of
+ case time >>= parseDate of
Just y -> do
when (threshold < y) $ do
whenLoud . putStrLn $ "==> New entry added to maildir."
- Maildir.add directory . itemToMail $ item
+ Maildir.add directory . itemToMail timeZone $ item
return y
_ -> do
- Maildir.add directory . itemToMail $ item
+ Maildir.add directory . itemToMail timeZone $ item
return threshold
downloadRaw :: URI -> IO (Either String String)
View
@@ -6,28 +6,28 @@ import Imm.Util
import Control.Monad
-import Data.Time.Clock.POSIX
+import Data.Time
import Text.Feed.Query
import Text.Feed.Types
-- }}}
-
+
defaultMail :: Mail
defaultMail = Mail {
mCharset = "utf-8",
mContent = "",
mContentDisposition = "inline",
- mDate = posixSecondsToUTCTime 0,
+ mDate = Nothing,
mFrom = "imm",
mMIME = "text/html",
mSubject = "Untitled",
mReturnPath = "<imm@noreply>"}
-itemToMail :: Item -> Mail
-itemToMail item = defaultMail {
- mDate = maybe (posixSecondsToUTCTime 0) id . (stringToUTC <=< getItemDate) $ item,
+itemToMail :: TimeZone -> Item -> Mail
+itemToMail timeZone item = defaultMail {
+ mDate = maybe Nothing (Just . utcToZonedTime timeZone) . parseDate <=< getItemDate $ item,
mFrom = maybe "Anonymous" id $ getItemAuthor item,
mSubject = maybe "Untitled" id $ getItemTitle item,
mContent = maybe "Empty" id $ getItemDescription item}
View
@@ -4,7 +4,8 @@ module Imm.Types where
-- {{{ Imports
import Network.URI
-import Data.Time.Clock
+import Data.Time
+import Data.Time.RFC2822
import System.Console.CmdArgs
@@ -31,7 +32,7 @@ data ImmFeed = ImmFeed {
data Mail = Mail {
mReturnPath :: String,
- mDate :: UTCTime,
+ mDate :: Maybe ZonedTime,
mFrom :: String,
mSubject :: String,
mMIME :: String,
@@ -43,7 +44,7 @@ data Mail = Mail {
instance Show Mail where
show mail = unlines [
"Return-Path: " ++ mReturnPath mail,
- "Date: " ++ (show $ mDate mail),
+ maybe "" (("Date: " ++) . showRFC2822) . mDate $ mail,
"From: " ++ mFrom mail,
"Subject: " ++ mSubject mail,
"Content-Type: " ++ mMIME mail ++ "; charset=" ++ mCharset mail,
View
@@ -3,20 +3,20 @@ module Imm.Util where
-- {{{ Imports
import Codec.Binary.UTF8.String
-import Data.Time.Clock
-import Data.Time.Format
+import Data.Maybe
+import Data.Time
+import Data.Time.RFC2822
+import Data.Time.RFC3339
--import Network.URI
-
-import System.Locale
-- }}}
escapeFileName :: Char -> String
escapeFileName '/' = "|"
escapeFileName x = x:[]
-stringToUTC :: String -> Maybe UTCTime
-stringToUTC = parseTime defaultTimeLocale "%a, %e %b %Y %T %z"
+parseDate :: String -> Maybe UTCTime
+parseDate date = listToMaybe . map zonedTimeToUTC . catMaybes . map ((flip ($)) date) $ [readRFC2822, readRFC3339]
decodeIfNeeded :: String -> String
decodeIfNeeded text = case isUTF8Encoded text of
View
@@ -28,21 +28,22 @@ Library
Imm.Maildir
Build-depends:
base == 4.*,
+ bytestring,
+ cmdargs,
directory,
dyre,
feed,
filepath,
HTTP,
- time,
- xml,
+ mime-mail,
+ network,
old-locale,
random,
- bytestring,
- mime-mail,
text,
- cmdargs,
- network,
+ time,
+ timerep,
utf8-string
+ xml,
-- Other-modules:
-- Build-tools:

0 comments on commit bf59938

Please sign in to comment.