diff src/c/urweb.c @ 939:38a376dc7401

Fix Postgres timestamp round-tripping
author Adam Chlipala <adamc@hcoop.net>
date Tue, 15 Sep 2009 10:50:49 -0400
parents d136bc34e4ca
children 53b9aeac676c
line wrap: on
line diff
--- 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};