1 -- | Format W3C Date and Time strings.
2 module Data.Time.W3C.Format
7 import Data.Convertible
10 import Data.Time.W3C.Types
13 -- | Format W3C Date and Time string from anything convertible to
14 -- 'W3CDateTime' type. The most obvious acceptable type is the
15 -- 'W3CDateTime' itself.
16 format :: Convertible t W3CDateTime => t -> String
17 format = format' . convert
19 format' (W3CDateTime year Nothing Nothing Nothing Nothing Nothing Nothing)
22 format' (W3CDateTime year (Just month) Nothing Nothing Nothing Nothing Nothing)
23 = concat [show4 year, "-", show2 month]
25 format' (W3CDateTime year (Just month) (Just day) Nothing Nothing Nothing Nothing)
26 = concat [show4 year, "-", show2 month, "-", show2 day]
28 format' (W3CDateTime year (Just month) (Just day) (Just hour) (Just minute) Nothing (Just tz))
41 format' (W3CDateTime year (Just month) (Just day) (Just hour) (Just minute) (Just second) (Just tz))
52 , case properFraction second :: (Int, Pico) of
53 (int, 0 ) -> show2 int
54 (int, frac) -> show2 int ++ tail (showFixed True frac)
58 format' w = error ("Invalid W3C Date and Time: " ++ show w)
60 show4 :: Integral i => i -> String
62 | i >= 0 && i < 10 = "000" ++ show i
63 | i >= 0 && i < 100 = "00" ++ show i
64 | i >= 0 && i < 1000 = "0" ++ show i
65 | i >= 0 && i < 10000 = show i
66 | otherwise = error ("show4: the integer i must satisfy 0 <= i < 10000: " ++ show i)
68 show2 :: Integral i => i -> String
70 | i >= 0 && i < 10 = "0" ++ show i
71 | i >= 0 && i < 100 = show i
72 | otherwise = error ("show2: the integer i must satisfy 0 <= i < 100: " ++ show i)
74 showTZ :: TimeZone -> String
76 = case timeZoneMinutes tz of
77 offset | offset < 0 -> '-' : showTZ' (negate offset)
79 | otherwise -> '+' : showTZ' offset
82 = let h = offset `div` 60
85 concat [show2 h, ":", show2 m]