]> gitweb @ CieloNegro.org - time-w3c.git/blobdiff - Data/Time/W3CDateTime.hs
data W3CDateTime
[time-w3c.git] / Data / Time / W3CDateTime.hs
index 3f10f99991f388f0b445a99e075698eae3a217de..a0de70ee8d554b19a22eff6f2d83e4772d4bedbf 100644 (file)
@@ -1,4 +1,55 @@
 module Data.Time.W3CDateTime
-    (
+    ( W3CDateTime
     )
     where
+
+import Data.Convertible
+import Data.Fixed
+import Data.Time
+import Data.Typeable
+
+
+-- This data type is /partially ordered/ so we can't make it an
+-- instance of Ord (e.g. "2010" and "2010-01" can't be compared).
+data W3CDateTime
+    = W3CDateTime {
+        w3cYear     :: !Integer
+      , w3cMonth    :: !(Maybe Int)
+      , w3cDay      :: !(Maybe Int)
+      , w3cHour     :: !(Maybe Int)
+      , w3cMinute   :: !(Maybe Int)
+      , w3cSecond   :: !(Maybe Pico)
+      , w3cTimeZone :: !(Maybe TimeZone)
+      }
+    deriving (Show, Eq, Typeable)
+
+empty :: W3CDateTime
+empty = W3CDateTime {
+          w3cYear     = 0
+        , w3cMonth    = Nothing
+        , w3cDay      = Nothing
+        , w3cHour     = Nothing
+        , w3cMinute   = Nothing
+        , w3cSecond   = Nothing
+        , w3cTimeZone = Nothing
+        }
+
+instance Convertible Day W3CDateTime where
+    safeConvert day
+        = case toGregorian day of
+            (y, m, d) -> return empty {
+                           w3cYear  = y
+                         , w3cMonth = Just m
+                         , w3cDay   = Just d
+                         }
+
+instance Convertible W3CDateTime Day where
+    safeConvert w3c
+        = do let y = w3cYear w3c
+             m <- case w3cMonth w3c of
+                    Just m  -> return m
+                    Nothing -> convError "No month info" w3c
+             d <- case w3cDay w3c of
+                    Just d  -> return d
+                    Nothing -> convError "No day info" w3c
+             return $ fromGregorian y m d
\ No newline at end of file