From af108dfe039a25905becdd58e6bd609e6e8cc6be Mon Sep 17 00:00:00 2001 From: Adam Chlipala Date: Tue, 15 Sep 2009 10:50:49 -0400 Subject: Fix Postgres timestamp round-tripping --- src/c/urweb.c | 30 ++++++++++++++++++++++++++++++ src/postgres.sml | 2 +- 2 files changed, 31 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/c/urweb.c b/src/c/urweb.c index 21e50893..03757de4 100644 --- a/src/c/urweb.c +++ b/src/c/urweb.c @@ -2160,6 +2160,7 @@ char *uw_Basis_sqlifyTime(uw_context ctx, uw_Basis_time t) { 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 @@ char *uw_Basis_attrifyTime(uw_context ctx, uw_Basis_time t) { 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_stringToTime_error(uw_context ctx, uw_Basis_string s) { } } +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 --git a/src/postgres.sml b/src/postgres.sml index bf58fe1a..f3942db4 100644 --- a/src/postgres.sml +++ b/src/postgres.sml @@ -506,7 +506,7 @@ fun p_getcol {loc, wontLeakStrings, col = i, typ = t} = 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 ", ", -- cgit v1.2.3