# HG changeset patch # User Adam Chlipala # Date 1225393035 14400 # Node ID 1c27f03d9bd21e402977ccd708d80652fd5f9daa # Parent 1a4c1b5f4d8f735aa35e6a951f3977e5aa991fbe Reading timestamps from SQL diff -r 1a4c1b5f4d8f -r 1c27f03d9bd2 src/c/urweb.c --- a/src/c/urweb.c Thu Oct 30 14:40:42 2008 -0400 +++ b/src/c/urweb.c Thu Oct 30 14:57:15 2008 -0400 @@ -728,6 +728,7 @@ } #define TIME_FMT "%x %X" +#define TIME_FMT_PG "%Y-%m-%d %T" uw_Basis_string uw_Basis_htmlifyTime(uw_context ctx, uw_Basis_time t) { size_t len; @@ -950,10 +951,10 @@ } uw_Basis_time *uw_Basis_stringToTime(uw_context ctx, uw_Basis_string s) { - char *end = strchr(s, 0); + char *dot = strchr(s, '.'), *end = strchr(s, 0); struct tm stm; - if (strptime(s, TIME_FMT, &stm) == end) { + if ((dot ? (*dot = 0, strptime(s, TIME_FMT_PG, &stm)) : strptime(s, TIME_FMT, &stm)) == end) { uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time)); *r = mktime(&stm); return r; @@ -992,11 +993,24 @@ } uw_Basis_time uw_Basis_stringToTime_error(uw_context ctx, uw_Basis_string s) { - char *end = strchr(s, 0); + char *dot = strchr(s, '.'), *end = strchr(s, 0); struct tm stm = {}; - if (strptime(s, TIME_FMT, &stm) == end) - return mktime(&stm); - else - uw_error(ctx, FATAL, "Can't parse time: %s", s); + if (dot) { + *dot = 0; + if (strptime(s, TIME_FMT_PG, &stm)) { + *dot = '.'; + return mktime(&stm); + } + else { + *dot = '.'; + uw_error(ctx, FATAL, "Can't parse time: %s", s); + } + } + else { + if (strptime(s, TIME_FMT, &stm) == end) + return mktime(&stm); + else + uw_error(ctx, FATAL, "Can't parse time: %s", s); + } } diff -r 1a4c1b5f4d8f -r 1c27f03d9bd2 src/cjr_print.sml --- a/src/cjr_print.sml Thu Oct 30 14:40:42 2008 -0400 +++ b/src/cjr_print.sml Thu Oct 30 14:57:15 2008 -0400 @@ -403,6 +403,7 @@ else box [string "uw_Basis_strdup(ctx, ", e, string ")"] | TFfi ("Basis", "bool") => box [string "uw_Basis_stringToBool_error(ctx, ", e, string ")"] + | TFfi ("Basis", "time") => box [string "uw_Basis_stringToTime_error(ctx, ", e, string ")"] | _ => (ErrorMsg.errorAt loc "Don't know how to unmarshal type from SQL"; Print.eprefaces' [("Type", p_typ env tAll)]; string "ERROR") @@ -1395,6 +1396,7 @@ | TFfi ("Basis", "float") => "float8" | TFfi ("Basis", "string") => "text" | TFfi ("Basis", "bool") => "bool" + | TFfi ("Basis", "time") => "timestamp" | _ => (ErrorMsg.errorAt loc "Don't know SQL equivalent of type"; Print.eprefaces' [("Type", p_typ env tAll)]; "ERROR") diff -r 1a4c1b5f4d8f -r 1c27f03d9bd2 tests/time.ur --- a/tests/time.ur Thu Oct 30 14:40:42 2008 -0400 +++ b/tests/time.ur Thu Oct 30 14:57:15 2008 -0400 @@ -1,4 +1,12 @@ +table t : { Id : int, Time : time } + val now : time = readError "10/30/08 14:35:42" val later : time = readError "10/30/08 14:37:42" -fun main () = return {[now]}, {[now = now]}, {[now = later]}, {[later < now]}, {[now < later]} +fun main () = + xml <- queryX (SELECT * FROM t) + (fn r => {[r.T.Id]}: {[r.T.Time]}
); + return + {xml} + {[now]}, {[now = now]}, {[now = later]}, {[later < now]}, {[now < later]} + diff -r 1a4c1b5f4d8f -r 1c27f03d9bd2 tests/time.urp --- a/tests/time.urp Thu Oct 30 14:40:42 2008 -0400 +++ b/tests/time.urp Thu Oct 30 14:57:15 2008 -0400 @@ -1,3 +1,5 @@ debug +database dbname=time +sql time.sql time