3 , MultiParamTypeClasses
9 -- |This module provides functions for ANSI C's date and time strings.
11 -- ANSI C's @ctime(3)@/@asctime(3)@ format looks like:
13 -- @Wdy Mon [D]D HH:MM:SS YYYY@
15 -- The exact syntax is as follows:
17 -- > date-time ::= wday SP month SP day SP time SP year
18 -- > wday ::= "Mon" | "Tue" | "Wed" | "Thu"
19 -- > | "Fri" | "Sat" | "Sun"
20 -- > month ::= "Jan" | "Feb" | "Mar" | "Apr"
21 -- > | "May" | "Jun" | "Jul" | "Aug"
22 -- > | "Sep" | "Oct" | "Nov" | "Dec"
23 -- > day ::= 2DIGIT | SP 1DIGIT
24 -- > time ::= 2DIGIT ':' 2DIGIT [':' 2DIGIT]
26 module Data.Time.Format.C
30 import Data.Ascii (Ascii, AsciiBuilder)
31 import qualified Data.Ascii as A
32 import Data.Attoparsec.Char8
33 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 -- strings and 'LocalTime'.
45 -- >>> convertSuccess (Tagged (LocalTime (ModifiedJulianDay 49662) (TimeOfDay 8 49 37)) :: Tagged C LocalTime)
46 -- "Sun Nov 6 08:49:37 1994"
49 instance ConvertSuccess (Tagged C LocalTime) Ascii where
50 {-# INLINE convertSuccess #-}
51 convertSuccess = A.fromAsciiBuilder ∘ cs
53 instance ConvertSuccess (Tagged C LocalTime) AsciiBuilder where
54 {-# INLINE convertSuccess #-}
55 convertSuccess = toAsciiBuilder ∘ untag
57 instance ConvertAttempt Ascii (Tagged C LocalTime) where
58 {-# INLINE convertAttempt #-}
59 convertAttempt = parseAttempt' def
61 -- |Parse an ANSI C's date and time string.
62 instance Default (Parser (Tagged C LocalTime)) where
63 {-# INLINEABLE def #-}
64 def = do weekDay ← shortWeekDayNameP
66 month ← shortMonthNameP
78 gregDay ← assertGregorianDateIsGood year month day
79 _ ← assertWeekDayIsGood weekDay gregDay
80 tod ← assertTimeOfDayIsGood hour minute second
82 return ∘ Tagged $ 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| Tagged C LocalTime |], [t| Ascii |])
105 , ([t| Tagged C LocalTime |], [t| AsciiBuilder |])