From 226fb11ff2da90b7b68dad1b29b9688f880a06a3 Mon Sep 17 00:00:00 2001 From: Adam Chlipala Date: Sun, 4 Dec 2011 14:40:12 -0500 Subject: timeFormat .urp directive --- src/c/urweb.c | 54 ++++++++---------------------------------------------- src/cjr_print.sml | 3 ++- src/compiler.sml | 1 + src/jscomp.sml | 1 + src/settings.sig | 3 +++ src/settings.sml | 6 +++++- 6 files changed, 20 insertions(+), 48 deletions(-) (limited to 'src') diff --git a/src/c/urweb.c b/src/c/urweb.c index 8ebd9a2f..270443c6 100644 --- a/src/c/urweb.c +++ b/src/c/urweb.c @@ -2151,40 +2151,14 @@ uw_unit uw_Basis_htmlifyBool_w(uw_context ctx, uw_Basis_bool b) { #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; - char *r; - struct tm stm = {}; - stm.tm_isdst = -1; +uw_Basis_string uw_Basis_timeToString(uw_context, uw_Basis_time); - if (localtime_r(&t.seconds, &stm)) { - uw_check_heap(ctx, TIMES_MAX); - r = ctx->heap.front; - len = strftime(r, TIMES_MAX, TIME_FMT, &stm); - ctx->heap.front += len+1; - return r; - } else - return "Invalid time"; +uw_Basis_string uw_Basis_htmlifyTime(uw_context ctx, uw_Basis_time t) { + return uw_Basis_htmlifyString(ctx, uw_Basis_timeToString(ctx, t)); } uw_unit uw_Basis_htmlifyTime_w(uw_context ctx, uw_Basis_time t) { - size_t len; - char *r; - struct tm stm = {}; - stm.tm_isdst = -1; - - if (localtime_r(&t.seconds, &stm)) { - uw_check(ctx, TIMES_MAX); - r = ctx->page.front; - len = strftime(r, TIMES_MAX, TIME_FMT, &stm); - ctx->page.front += len; - } else { - uw_check(ctx, 20); - strcpy(ctx->page.front, "Invalid time"); - ctx->page.front += 19; - } - - return uw_unit_v; + return uw_Basis_htmlifyString_w(ctx, uw_Basis_timeToString(ctx, t)); } char *uw_Basis_htmlifySource(uw_context ctx, uw_Basis_source src) { @@ -2724,22 +2698,6 @@ uw_Basis_string uw_Basis_boolToString(uw_context ctx, uw_Basis_bool b) { return "True"; } -uw_Basis_string uw_Basis_timeToString(uw_context ctx, uw_Basis_time t) { - size_t len; - char *r; - struct tm stm = {}; - stm.tm_isdst = -1; - - if (localtime_r(&t.seconds, &stm)) { - uw_check_heap(ctx, TIMES_MAX); - r = ctx->heap.front; - len = strftime(r, TIMES_MAX, TIME_FMT, &stm); - ctx->heap.front += len+1; - return r; - } else - return ""; -} - uw_Basis_string uw_Basis_timef(uw_context ctx, const char *fmt, uw_Basis_time t) { size_t len; char *r; @@ -2756,6 +2714,10 @@ uw_Basis_string uw_Basis_timef(uw_context ctx, const char *fmt, uw_Basis_time t) return ""; } +uw_Basis_string uw_Basis_timeToString(uw_context ctx, uw_Basis_time t) { + return uw_Basis_timef(ctx, ctx->app->time_format, t); +} + uw_Basis_int *uw_Basis_stringToInt(uw_context ctx, uw_Basis_string s) { char *endptr; uw_Basis_int n = strtoll(s, &endptr, 10); diff --git a/src/cjr_print.sml b/src/cjr_print.sml index d51cc4ee..a5015041 100644 --- a/src/cjr_print.sml +++ b/src/cjr_print.sml @@ -3391,7 +3391,8 @@ fun p_file env (ds, ps) = "uw_db_init", "uw_db_begin", "uw_db_commit", "uw_db_rollback", "uw_db_close", "uw_handle", "uw_input_num", "uw_cookie_sig", "uw_check_url", "uw_check_mime", "uw_check_requestHeader", "uw_check_responseHeader", - case onError of NONE => "NULL" | SOME _ => "uw_onError", "my_periodics"], + case onError of NONE => "NULL" | SOME _ => "uw_onError", "my_periodics", + "\"" ^ String.toCString (Settings.getTimeFormat ()) ^ "\""], string "};", newline] end diff --git a/src/compiler.sml b/src/compiler.sml index 6c88010b..1318b561 100644 --- a/src/compiler.sml +++ b/src/compiler.sml @@ -799,6 +799,7 @@ fun parseUrp' accLibs fname = | SOME n => minHeap := n) | "alwaysInline" => Settings.addAlwaysInline arg | "noXsrfProtection" => Settings.addNoXsrfProtection arg + | "timeFormat" => Settings.setTimeFormat arg | _ => ErrorMsg.error ("Unrecognized command '" ^ cmd ^ "'"); read () diff --git a/src/jscomp.sml b/src/jscomp.sml index 16043ba5..7a5bf85d 100644 --- a/src/jscomp.sml +++ b/src/jscomp.sml @@ -1324,6 +1324,7 @@ fun process file = val script = if !foundJavaScript then lines ^ urlRules ^ String.concat (rev (#script st)) + ^ "\ntime_format = \"" ^ String.toCString (Settings.getTimeFormat ()) ^ "\";\n" else "" in diff --git a/src/settings.sig b/src/settings.sig index e3c2e7cd..8f82c8a5 100644 --- a/src/settings.sig +++ b/src/settings.sig @@ -230,4 +230,7 @@ signature SETTINGS = sig val addNoXsrfProtection : string -> unit val checkNoXsrfProtection : string -> bool + + val setTimeFormat : string -> unit + val getTimeFormat : unit -> string end diff --git a/src/settings.sml b/src/settings.sml index f4022cf9..922638f5 100644 --- a/src/settings.sml +++ b/src/settings.sml @@ -280,7 +280,7 @@ val jsFuncsBase = basisM [("alert", "alert"), ("now", "now"), ("timeToString", "showTime"), - ("htmlifyTime", "showTime"), + ("htmlifyTime", "showTimeHtml"), ("toSeconds", "toSeconds"), ("addSeconds", "addSeconds"), ("diffInSeconds", "diffInSeconds"), @@ -621,4 +621,8 @@ val noXsrfProtection = ref SS.empty fun addNoXsrfProtection s = noXsrfProtection := SS.add (!noXsrfProtection, s) fun checkNoXsrfProtection s = SS.member (!noXsrfProtection, s) +val timeFormat = ref "%c" +fun setTimeFormat v = timeFormat := v +fun getTimeFormat () = !timeFormat + end -- cgit v1.2.3