--- /dev/null
+module Data.Time.W3C.Format
+ ( format
+ )
+ where
+
+import Data.Convertible
+import Data.Fixed
+import Data.Time
+import Data.Time.W3C.Types
+
+
+format :: Convertible t W3CDateTime => t -> String
+format = format' . convert
+ where
+ format' (W3CDateTime year Nothing Nothing Nothing Nothing Nothing Nothing)
+ = show4 year
+
+ format' (W3CDateTime year (Just month) Nothing Nothing Nothing Nothing Nothing)
+ = concat [show4 year, "-", show2 month]
+
+ format' (W3CDateTime year (Just month) (Just day) Nothing Nothing Nothing Nothing)
+ = concat [show4 year, "-", show2 month, "-", show2 day]
+
+ format' (W3CDateTime year (Just month) (Just day) (Just hour) (Just minute) Nothing (Just tz))
+ = concat [ show4 year
+ , "-"
+ , show2 month
+ , "-"
+ , show2 day
+ , "T"
+ , show2 hour
+ , ":"
+ , show2 minute
+ , showTZ tz
+ ]
+
+ format' (W3CDateTime year (Just month) (Just day) (Just hour) (Just minute) (Just second) (Just tz))
+ = concat [ show4 year
+ , "-"
+ , show2 month
+ , "-"
+ , show2 day
+ , "T"
+ , show2 hour
+ , ":"
+ , show2 minute
+ , ":"
+ , case properFraction second :: (Int, Pico) of
+ (int, 0 ) -> show2 int
+ (int, frac) -> show2 int ++ tail (show frac)
+ , showTZ tz
+ ]
+
+ format' w = error ("Invalid W3C Date and Time: " ++ show w)
+
+show4 :: Integral i => i -> String
+show4 i
+ | i >= 0 && i < 10 = "000" ++ show i
+ | i >= 0 && i < 100 = "00" ++ show i
+ | i >= 0 && i < 1000 = "0" ++ show i
+ | i >= 0 && i < 10000 = show i
+ | otherwise = error ("show4: the integer i must satisfy 0 <= i < 10000: " ++ show i)
+
+show2 :: Integral i => i -> String
+show2 i
+ | i >= 0 && i < 10 = "0" ++ show i
+ | i >= 0 && i < 100 = show i
+ | otherwise = error ("show2: the integer i must satisfy 0 <= i < 100: " ++ show i)
+
+showTZ :: TimeZone -> String
+showTZ tz
+ = case timeZoneMinutes tz of
+ offset | offset < 0 -> '-' : showTZ' (negate offset)
+ | offset == 0 -> "Z"
+ | otherwise -> '+' : showTZ' offset
+ where
+ showTZ' offset
+ = let h = offset `div` 60
+ m = offset - h * 60
+ in
+ concat [show2 h, ":", show2 m]
-module Data.Time.W3CDateTime.Types
+module Data.Time.W3C.Types
( W3CDateTime(..)
)
where
}
deriving (Show, Eq, Typeable)
+fetch :: (Show a, Typeable a, Typeable b) =>
+ String
+ -> (a -> Maybe b)
+ -> a
+ -> ConvertResult b
+fetch name f a
+ = case f a of
+ Nothing -> convError ("No " ++ name ++ " information in the given value") a
+ Just b -> return b
+
instance Convertible Day W3CDateTime where
safeConvert day
= case toGregorian day of
, w3cTimeZone = Nothing
}
-fetch :: (Show a, Typeable a, Typeable b) =>
- String
- -> (a -> Maybe b)
- -> a
- -> ConvertResult b
-fetch name f a
- = case f a of
- Nothing -> convError ("No " ++ name ++ " information in the given value") a
- Just b -> return b
-
instance Convertible W3CDateTime Day where
safeConvert w3c
= do let y = w3cYear w3c
Name: time-w3c
Version: 0.1
Synopsis: Parse, format and convert W3C Date and Time
-
--- A longer description of the package.
--- Description:
+Description:
+ This module provides functionalities to parse and format W3C
+ Date and Time. The module can also be used to convert it
+ from/to "Data.Time.Calendar.Day" and
+ "Data.Time.LocalTime.ZonedTime".
License: PublicDomain
License-file: COPYING
Homepage: http://cielonegro.org/W3CDateTime.html
Category: Web
Build-type: Simple
-Cabal-version: >=1.2
+Cabal-version: >= 1.2.3
Extra-source-files:
Library
Exposed-modules:
- Data.Time.W3CDateTime
- Data.Time.W3CDateTime.Types
+ Data.Time.W3C
+ Data.Time.W3C.Format
+ Data.Time.W3C.Types
Build-depends:
base >= 4 && < 5,
Extensions:
DeriveDataTypeable
+ FlexibleContexts
MultiParamTypeClasses
GHC-Options: