]> gitweb @ CieloNegro.org - time-http.git/blob - Data/Time/Asctime.hs
Changed some module's name
[time-http.git] / Data / Time / Asctime.hs
1 -- |This module provides functions 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 import Data.Time
27 import Data.Time.Calendar.WeekDate
28 import Data.Time.HTTP.Common
29 import Data.Time.Asctime.Internal
30
31 -- |Format a 'LocalTime' in the ANSI C's asctime() way.
32 format :: LocalTime -> String
33 format localTime
34     = let (year, month, day) = toGregorian (localDay localTime)
35           (_, _, week)       = toWeekDate  (localDay localTime)
36           timeOfDay          = localTimeOfDay localTime
37       in
38         concat [ shortWeekDayName week
39                , ", "
40                , shortMonthName month
41                , " "
42                , show2 day
43                , " "
44                , show2 (todHour timeOfDay)
45                , ":"
46                , show2 (todMin timeOfDay)
47                , ":"
48                , show2 (floor (todSec timeOfDay))
49                , " "
50                , show4 year
51                ]
52
53 -- |Parse an ANSI C's asctime() format to 'LocalTime'. When the string
54 -- can't be parsed, it returns 'Nothing'.
55 parse :: String -> Maybe LocalTime
56 parse src = case P.parse p "" src of
57               Right zt -> Just zt
58               Left  _  -> Nothing
59     where
60       p = do zt <- asctime
61              _  <- P.eof
62              return zt