diff src/c/urweb.c @ 1629:438561303d02

timeFormat .urp directive
author Adam Chlipala <adam@chlipala.net>
date Sun, 04 Dec 2011 14:40:12 -0500
parents 43f22a8f76cc
children 4682b312e9d5
line wrap: on
line diff
--- a/src/c/urweb.c	Sat Dec 03 17:25:51 2011 -0500
+++ b/src/c/urweb.c	Sun Dec 04 14:40:12 2011 -0500
@@ -2151,40 +2151,14 @@
 #define TIME_FMT "%x %X"
 #define TIME_FMT_PG "%Y-%m-%d %T"
 
+uw_Basis_string uw_Basis_timeToString(uw_context, uw_Basis_time);
+
 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;
-
-  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 "<i>Invalid time</i>";
+  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, "<i>Invalid time</i>");
-    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 @@
     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 "<Invalid time>";
-}
-
 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 @@
     return "<Invalid time>";
 }
 
+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);