packages feed

xsd 0.2 → 0.3.0

raw patch · 2 files changed

+36/−8 lines, 2 files

Files

Text/XML/XSD/DateTime.hs view
@@ -5,24 +5,37 @@                               dateTime,                               toZonedTime,                               zonedTime',-                              zonedTime+                              zonedTime,+                              toUTCTime,+                              utcTime',+                              utcTime                             ) where  import Text.ParserCombinators.Parsec import Data.Maybe import Data.Char import Data.Time+import Data.Function import Control.Monad import Control.Monad.Instances+import Control.Arrow  -- | XSD @dateTime@ data structure <http://www.w3.org/TR/xmlschema-2/#dateTime> data DateTime = DateTime Bool Int Int Int Int Int Int (Maybe String) Offset-  deriving Eq  instance Show DateTime where   show (DateTime neg yy mm dd hhh mmm sss ssss tz) =     join [if neg then "-" else [], showy yy, "-", showi mm, "-", showi dd, "T", showi hhh, ":", showi mmm, ":", showi sss, seconds ssss,show tz] +instance Read DateTime where+  readList s = [(maybeToList (dateTime s), [])]++instance Eq DateTime where+  (==) = (==) `on` (zonedTimeToLocalTime . toZonedTime &&& zonedTimeZone . toZonedTime)++instance Ord DateTime where+  compare = compare `on` (zonedTimeToLocalTime . toZonedTime &&& zonedTimeZone . toZonedTime)+ -- | Parses the string into a @dateTime@ or may fail with a parse error. dateTime' :: String -> Either ParseError DateTime dateTime' = parse parseDateTime "DateTime parser"@@ -33,7 +46,10 @@  -- | Converts a @dateTime@ to a zoned time. toZonedTime :: DateTime -> ZonedTime-toZonedTime (DateTime neg yy mm dd hhh mmm sss ssss tz) = ZonedTime (LocalTime (fromGregorian (fromIntegral ((if neg then negate else id) yy)) mm dd) (TimeOfDay hhh mmm (realToFrac (read (show sss ++ seconds ssss) :: Double)))) (timeZone tz)+toZonedTime (DateTime neg yy mm dd hhh mmm sss ssss tz) =+  ZonedTime (+    LocalTime (fromGregorian (fromIntegral ((if neg then negate else id) yy)) mm dd) (+    TimeOfDay hhh mmm (realToFrac (read (show sss ++ seconds ssss) :: Double)))) (timeZone tz)  -- | Parses the string in a @dateTime@ then converts to a zoned time and may fail with a parse error. zonedTime' :: String -> Either ParseError ZonedTime@@ -43,6 +59,18 @@ zonedTime :: String -> Maybe ZonedTime zonedTime = fmap toZonedTime . dateTime +-- | Converts a @dateTime@ to a UTC time.+toUTCTime :: DateTime -> UTCTime+toUTCTime = uncurry localTimeToUTC . (zonedTimeZone &&& zonedTimeToLocalTime) . toZonedTime++-- | Parses the string in a @dateTime@ then converts to a UTC time and may fail with a parse error.+utcTime' :: String -> Either ParseError UTCTime+utcTime' = fmap toUTCTime . dateTime'++-- | Parses the string in a @dateTime@ then converts to a UTC time and may fail.+utcTime :: String -> Maybe UTCTime+utcTime = fmap toUTCTime . dateTime+ -- not exported  data Offset = Offset Bool (Maybe Bool) (Maybe Int) (Maybe Int)@@ -115,10 +143,10 @@                          then unexpected "zero digit"                          else return d -p2i :: GenParser Char st Int-p2i = liftM2 (\a b -> read [a, b]) digit digit- p2imax :: Int -> GenParser Char st Int-p2imax m = p2i >>= \n -> if n > m then unexpected ("value " ++ show n ++ " exceeded maximum " ++ show m) else return n+p2imax m = do a <- digit+              b <- digit+              let n = read [a, b]+              if n > m then unexpected ("value " ++ show n ++ " exceeded maximum " ++ show m) else return n  -- examples = ["2009-10-10T03:10:10-05:00", "2119-10-10T03:10:10.4-15:26", "0009-10-10T03:10:10+15:00", "2009-10-10T03:10:10Z", "-2009-05-10T21:08:59-05:00", "-9399-12-31T13:10:10+15:00", "-2009-10-10T03:10:10Z"]
xsd.cabal view
@@ -1,5 +1,5 @@ Name:                xsd-Version:             0.2+Version:             0.3.0 License:             BSD3 License-File:        LICENSE Synopsis:            XML Schema data structures