]> gitweb @ CieloNegro.org - time-http.git/blob - Data/Time/Asctime.hs
haddock comments
[time-http.git] / Data / Time / Asctime.hs
1 -- |This module is for ANSI C's asctime() format.
2 --
3 -- ANSI C's asctime() format looks like:
4 --
5 -- @Wdy Mon DD HH:MM:SS YYYY@
6 --
7 -- The exact syntax is as follows:
8 --
9 -- > date-time ::= wday SP month SP day SP time SP year
10 -- > wday      ::= "Mon" | "Tue" | "Wed" | "Thu"
11 -- >             | "Fri" | "Sat" | "Sun"
12 -- > month     ::= "Jan" | "Feb" | "Mar" | "Apr"
13 -- >             | "May" | "Jun" | "Jul" | "Aug"
14 -- >             | "Sep" | "Oct" | "Nov" | "Dec"
15 -- > day       ::= 2DIGIT
16 -- > time      ::= 2DIGIT ':' 2DIGIT [':' 2DIGIT]
17 -- > year      ::= 4DIGIT
18 --
19 -- As you can see, it has no time zone info. "Data.Time.HTTP" will
20 -- treat it as UTC.
21 module Data.Time.Asctime
22     ( format
23     , parse
24     )
25     where
26
27 import qualified Text.Parsec as P
28
29 import Data.Time
30 import Data.Time.Calendar.WeekDate
31 import Data.Time.HTTP.Common
32 import Data.Time.Asctime.Parsec
33
34 -- |Format a 'LocalTime' in the ANSI C's asctime() way.
35 format :: LocalTime -> String
36 format localTime
37     = let (year, month, day) = toGregorian (localDay localTime)
38           (_, _, week)       = toWeekDate  (localDay localTime)
39           timeOfDay          = localTimeOfDay localTime
40       in
41         concat [ shortWeekDayName week
42                , ", "
43                , shortMonthName month
44                , " "
45                , show2 day
46                , " "
47                , show2 (todHour timeOfDay)
48                , ":"
49                , show2 (todMin timeOfDay)
50                , ":"
51                , show2 (floor (todSec timeOfDay))
52                , " "
53                , show4 year
54                ]
55
56 -- |Parse an ANSI C's asctime() format to 'LocalTime'. When the string
57 -- can't be parsed, it returns 'Nothing'.
58 parse :: String -> Maybe LocalTime
59 parse src = case P.parse p "" src of
60               Right zt -> Just zt
61               Left  _  -> Nothing
62     where
63       p = do zt <- asctime
64              _  <- P.eof
65              return zt