changeset 1364:0dec38af601c

Fix Postgres date serialization
author Adam Chlipala <adam@chlipala.net>
date Fri, 24 Dec 2010 17:18:28 -0500
parents 7dd8a6704265
children b02cb9da5686
files include/urweb.h src/c/urweb.c src/postgres.sml
diffstat 3 files changed, 18 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/include/urweb.h	Fri Dec 24 16:41:10 2010 -0500
+++ b/include/urweb.h	Fri Dec 24 17:18:28 2010 -0500
@@ -165,6 +165,7 @@
 uw_Basis_string uw_Basis_sqlifyTimeN(uw_context, uw_Basis_time*);
 
 char *uw_Basis_ensqlBool(uw_Basis_bool);
+char *uw_Basis_ensqlTime(uw_context ctx, uw_Basis_time);
 
 char *uw_Basis_jsifyString(uw_context, uw_Basis_string);
 char *uw_Basis_jsifyChar(uw_context, uw_Basis_char);
--- a/src/c/urweb.c	Fri Dec 24 16:41:10 2010 -0500
+++ b/src/c/urweb.c	Fri Dec 24 17:18:28 2010 -0500
@@ -2513,7 +2513,7 @@
 
   if (localtime_r(&t, &stm)) {
     s = uw_malloc(ctx, TIMES_MAX);
-    len = strftime(s, TIMES_MAX, TIME_FMT, &stm);
+    len = strftime(s, TIMES_MAX, TIME_FMT_PG, &stm);
     r = uw_malloc(ctx, len + 14);
     sprintf(r, "'%s'::timestamp", s);
     return r;
@@ -2536,6 +2536,21 @@
     return "<Invalid time>";
 }
 
+char *uw_Basis_ensqlTime(uw_context ctx, uw_Basis_time t) {
+  size_t len;
+  char *r;
+  struct tm stm;
+
+  if (localtime_r(&t, &stm)) {
+    uw_check_heap(ctx, TIMES_MAX);
+    r = ctx->heap.front;
+    len = strftime(r, TIMES_MAX, TIME_FMT_PG, &stm);
+    ctx->heap.front += len+1;
+    return r;
+  } else
+    return "<Invalid time>";
+}
+
 char *uw_Basis_sqlifyTimeN(uw_context ctx, uw_Basis_time *t) {
   if (t == NULL)
     return "NULL";
--- a/src/postgres.sml	Fri Dec 24 16:41:10 2010 -0500
+++ b/src/postgres.sml	Fri Dec 24 17:18:28 2010 -0500
@@ -654,7 +654,7 @@
       | String => e
       | Char => box [string "uw_Basis_attrifyChar(ctx, ", e, string ")"]
       | Bool => box [string "(", e, string " ? \"TRUE\" : \"FALSE\")"]
-      | Time => box [string "uw_Basis_attrifyTime(ctx, ", e, string ")"]
+      | Time => box [string "uw_Basis_ensqlTime(ctx, ", e, string ")"]
       | Blob => box [e, string ".data"]
       | Channel => box [string "uw_Basis_attrifyChannel(ctx, ", e, string ")"]
       | Client => box [string "uw_Basis_attrifyClient(ctx, ", e, string ")"]