From fb743c0b822f29bac00c39e762ddd57dc65ee28f Mon Sep 17 00:00:00 2001 From: PHO Date: Thu, 11 Mar 2010 16:22:26 +0900 Subject: [PATCH] renamed modules --- Data/Time/W3C.hs | 8 +++ Data/Time/W3C/Format.hs | 81 +++++++++++++++++++++++++ Data/Time/{W3CDateTime => W3C}/Types.hs | 22 +++---- Data/Time/W3CDateTime.hs | 6 -- time-w3c.cabal | 16 +++-- 5 files changed, 110 insertions(+), 23 deletions(-) create mode 100644 Data/Time/W3C.hs create mode 100644 Data/Time/W3C/Format.hs rename Data/Time/{W3CDateTime => W3C}/Types.hs (98%) delete mode 100644 Data/Time/W3CDateTime.hs diff --git a/Data/Time/W3C.hs b/Data/Time/W3C.hs new file mode 100644 index 0000000..a6f19f4 --- /dev/null +++ b/Data/Time/W3C.hs @@ -0,0 +1,8 @@ +module Data.Time.W3C + ( W3CDateTime(..) + , format + ) + where + +import Data.Time.W3C.Format +import Data.Time.W3C.Types diff --git a/Data/Time/W3C/Format.hs b/Data/Time/W3C/Format.hs new file mode 100644 index 0000000..d83d8c3 --- /dev/null +++ b/Data/Time/W3C/Format.hs @@ -0,0 +1,81 @@ +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] diff --git a/Data/Time/W3CDateTime/Types.hs b/Data/Time/W3C/Types.hs similarity index 98% rename from Data/Time/W3CDateTime/Types.hs rename to Data/Time/W3C/Types.hs index c7573eb..9f48f68 100644 --- a/Data/Time/W3CDateTime/Types.hs +++ b/Data/Time/W3C/Types.hs @@ -1,4 +1,4 @@ -module Data.Time.W3CDateTime.Types +module Data.Time.W3C.Types ( W3CDateTime(..) ) where @@ -23,6 +23,16 @@ data W3CDateTime } 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 @@ -36,16 +46,6 @@ instance Convertible Day W3CDateTime where , 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 diff --git a/Data/Time/W3CDateTime.hs b/Data/Time/W3CDateTime.hs deleted file mode 100644 index b1c1230..0000000 --- a/Data/Time/W3CDateTime.hs +++ /dev/null @@ -1,6 +0,0 @@ -module Data.Time.W3CDateTime - ( W3CDateTime(..) - ) - where - -import Data.Time.W3CDateTime.Types diff --git a/time-w3c.cabal b/time-w3c.cabal index 96a224a..6b08679 100644 --- a/time-w3c.cabal +++ b/time-w3c.cabal @@ -1,9 +1,11 @@ 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 @@ -13,13 +15,14 @@ Stability: Experimental 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, @@ -28,6 +31,7 @@ Library Extensions: DeriveDataTypeable + FlexibleContexts MultiParamTypeClasses GHC-Options: -- 2.40.0