changeset 939:38a376dc7401

Fix Postgres timestamp round-tripping
author Adam Chlipala <adamc@hcoop.net>
date Tue, 15 Sep 2009 10:50:49 -0400
parents 6b1d960e2616
children e2be476673f2
files include/urweb.h src/c/urweb.c src/postgres.sml
diffstat 3 files changed, 33 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/include/urweb.h	Tue Sep 15 10:43:01 2009 -0400
+++ b/include/urweb.h	Tue Sep 15 10:50:49 2009 -0400
@@ -171,6 +171,8 @@
 uw_Basis_channel uw_Basis_stringToChannel_error(uw_context, uw_Basis_string);
 uw_Basis_client uw_Basis_stringToClient_error(uw_context, uw_Basis_string);
 
+uw_Basis_time uw_Basis_unsqlTime(uw_context, uw_Basis_string);
+
 uw_Basis_string uw_Basis_requestHeader(uw_context, uw_Basis_string);
 
 void uw_write_header(uw_context, uw_Basis_string);
--- a/src/c/urweb.c	Tue Sep 15 10:43:01 2009 -0400
+++ b/src/c/urweb.c	Tue Sep 15 10:50:49 2009 -0400
@@ -2160,6 +2160,7 @@
 
   if (localtime_r(&t, &stm)) {
     s = uw_malloc(ctx, TIMES_MAX);
+    --stm.tm_hour;
     len = strftime(s, TIMES_MAX, TIME_FMT, &stm);
     r = uw_malloc(ctx, len + 14);
     sprintf(r, "'%s'::timestamp", s);
@@ -2176,6 +2177,7 @@
   if (localtime_r(&t, &stm)) {
     uw_check_heap(ctx, TIMES_MAX);
     r = ctx->heap.front;
+    --stm.tm_hour;
     len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
     ctx->heap.front += len+1;
     return r;
@@ -2420,6 +2422,34 @@
   }
 }
 
+uw_Basis_time uw_Basis_unsqlTime(uw_context ctx, uw_Basis_string s) {
+  char *dot = strchr(s, '.'), *end = strchr(s, 0);
+  struct tm stm = {};
+
+  if (dot) {
+    *dot = 0;
+    if (strptime(s, TIME_FMT_PG, &stm)) {
+      *dot = '.';
+      --stm.tm_hour;
+      return mktime(&stm);
+    }
+    else {
+      *dot = '.';
+      uw_error(ctx, FATAL, "Can't parse time: %s", s);
+    }
+  }
+  else {
+    if (strptime(s, TIME_FMT_PG, &stm) == end) {
+      --stm.tm_hour;
+      return mktime(&stm);
+    } else if (strptime(s, TIME_FMT, &stm) == end) {
+      --stm.tm_hour;
+      return mktime(&stm);
+    } else
+      uw_error(ctx, FATAL, "Can't parse time: %s", s);
+  }
+}
+
 uw_Basis_blob uw_Basis_stringToBlob_error(uw_context ctx, uw_Basis_string s, size_t len) {
   char *r = ctx->heap.front;
   uw_Basis_blob b = {len, r};
--- a/src/postgres.sml	Tue Sep 15 10:43:01 2009 -0400
+++ b/src/postgres.sml	Tue Sep 15 10:50:49 2009 -0400
@@ -506,7 +506,7 @@
                 else
                     box [string "uw_strdup(ctx, ", e, string ")"]
               | Bool => box [string "uw_Basis_stringToBool_error(ctx, ", e, string ")"]
-              | Time => box [string "uw_Basis_stringToTime_error(ctx, ", e, string ")"]
+              | Time => box [string "uw_Basis_unsqlTime(ctx, ", e, string ")"]
               | Blob => box [string "uw_Basis_stringToBlob_error(ctx, ",
                              e,
                              string ", ",