3 , MultiParamTypeClasses
8 -- |This module provides functions for ANSI C's date and time strings.
10 -- ANSI C's @ctime(3)@/@asctime(3)@ format looks like:
12 -- @Wdy Mon [D]D HH:MM:SS YYYY@
14 -- The exact syntax is as follows:
16 -- > date-time ::= wday SP month SP day SP time SP year
17 -- > wday ::= "Mon" | "Tue" | "Wed" | "Thu"
18 -- > | "Fri" | "Sat" | "Sun"
19 -- > month ::= "Jan" | "Feb" | "Mar" | "Apr"
20 -- > | "May" | "Jun" | "Jul" | "Aug"
21 -- > | "Sep" | "Oct" | "Nov" | "Dec"
22 -- > day ::= 2DIGIT | SP 1DIGIT
23 -- > time ::= 2DIGIT ':' 2DIGIT [':' 2DIGIT]
25 module Data.Time.Format.Asctime
30 import Control.Applicative
31 import Data.Ascii (Ascii, AsciiBuilder)
32 import qualified Data.Ascii as A
33 import Data.Attoparsec.Char8
34 import Data.Convertible.Base
35 import Data.Monoid.Unicode
38 import Data.Time.Calendar.WeekDate
39 import Data.Time.Format.HTTP.Common
40 import Prelude.Unicode
42 -- |The phantom type for conversions between ANSI C's date and time
43 -- string and 'LocalTime'.
45 -- >>> convertSuccess (LocalTime (ModifiedJulianDay 49662) (TimeOfDay 8 49 37))
46 -- Tagged "Sun Nov 6 08:49:37 1994"
49 instance ConvertSuccess LocalTime (Tagged Asctime Ascii) where
50 {-# INLINE convertSuccess #-}
51 convertSuccess = (A.fromAsciiBuilder <$>) ∘ cs
53 instance ConvertSuccess LocalTime (Tagged Asctime AsciiBuilder) where
54 {-# INLINE convertSuccess #-}
55 convertSuccess = Tagged ∘ toAsciiBuilder
57 instance ConvertAttempt (Tagged Asctime Ascii) LocalTime where
58 {-# INLINE convertAttempt #-}
59 convertAttempt = parseAttempt' asctime ∘ untag
61 -- |Parse an ANSI C's date and time string.
62 asctime ∷ Parser LocalTime
64 = do weekDay ← shortWeekDayNameP
66 month ← shortMonthNameP
78 gregDay ← assertGregorianDateIsGood year month day
79 _ ← assertWeekDayIsGood weekDay gregDay
80 tod ← assertTimeOfDayIsGood hour minute second
82 return (LocalTime gregDay tod)
84 toAsciiBuilder ∷ LocalTime → AsciiBuilder
85 toAsciiBuilder localTime
86 = let (year, month, day) = toGregorian (localDay localTime)
87 (_, _, week) = toWeekDate (localDay localTime)
88 timeOfDay = localTimeOfDay localTime
91 ⊕ A.toAsciiBuilder " "
92 ⊕ shortMonthName month
93 ⊕ A.toAsciiBuilder " "
95 ⊕ A.toAsciiBuilder " "
96 ⊕ show2 (todHour timeOfDay)
97 ⊕ A.toAsciiBuilder ":"
98 ⊕ show2 (todMin timeOfDay)
99 ⊕ A.toAsciiBuilder ":"
100 ⊕ show2 (floor (todSec timeOfDay) ∷ Int)
101 ⊕ A.toAsciiBuilder " "
104 deriveAttempts [ ([t| LocalTime |], [t| Tagged Asctime Ascii |])
105 , ([t| LocalTime |], [t| Tagged Asctime AsciiBuilder |])