]> gitweb @ CieloNegro.org - time-http.git/blobdiff - Data/Time/RFC1123.hs
RFC1123
[time-http.git] / Data / Time / RFC1123.hs
diff --git a/Data/Time/RFC1123.hs b/Data/Time/RFC1123.hs
new file mode 100644 (file)
index 0000000..abb0a6d
--- /dev/null
@@ -0,0 +1,52 @@
+module Data.Time.RFC1123
+    ( format
+    , parse
+    )
+    where
+
+import qualified Text.Parsec as P
+
+import Data.Time
+import Data.Time.Calendar.WeekDate
+import Data.Time.HTTP.Common
+import Data.Time.RFC1123.Parsec
+
+{-
+         The syntax for the date is hereby changed to:
+
+            date = 1*2DIGIT month 2*4DIGIT
+-}
+
+format :: ZonedTime -> String
+format zonedTime
+    = let localTime          = zonedTimeToLocalTime zonedTime
+          timeZone           = zonedTimeZone zonedTime
+          (year, month, day) = toGregorian (localDay localTime)
+          (_, _, week)       = toWeekDate  (localDay localTime)
+          timeOfDay          = localTimeOfDay localTime
+      in
+        concat [ shortWeekDayName week
+               , ", "
+               , show2 day
+               , " "
+               , shortMonthName month
+               , " "
+               , show4 year
+               , " "
+               , show2 (todHour timeOfDay)
+               , ":"
+               , show2 (todMin timeOfDay)
+               , ":"
+               , show2 (floor (todSec timeOfDay))
+               , " "
+               , show4digitsTZ timeZone
+               ]
+
+parse :: String -> Maybe ZonedTime
+parse src = case P.parse p "" src of
+              Right zt -> Just zt
+              Left  _  -> Nothing
+    where
+      p = do zt <- rfc1123DateAndTime
+             _  <- P.eof
+             return zt