]> gitweb @ CieloNegro.org - time-http.git/blobdiff - Data/Time/RFC822/Internal.hs
Data.Time.RFC822 now compiles.
[time-http.git] / Data / Time / RFC822 / Internal.hs
index 297120a40c51fdede753bd57e9a5770881b99d4a..607cf88c0277ee2cb0750c9b7c61a8eac10a37b5 100644 (file)
@@ -2,15 +2,23 @@
     OverloadedStrings
   , UnicodeSyntax
   #-}
+-- |Internal functions for "Data.Time.RFC822".
 module Data.Time.RFC822.Internal
     ( rfc822DateAndTime
     , rfc822time
+    , showRFC822TimeZone
+    , toAsciiBuilder
     )
     where
 import Control.Applicative
+import Data.Ascii (AsciiBuilder)
+import qualified Data.Ascii as A
 import Data.Attoparsec.Char8
+import Data.Monoid.Unicode
 import Data.Time
+import Data.Time.Calendar.WeekDate
 import Data.Time.HTTP.Common
+import Prelude.Unicode
 
 -- |Parse an RFC 822 date and time string.
 rfc822DateAndTime ∷ Parser ZonedTime
@@ -80,3 +88,34 @@ zone = choice [ string "UT"  *> return (TimeZone 0 False "UT" )
               , char 'Y' *> return (TimeZone ( 12  * 60) False "Y")
               , read4digitsTZ
               ]
+
+-- |No need to explain.
+showRFC822TimeZone ∷ TimeZone → AsciiBuilder
+showRFC822TimeZone tz
+    | timeZoneMinutes tz ≡ 0 = A.toAsciiBuilder "GMT"
+    | otherwise              = show4digitsTZ tz
+
+-- |Convert a 'ZonedTime' to RFC 822 date and time string.
+toAsciiBuilder ∷ ZonedTime → AsciiBuilder
+toAsciiBuilder zonedTime
+    = let localTime          = zonedTimeToLocalTime zonedTime
+          timeZone           = zonedTimeZone zonedTime
+          (year, month, day) = toGregorian (localDay localTime)
+          (_, _, week)       = toWeekDate  (localDay localTime)
+          timeOfDay          = localTimeOfDay localTime
+      in
+        shortWeekDayName week
+        ⊕ A.toAsciiBuilder ", "
+        ⊕ show2 day
+        ⊕ A.toAsciiBuilder " "
+        ⊕ shortMonthName month
+        ⊕ A.toAsciiBuilder " "
+        ⊕ show2 (year `mod` 100)
+        ⊕ A.toAsciiBuilder " "
+        ⊕ show2 (todHour timeOfDay)
+        ⊕ A.toAsciiBuilder ":"
+        ⊕ show2 (todMin timeOfDay)
+        ⊕ A.toAsciiBuilder ":"
+        ⊕ show2 (floor (todSec timeOfDay) ∷ Int)
+        ⊕ A.toAsciiBuilder " "
+        ⊕ showRFC822TimeZone timeZone