]> gitweb @ CieloNegro.org - time-http.git/blob - Data/Time/Asctime.hs
Tests for Data.Time.Asctime
[time-http.git] / Data / Time / Asctime.hs
1 {-# LANGUAGE
2     UnicodeSyntax
3   #-}
4 -- |This module provides functions for ANSI C's asctime() format.
5 --
6 -- ANSI C's asctime() format looks like:
7 --
8 -- @Wdy Mon [D]D HH:MM:SS YYYY@
9 --
10 -- The exact syntax is as follows:
11 --
12 -- > date-time ::= wday SP month SP day SP time SP year
13 -- > wday      ::= "Mon" | "Tue" | "Wed" | "Thu"
14 -- >             | "Fri" | "Sat" | "Sun"
15 -- > month     ::= "Jan" | "Feb" | "Mar" | "Apr"
16 -- >             | "May" | "Jun" | "Jul" | "Aug"
17 -- >             | "Sep" | "Oct" | "Nov" | "Dec"
18 -- > day       ::= 2DIGIT | SP 1DIGIT
19 -- > time      ::= 2DIGIT ':' 2DIGIT [':' 2DIGIT]
20 -- > year      ::= 4DIGIT
21 --
22 -- As you can see, it has no time zone info. "Data.Time.HTTP" will
23 -- treat it as UTC.
24 module Data.Time.Asctime
25     ( -- * Formatting
26       toAscii
27     , toAsciiBuilder
28
29       -- * Parsing
30     , fromAscii
31     , asctime
32     )
33     where
34 import Data.Ascii (Ascii)
35 import qualified Data.Ascii as A
36 import qualified Data.Attoparsec.Char8 as P
37 import Data.Time
38 import Data.Time.Asctime.Internal
39 import Prelude.Unicode
40
41 -- |Convert a 'LocalTime' to ANSI C's @asctime()@ string.
42 toAscii ∷ LocalTime → Ascii
43 toAscii = A.fromAsciiBuilder ∘ toAsciiBuilder
44
45 -- |Parse an ANSI C's @asctime()@ string. When the string can't be
46 -- parsed, it returns @'Left' err@.
47 fromAscii ∷ Ascii → Either String LocalTime
48 fromAscii = P.parseOnly p ∘ A.toByteString
49     where
50       p = do zt ← asctime
51              P.endOfInput
52              return zt