]> gitweb @ CieloNegro.org - time-http.git/commitdiff
Asctime
authorPHO <pho@cielonegro.org>
Wed, 17 Mar 2010 08:46:59 +0000 (17:46 +0900)
committerPHO <pho@cielonegro.org>
Wed, 17 Mar 2010 08:46:59 +0000 (17:46 +0900)
Data/Time/Asctime.hs [new file with mode: 0644]
Data/Time/Asctime/Parsec.hs [new file with mode: 0644]
Data/Time/RFC1123/Parsec.hs
Data/Time/RFC733/Parsec.hs
Data/Time/RFC822/Parsec.hs
time-http.cabal

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
diff --git a/Data/Time/Asctime/Parsec.hs b/Data/Time/Asctime/Parsec.hs
new file mode 100644 (file)
index 0000000..3cff94c
--- /dev/null
@@ -0,0 +1,34 @@
+{-# LANGUAGE FlexibleContexts #-}
+module Data.Time.Asctime.Parsec
+    ( asctime
+    )
+    where
+
+import Control.Monad
+import Data.Fixed
+import Data.Time
+import Data.Time.Calendar.WeekDate
+import Data.Time.HTTP.Common
+import Text.Parsec
+
+
+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
+
+             gregDay <- assertGregorianDateIsGood year month day
+             _       <- assertWeekDayIsGood weekDay gregDay
+             tod     <- assertTimeOfDayIsGood hour minute second
+
+             return (LocalTime gregDay tod)
index f2176cf089a06f31e1f05204a0d659bfd88a47ed..ccdf0bf88873fe949e6e870b5ef14462ad71eb89 100644 (file)
@@ -39,4 +39,4 @@ date = do day   <- read2
           _     <- char ' '
           year  <- read4
           _     <- char ' '
-          assertGregorianDateIsGood (toInteger year) month day
+          assertGregorianDateIsGood year month day
index 996c94e51ee803fcbafaa56258b5192a6e140b9f..6746a3ed5ec8c4545e5fc018e03eff3ea60ccc6c 100644 (file)
@@ -44,7 +44,7 @@ date = do day   <- read2
                    <|>
                    liftM (+ 1900) read2
           _     <- char ' '
-          assertGregorianDateIsGood (toInteger year) month day
+          assertGregorianDateIsGood year month day
 
 time :: Stream s m Char => ParsecT s u m (TimeOfDay, TimeZone)
 time = do tod <- hour
index 0c6762e6a679fee91368e20dca4996d4ca1ac2f2..5d73ca8907a9406d4399b840699cdae1ec1d902f 100644 (file)
@@ -41,7 +41,7 @@ date = do day   <- read2
           _     <- char ' '
           year  <- liftM (+ 1900) read2
           _     <- char ' '
-          assertGregorianDateIsGood (toInteger year) month day
+          assertGregorianDateIsGood year month day
 
 rfc822time :: Stream s m Char => ParsecT s u m (TimeOfDay, TimeZone)
 rfc822time = do tod <- hour
index aeef0f6f88a521e600af42087d07a9bcec072563..051216baf59ea6809f29a7776b9879e6ec8b7ca8 100644 (file)
@@ -27,6 +27,8 @@ Library
         Data.Time.RFC822.Parsec
         Data.Time.RFC1123
         Data.Time.RFC1123.Parsec
+        Data.Time.Asctime
+        Data.Time.Asctime.Parsec
 
     Other-modules:
         Data.Time.HTTP.Common