Random improvements
authorAdam Chlipala <adamc@hcoop.net>
Sun, 10 Aug 2003 02:21:03 +0000 (02:21 +0000)
committerAdam Chlipala <adamc@hcoop.net>
Sun, 10 Aug 2003 02:21:03 +0000 (02:21 +0000)
libpq/pg.sml
libpq/sources.cm
sql_driver.sig

index 928bf98..723df54 100644 (file)
@@ -192,7 +192,12 @@ struct
 
     fun pad' (s, 0) = s
       | pad' (s, n) = pad' ("0" ^ s, n-1)
-    fun pad (n, i) = pad' (Int.toString n, i)
+    fun pad (n, i) =
+       let
+           val base = Int.toString n
+       in
+           pad' (base, Int.max (i - size base, 0))
+       end
 
     fun offsetStr NONE = "+00"
       | offsetStr (SOME n) =
@@ -205,14 +210,15 @@ struct
                "+" ^ pad (n, 2)
        end
 
-    fun timestampToSql t =
+    fun timestampToSqlUnquoted t =
        let
            val d = Date.fromTimeLocal t
        in
-           "'" ^ pad (Date.year d, 4) ^ "-" ^ pad (fromMonth (Date.month d), 2) ^ "-" ^ pad (Date.day d, 2) ^
+           pad (Date.year d, 4) ^ "-" ^ pad (fromMonth (Date.month d), 2) ^ "-" ^ pad (Date.day d, 2) ^
            " " ^ pad (Date.hour d, 2) ^ ":" ^ pad (Date.minute d, 2) ^ ":" ^ pad (Date.second d, 2) ^
-           ".000000+" ^ offsetStr (Date.offset d) ^ "'"
+           ".000000" ^ offsetStr (Date.offset d)
        end
+    fun timestampToSql t = "'" ^ timestampToSqlUnquoted t ^ "'"
     fun timestampFromSql s =
        let
            val tokens = String.tokens (fn ch => ch = #"-" orelse ch = #" " orelse ch = #":"
@@ -224,7 +230,17 @@ struct
                                        month = toMonth (intFromSql mon),
                                        offset = SOME (Time.fromSeconds (Int32.fromInt (intFromSql offset * 3600))),
                                        second = intFromSql second div 1000, year = intFromSql year})
-             | _ => raise Format "Invalid timestamp"
+             | [year, mon, day, hour, minute, second, _] =>
+               Date.toTime (Date.date {day = intFromSql day, hour = intFromSql mon, minute = intFromSql minute,
+                                       month = toMonth (intFromSql mon),
+                                       offset = NONE,
+                                       second = intFromSql second div 1000, year = intFromSql year})
+             | [year, mon, day, hour, minute, second] =>
+               Date.toTime (Date.date {day = intFromSql day, hour = intFromSql mon, minute = intFromSql minute,
+                                       month = toMonth (intFromSql mon),
+                                       offset = NONE,
+                                       second = intFromSql second div 1000, year = intFromSql year})
+             | _ => raise Format ("Invalid timestamp " ^ s)
        end
                
 
index dd36ba9..d4485a0 100644 (file)
@@ -28,4 +28,3 @@ is
      FFI/libpq.h.cm (*: make ()*)
 
      pg.sml
-     pg.sig
index 2b1016e..c96fbdb 100644 (file)
@@ -45,6 +45,7 @@ sig
     val stringToSql : string -> string
     val stringFromSql : string -> string
     val timestampToSql : timestamp -> string
+    val timestampToSqlUnquoted : timestamp -> string
     val timestampFromSql : string -> timestamp
     val realToSql : real -> string
     val realFromSql : string -> real