aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorGravatar Adam Chlipala <adamc@hcoop.net>2009-09-15 10:50:49 -0400
committerGravatar Adam Chlipala <adamc@hcoop.net>2009-09-15 10:50:49 -0400
commit0b1061c946b2cca6866008d0907cd0f138be03dc (patch)
treed263c421be4668aa8c7963ee88335bf2ef3d23f8 /src
parent3a19d9df41599153b83a77006b87dcae49c97035 (diff)
Fix Postgres timestamp round-tripping
Diffstat (limited to 'src')
-rw-r--r--src/c/urweb.c30
-rw-r--r--src/postgres.sml2
2 files changed, 31 insertions, 1 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};
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 ", ",