]> gitweb @ CieloNegro.org - time-w3c.git/commitdiff
renamed modules
authorPHO <pho@cielonegro.org>
Thu, 11 Mar 2010 07:22:26 +0000 (16:22 +0900)
committerPHO <pho@cielonegro.org>
Thu, 11 Mar 2010 07:22:26 +0000 (16:22 +0900)
Data/Time/W3C.hs [new file with mode: 0644]
Data/Time/W3C/Format.hs [new file with mode: 0644]
Data/Time/W3C/Types.hs [moved from Data/Time/W3CDateTime/Types.hs with 98% similarity]
Data/Time/W3CDateTime.hs [deleted file]
time-w3c.cabal

diff --git a/Data/Time/W3C.hs b/Data/Time/W3C.hs
new file mode 100644 (file)
index 0000000..a6f19f4
--- /dev/null
@@ -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 (file)
index 0000000..d83d8c3
--- /dev/null
@@ -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]
similarity index 98%
rename from Data/Time/W3CDateTime/Types.hs
rename to Data/Time/W3C/Types.hs
index c7573eb09814481b69bd9402e183d9282334790d..9f48f68be58dbc627f8f08a6e4d6b71a612f11e9 100644 (file)
@@ -1,4 +1,4 @@
-module Data.Time.W3CDateTime.Types
+module Data.Time.W3C.Types
     ( W3CDateTime(..)
     )
     where
     ( W3CDateTime(..)
     )
     where
@@ -23,6 +23,16 @@ data W3CDateTime
       }
     deriving (Show, Eq, Typeable)
 
       }
     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
 instance Convertible Day W3CDateTime where
     safeConvert day
         = case toGregorian day of
@@ -36,16 +46,6 @@ instance Convertible Day W3CDateTime where
                                      , w3cTimeZone = Nothing
                                      }
 
                                      , 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
 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 (file)
index b1c1230..0000000
+++ /dev/null
@@ -1,6 +0,0 @@
-module Data.Time.W3CDateTime
-    ( W3CDateTime(..)
-    )
-    where
-
-import Data.Time.W3CDateTime.Types
index 96a224a364c7f8cf952edfad6a3506f306b74c37..6b08679ce6627e9768efa6f34752df0b031826a0 100644 (file)
@@ -1,9 +1,11 @@
 Name:                time-w3c
 Version:             0.1
 Synopsis:            Parse, format and convert W3C Date and Time
 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
 
 License:             PublicDomain
 License-file:        COPYING
@@ -13,13 +15,14 @@ Stability:           Experimental
 Homepage:            http://cielonegro.org/W3CDateTime.html
 Category:            Web
 Build-type:          Simple
 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:
 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,
 
     Build-depends:
         base >= 4 && < 5,
@@ -28,6 +31,7 @@ Library
 
     Extensions:
         DeriveDataTypeable
 
     Extensions:
         DeriveDataTypeable
+        FlexibleContexts
         MultiParamTypeClasses
 
     GHC-Options:
         MultiParamTypeClasses
 
     GHC-Options: