@@ -4,10 +4,13 @@ module Database.Postgres.SqlValue
44 , toSql
55 ) where
66
7- import Prelude ((<<<))
7+ import Prelude
8+ import Data.Enum (fromEnum )
89import Data.Int (toNumber )
910import Data.Maybe (Maybe (..))
10- import Data.Date as Date
11+ import Data.Date (year , month , day )
12+ import Data.DateTime (DateTime (DateTime))
13+ import Data.Time (hour , minute , second )
1114
1215foreign import data SqlValue :: *
1316
@@ -27,8 +30,20 @@ instance isSqlValueMaybe :: (IsSqlValue a) => IsSqlValue (Maybe a) where
2730 toSql Nothing = nullSqlValue
2831 toSql (Just x) = toSql x
2932
30- instance isSqlValueDate :: IsSqlValue Date.Date where
31- toSql = unsafeToSqlValue
33+ instance isSqlValueDateTime :: IsSqlValue DateTime where
34+ toSql = toSql <<< format
35+ where
36+ format (DateTime d t)
37+ = show (fromEnum (year d)) <> " -"
38+ <> zeroPad (fromEnum (month d)) <> " -"
39+ <> zeroPad (fromEnum (day d)) <> " "
40+ <> zeroPad (fromEnum (hour t)) <> " :"
41+ <> zeroPad (fromEnum (minute t)) <> " :"
42+ <> zeroPad (fromEnum (second t))
43+
44+ zeroPad :: Int -> String
45+ zeroPad i | i < 10 = " 0" <> (show i)
46+ zeroPad i = show i
3247
3348foreign import unsafeToSqlValue :: forall a . a -> SqlValue
3449
0 commit comments