]> gitweb @ CieloNegro.org - time-http.git/blobdiff - Data/Time/Asctime/Internal.hs
Data.Time.{RFC1123,Asctime} now compiles.
[time-http.git] / Data / Time / Asctime / Internal.hs
index 85707c653c6d3d98b0684f9886977741df7212f0..910fd5d6019f2520c840e1e437954d0b53a93900 100644 (file)
@@ -1,32 +1,60 @@
-{-# LANGUAGE FlexibleContexts #-}
+{-# LANGUAGE
+    OverloadedStrings
+  , UnicodeSyntax
+  #-}
 module Data.Time.Asctime.Internal
     ( asctime
+    , toAsciiBuilder
     )
     where
-import Control.Monad
-import Data.Fixed
+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
 
--- |This is a parsec parser for ANSI C's asctime() format.
-asctime :: Stream s m Char => ParsecT s u m LocalTime
-asctime = do weekDay <- shortWeekDayNameP
-             _       <- string ", "
-             month   <- shortMonthNameP
-             _       <- char ' '
-             day     <- read2
-             _       <- char ' '
-             hour    <- read2
-             _       <- char ':'
-             minute  <- read2
-             _       <- char ':'
-             second  <- read2
-             _       <- char ' '
-             year    <- read4
+-- |Parse an ANSI C's @asctime()@ string.
+asctime ∷ Parser LocalTime
+asctime = do weekDay  shortWeekDayNameP
+             _        string ", "
+             month    shortMonthNameP
+             _        char ' '
+             day      read2
+             _        char ' '
+             hour     read2
+             _        char ':'
+             minute   read2
+             _        char ':'
+             second   read2
+             _        char ' '
+             year     read4
 
-             gregDay <- assertGregorianDateIsGood year month day
-             _       <- assertWeekDayIsGood weekDay gregDay
-             tod     <- assertTimeOfDayIsGood hour minute second
+             gregDay  assertGregorianDateIsGood year month day
+             _        assertWeekDayIsGood weekDay gregDay
+             tod      assertTimeOfDayIsGood hour minute second
 
              return (LocalTime gregDay tod)
+
+-- |Convert a 'LocalTime' to ANSI C's @asctime()@ string.
+toAsciiBuilder ∷ LocalTime → AsciiBuilder
+toAsciiBuilder localTime
+    = let (year, month, day) = toGregorian (localDay localTime)
+          (_, _, week)       = toWeekDate  (localDay localTime)
+          timeOfDay          = localTimeOfDay localTime
+      in
+        shortWeekDayName week
+        ⊕ A.toAsciiBuilder "⊕ "
+        ⊕ shortMonthName month
+        ⊕ A.toAsciiBuilder " "
+        ⊕ show2 day
+        ⊕ A.toAsciiBuilder " "
+        ⊕ show2 (todHour timeOfDay)
+        ⊕ A.toAsciiBuilder ":"
+        ⊕ show2 (todMin timeOfDay)
+        ⊕ A.toAsciiBuilder ":"
+        ⊕ show2 (floor (todSec timeOfDay) ∷ Int)
+        ⊕ A.toAsciiBuilder " "
+        ⊕ show4 year
+