# HG changeset patch # User Adam Chlipala # Date 1253026249 14400 # Node ID 38a376dc7401f6ac355c3fe39de5c4c0be585916 # Parent 6b1d960e2616c8b475eb7440fa0864ffc24a8062 Fix Postgres timestamp round-tripping diff -r 6b1d960e2616 -r 38a376dc7401 include/urweb.h --- 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); diff -r 6b1d960e2616 -r 38a376dc7401 src/c/urweb.c --- 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}; diff -r 6b1d960e2616 -r 38a376dc7401 src/postgres.sml --- 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 ", ",