diff options
-rw-r--r-- | include/urweb.h | 2 | ||||
-rw-r--r-- | src/c/urweb.c | 30 | ||||
-rw-r--r-- | src/postgres.sml | 2 |
3 files changed, 33 insertions, 1 deletions
diff --git a/include/urweb.h b/include/urweb.h index b6531a39..926d8523 100644 --- a/include/urweb.h +++ b/include/urweb.h @@ -171,6 +171,8 @@ uw_Basis_blob uw_Basis_stringToBlob_error(uw_context, uw_Basis_string, size_t); 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 --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 ", ", |