comparison src/c/urweb.c @ 939:38a376dc7401

Fix Postgres timestamp round-tripping
author Adam Chlipala <adamc@hcoop.net>
date Tue, 15 Sep 2009 10:50:49 -0400
parents d136bc34e4ca
children 53b9aeac676c
comparison
equal deleted inserted replaced
938:6b1d960e2616 939:38a376dc7401
2158 char *r, *s; 2158 char *r, *s;
2159 struct tm stm; 2159 struct tm stm;
2160 2160
2161 if (localtime_r(&t, &stm)) { 2161 if (localtime_r(&t, &stm)) {
2162 s = uw_malloc(ctx, TIMES_MAX); 2162 s = uw_malloc(ctx, TIMES_MAX);
2163 --stm.tm_hour;
2163 len = strftime(s, TIMES_MAX, TIME_FMT, &stm); 2164 len = strftime(s, TIMES_MAX, TIME_FMT, &stm);
2164 r = uw_malloc(ctx, len + 14); 2165 r = uw_malloc(ctx, len + 14);
2165 sprintf(r, "'%s'::timestamp", s); 2166 sprintf(r, "'%s'::timestamp", s);
2166 return r; 2167 return r;
2167 } else 2168 } else
2174 struct tm stm; 2175 struct tm stm;
2175 2176
2176 if (localtime_r(&t, &stm)) { 2177 if (localtime_r(&t, &stm)) {
2177 uw_check_heap(ctx, TIMES_MAX); 2178 uw_check_heap(ctx, TIMES_MAX);
2178 r = ctx->heap.front; 2179 r = ctx->heap.front;
2180 --stm.tm_hour;
2179 len = strftime(r, TIMES_MAX, TIME_FMT, &stm); 2181 len = strftime(r, TIMES_MAX, TIME_FMT, &stm);
2180 ctx->heap.front += len+1; 2182 ctx->heap.front += len+1;
2181 return r; 2183 return r;
2182 } else 2184 } else
2183 return "<Invalid time>"; 2185 return "<Invalid time>";
2414 if (strptime(s, TIME_FMT_PG, &stm) == end) 2416 if (strptime(s, TIME_FMT_PG, &stm) == end)
2415 return mktime(&stm); 2417 return mktime(&stm);
2416 else if (strptime(s, TIME_FMT, &stm) == end) 2418 else if (strptime(s, TIME_FMT, &stm) == end)
2417 return mktime(&stm); 2419 return mktime(&stm);
2418 else 2420 else
2421 uw_error(ctx, FATAL, "Can't parse time: %s", s);
2422 }
2423 }
2424
2425 uw_Basis_time uw_Basis_unsqlTime(uw_context ctx, uw_Basis_string s) {
2426 char *dot = strchr(s, '.'), *end = strchr(s, 0);
2427 struct tm stm = {};
2428
2429 if (dot) {
2430 *dot = 0;
2431 if (strptime(s, TIME_FMT_PG, &stm)) {
2432 *dot = '.';
2433 --stm.tm_hour;
2434 return mktime(&stm);
2435 }
2436 else {
2437 *dot = '.';
2438 uw_error(ctx, FATAL, "Can't parse time: %s", s);
2439 }
2440 }
2441 else {
2442 if (strptime(s, TIME_FMT_PG, &stm) == end) {
2443 --stm.tm_hour;
2444 return mktime(&stm);
2445 } else if (strptime(s, TIME_FMT, &stm) == end) {
2446 --stm.tm_hour;
2447 return mktime(&stm);
2448 } else
2419 uw_error(ctx, FATAL, "Can't parse time: %s", s); 2449 uw_error(ctx, FATAL, "Can't parse time: %s", s);
2420 } 2450 }
2421 } 2451 }
2422 2452
2423 uw_Basis_blob uw_Basis_stringToBlob_error(uw_context ctx, uw_Basis_string s, size_t len) { 2453 uw_Basis_blob uw_Basis_stringToBlob_error(uw_context ctx, uw_Basis_string s, size_t len) {