diff options
author | Adam Chlipala <adamc@hcoop.net> | 2009-09-15 10:50:49 -0400 |
---|---|---|
committer | Adam Chlipala <adamc@hcoop.net> | 2009-09-15 10:50:49 -0400 |
commit | 0b1061c946b2cca6866008d0907cd0f138be03dc (patch) | |
tree | d263c421be4668aa8c7963ee88335bf2ef3d23f8 /src/c | |
parent | 3a19d9df41599153b83a77006b87dcae49c97035 (diff) |
Fix Postgres timestamp round-tripping
Diffstat (limited to 'src/c')
-rw-r--r-- | src/c/urweb.c | 30 |
1 files changed, 30 insertions, 0 deletions
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}; |