]> gitweb @ CieloNegro.org - time-http.git/blobdiff - Data/Time/Asctime.hs
Asctime
[time-http.git] / Data / Time / Asctime.hs
diff --git a/Data/Time/Asctime.hs b/Data/Time/Asctime.hs
new file mode 100644 (file)
index 0000000..7ebb596
--- /dev/null
@@ -0,0 +1,46 @@
+module Data.Time.Asctime
+    ( 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.Asctime.Parsec
+
+{-
+  Wdy Mon DD HH:MM:SS YYYY
+-}
+
+format :: LocalTime -> String
+format localTime
+    = let (year, month, day) = toGregorian (localDay localTime)
+          (_, _, week)       = toWeekDate  (localDay localTime)
+          timeOfDay          = localTimeOfDay localTime
+      in
+        concat [ shortWeekDayName week
+               , ", "
+               , shortMonthName month
+               , " "
+               , show2 day
+               , " "
+               , show2 (todHour timeOfDay)
+               , ":"
+               , show2 (todMin timeOfDay)
+               , ":"
+               , show2 (floor (todSec timeOfDay))
+               , " "
+               , show4 year
+               ]
+
+parse :: String -> Maybe LocalTime
+parse src = case P.parse p "" src of
+              Right zt -> Just zt
+              Left  _  -> Nothing
+    where
+      p = do zt <- asctime
+             _  <- P.eof
+             return zt