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.C
31 import Control.Applicative
32 import Data.Ascii (Ascii, AsciiBuilder)
33 import qualified Data.Ascii as A
34 import Data.Attoparsec.Char8
35 import Data.Convertible.Base
36 import Data.Monoid.Unicode
40 import Data.Time.Calendar.WeekDate
41 import Data.Time.Format.HTTP.Common
42 import Prelude.Unicode
44 -- |The phantom type for conversions between ANSI C's date and time
45 -- strings and 'LocalTime'.
47 -- >>> convertSuccess (LocalTime (ModifiedJulianDay 49662) (TimeOfDay 8 49 37))
48 -- Tagged "Sun Nov 6 08:49:37 1994"
51 -- |The proxy for conversions between ANSI C's date and time strings
54 {-# INLINE CONLIKE c #-}
57 instance ConvertSuccess LocalTime (Tagged C Ascii) where
58 {-# INLINE convertSuccess #-}
59 convertSuccess = (A.fromAsciiBuilder <$>) ∘ cs
61 instance ConvertSuccess LocalTime (Tagged C AsciiBuilder) where
62 {-# INLINE convertSuccess #-}
63 convertSuccess = Tagged ∘ toAsciiBuilder
65 instance ConvertAttempt (Tagged C Ascii) LocalTime where
66 {-# INLINE convertAttempt #-}
67 convertAttempt = parseAttempt' cDateAndTime ∘ untag
69 -- |Parse an ANSI C's date and time string.
70 cDateAndTime ∷ Parser LocalTime
72 = do weekDay ← shortWeekDayNameP
74 month ← shortMonthNameP
86 gregDay ← assertGregorianDateIsGood year month day
87 _ ← assertWeekDayIsGood weekDay gregDay
88 tod ← assertTimeOfDayIsGood hour minute second
90 return (LocalTime gregDay tod)
92 toAsciiBuilder ∷ LocalTime → AsciiBuilder
93 toAsciiBuilder localTime
94 = let (year, month, day) = toGregorian (localDay localTime)
95 (_, _, week) = toWeekDate (localDay localTime)
96 timeOfDay = localTimeOfDay localTime
99 ⊕ A.toAsciiBuilder " "
100 ⊕ shortMonthName month
101 ⊕ A.toAsciiBuilder " "
103 ⊕ A.toAsciiBuilder " "
104 ⊕ show2 (todHour timeOfDay)
105 ⊕ A.toAsciiBuilder ":"
106 ⊕ show2 (todMin timeOfDay)
107 ⊕ A.toAsciiBuilder ":"
108 ⊕ show2 (floor (todSec timeOfDay) ∷ Int)
109 ⊕ A.toAsciiBuilder " "
112 deriveAttempts [ ([t| LocalTime |], [t| Tagged C Ascii |])
113 , ([t| LocalTime |], [t| Tagged C AsciiBuilder |])