X-Git-Url: http://git.cielonegro.org/gitweb.cgi?p=time-http.git;a=blobdiff_plain;f=Data%2FTime%2FAsctime.hs;h=0814e451f9f0f82e37f849da656740adb0e09103;hp=fd5cca01f994d8dde7f733e89344c1ccad69a201;hb=e322e3c65458dd6004ae4d2fbf5e82ce9aaee162;hpb=469170632eee26999634929f964d9cc71e068bf3 diff --git a/Data/Time/Asctime.hs b/Data/Time/Asctime.hs index fd5cca0..0814e45 100644 --- a/Data/Time/Asctime.hs +++ b/Data/Time/Asctime.hs @@ -1,8 +1,11 @@ --- |This module is for ANSI C's asctime() format. +{-# LANGUAGE + UnicodeSyntax + #-} +-- |This module provides functions for ANSI C's asctime() format. -- -- ANSI C's asctime() format looks like: -- --- @Wdy Mon DD HH:MM:SS YYYY@ +-- @Wdy Mon [D]D HH:MM:SS YYYY@ -- -- The exact syntax is as follows: -- @@ -12,54 +15,38 @@ -- > month ::= "Jan" | "Feb" | "Mar" | "Apr" -- > | "May" | "Jun" | "Jul" | "Aug" -- > | "Sep" | "Oct" | "Nov" | "Dec" --- > day ::= 2DIGIT +-- > day ::= 2DIGIT | SP 1DIGIT -- > time ::= 2DIGIT ':' 2DIGIT [':' 2DIGIT] -- > year ::= 4DIGIT -- -- As you can see, it has no time zone info. "Data.Time.HTTP" will -- treat it as UTC. module Data.Time.Asctime - ( format - , parse + ( -- * Formatting + toAscii + , toAsciiBuilder + + -- * Parsing + , fromAscii + , asctime ) where - -import qualified Text.Parsec as P - +import Data.Ascii (Ascii) +import qualified Data.Ascii as A +import qualified Data.Attoparsec.Char8 as P import Data.Time -import Data.Time.Calendar.WeekDate -import Data.Time.HTTP.Common -import Data.Time.Asctime.Parsec +import Data.Time.Asctime.Internal +import Prelude.Unicode --- |Format a 'LocalTime' in the ANSI C's asctime() way. -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 - ] +-- |Convert a 'LocalTime' to ANSI C's @asctime()@ string. +toAscii ∷ LocalTime → Ascii +toAscii = A.fromAsciiBuilder ∘ toAsciiBuilder --- |Parse an ANSI C's asctime() format to 'LocalTime'. When the string --- can't be parsed, it returns 'Nothing'. -parse :: String -> Maybe LocalTime -parse src = case P.parse p "" src of - Right zt -> Just zt - Left _ -> Nothing +-- |Parse an ANSI C's @asctime()@ string. When the string can't be +-- parsed, it returns @'Left' err@. +fromAscii ∷ Ascii → Either String LocalTime +fromAscii = P.parseOnly p ∘ A.toByteString where - p = do zt <- asctime - _ <- P.eof + p = do zt ← asctime + P.endOfInput return zt