changeset 1632:4682b312e9d5

Fix client-side [int] parsing and extend server-side [time] parsing to support a format that also works portably in JavaScript
author Adam Chlipala <adam@chlipala.net>
date Sun, 04 Dec 2011 16:32:06 -0500
parents 747f94ac5fc2
children deeeb036c8ed
files lib/js/urweb.js src/c/urweb.c
diffstat 2 files changed, 12 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/lib/js/urweb.js	Sun Dec 04 15:18:41 2011 -0500
+++ b/lib/js/urweb.js	Sun Dec 04 16:32:06 2011 -0500
@@ -143,7 +143,7 @@
 function stringToTime_error(string) {
     var t = Date.parse(string);
     if (isNaN(t))
-        onFail("Invalid date string: " + string);
+        er("Invalid date string: " + string);
     else
         return t * 1000;
 }
@@ -962,7 +962,7 @@
 
 function trimZeroes(s) {
     for (var i = 0; i < s.length; ++i)
-        if (s[i] != '0') {
+        if (s.charAt(i) != '0') {
             if (i > 0)
                 return s.substring(i);
             else
--- a/src/c/urweb.c	Sun Dec 04 15:18:41 2011 -0500
+++ b/src/c/urweb.c	Sun Dec 04 16:32:06 2011 -0500
@@ -2150,6 +2150,7 @@
 
 #define TIME_FMT "%x %X"
 #define TIME_FMT_PG "%Y-%m-%d %T"
+#define TIME_FMT_JS "%Y/%m/%d %T"
 
 uw_Basis_string uw_Basis_timeToString(uw_context, uw_Basis_time);
 
@@ -2799,6 +2800,11 @@
       r->seconds = mktime(&stm);
       r->microseconds = 0;
       return r;
+    } else if (strptime(s, TIME_FMT_JS, &stm) == end) {
+      uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
+      r->seconds = mktime(&stm);
+      r->microseconds = 0;
+      return r;
     }
     else
       return NULL;
@@ -2939,6 +2945,9 @@
     } else if (strptime(s, TIME_FMT, &stm) == end) {
       uw_Basis_time r = { mktime(&stm) };
       return r;
+    } else if (strptime(s, TIME_FMT_JS, &stm) == end) {
+      uw_Basis_time r = { mktime(&stm) };
+      return r;
     } else
       uw_error(ctx, FATAL, "Can't parse time: %s", uw_Basis_htmlifyString(ctx, s));
   }
@@ -3883,7 +3892,7 @@
   char *end = strchr(s, 0);
   stm.tm_isdst = -1;
 
-  if (strptime(s, TIME_FMT_PG, &stm) == end || strptime(s, TIME_FMT, &stm) == end) {
+  if (strptime(s, TIME_FMT_PG, &stm) == end || strptime(s, TIME_FMT, &stm) == end || strptime(s, TIME_FMT_JS, &stm) == end) {
     uw_Basis_time *r = uw_malloc(ctx, sizeof(uw_Basis_time));
 
     r->seconds = timegm(&stm);