]> gitweb @ CieloNegro.org - time-http.git/blob - Data/Time/Format/Asctime.hs
Done.
[time-http.git] / Data / Time / Format / Asctime.hs
1 {-# LANGUAGE
2     FlexibleInstances
3   , MultiParamTypeClasses
4   , OverloadedStrings
5   , TemplateHaskell
6   , UnicodeSyntax
7   #-}
8 -- |This module provides functions for ANSI C's date and time strings.
9 --
10 -- ANSI C's @ctime(3)@/@asctime(3)@ format looks like:
11 --
12 -- @Wdy Mon [D]D HH:MM:SS YYYY@
13 --
14 -- The exact syntax is as follows:
15 --
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]
24 -- > year      ::= 4DIGIT
25 module Data.Time.Format.Asctime
26     ( Asctime
27     , asctime
28     )
29     where
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
36 import Data.Tagged
37 import Data.Time
38 import Data.Time.Calendar.WeekDate
39 import Data.Time.Format.HTTP.Common
40 import Prelude.Unicode
41
42 -- |The phantom type for conversions between ANSI C's date and time
43 -- string and 'LocalTime'.
44 --
45 -- >>> convertSuccess (LocalTime (ModifiedJulianDay 49662) (TimeOfDay 8 49 37))
46 -- Tagged "Sun Nov  6 08:49:37 1994"
47 data Asctime
48
49 instance ConvertSuccess LocalTime (Tagged Asctime Ascii) where
50     {-# INLINE convertSuccess #-}
51     convertSuccess = (A.fromAsciiBuilder <$>) ∘ cs
52
53 instance ConvertSuccess LocalTime (Tagged Asctime AsciiBuilder) where
54     {-# INLINE convertSuccess #-}
55     convertSuccess = Tagged ∘ toAsciiBuilder
56
57 instance ConvertAttempt (Tagged Asctime Ascii) LocalTime where
58     {-# INLINE convertAttempt #-}
59     convertAttempt = parseAttempt' asctime ∘ untag
60
61 -- |Parse an ANSI C's date and time string.
62 asctime ∷ Parser LocalTime
63 asctime
64     = do weekDay ← shortWeekDayNameP
65          _       ← char ' '
66          month   ← shortMonthNameP
67          _       ← char ' '
68          day     ← read2'
69          _       ← char ' '
70          hour    ← read2
71          _       ← char ':'
72          minute  ← read2
73          _       ← char ':'
74          second  ← read2
75          _       ← char ' '
76          year    ← read4
77
78          gregDay ← assertGregorianDateIsGood year month day
79          _       ← assertWeekDayIsGood weekDay gregDay
80          tod     ← assertTimeOfDayIsGood hour minute second
81
82          return (LocalTime gregDay tod)
83
84 toAsciiBuilder ∷ LocalTime → AsciiBuilder
85 toAsciiBuilder localTime
86     = let (year, month, day) = toGregorian (localDay localTime)
87           (_, _, week)       = toWeekDate  (localDay localTime)
88           timeOfDay          = localTimeOfDay localTime
89       in
90         shortWeekDayName week
91         ⊕ A.toAsciiBuilder " "
92         ⊕ shortMonthName month
93         ⊕ A.toAsciiBuilder " "
94         ⊕ show2' day
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 " "
102         ⊕ show4 year
103
104 deriveAttempts [ ([t| LocalTime |], [t| Tagged Asctime Ascii        |])
105                , ([t| LocalTime |], [t| Tagged Asctime AsciiBuilder |])
106                ]