changeset 1685:225b87d4a7df

Basis.toMilliseconds and diffInMilliseconds, based on a patch from Gergely Buday
author Adam Chlipala <adam@chlipala.net>
date Sat, 04 Feb 2012 11:01:06 -0500
parents 9dd8d47c3e58
children 0930c92a608e
files include/urweb.h lib/js/urweb.js lib/ur/basis.urs src/c/urweb.c src/settings.sml tests/millis.ur
diffstat 6 files changed, 39 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/include/urweb.h	Sat Feb 04 10:42:18 2012 -0500
+++ b/include/urweb.h	Sat Feb 04 11:01:06 2012 -0500
@@ -256,6 +256,8 @@
 uw_Basis_time uw_Basis_addSeconds(uw_context, uw_Basis_time, uw_Basis_int);
 uw_Basis_int uw_Basis_diffInSeconds(uw_context, uw_Basis_time, uw_Basis_time);
 uw_Basis_int uw_Basis_toSeconds(uw_context, uw_Basis_time);
+uw_Basis_int uw_Basis_diffInMilliseconds(uw_context, uw_Basis_time, uw_Basis_time);
+uw_Basis_int uw_Basis_toMilliseconds(uw_context, uw_Basis_time);
 extern const uw_Basis_time uw_Basis_minTime;
 
 void uw_register_transactional(uw_context, void *data, uw_callback commit, uw_callback rollback, uw_callback_with_retry free);
--- a/lib/js/urweb.js	Sat Feb 04 10:42:18 2012 -0500
+++ b/lib/js/urweb.js	Sat Feb 04 11:01:06 2012 -0500
@@ -132,10 +132,18 @@
     return Math.round((tm2 - tm1) / 1000000);
 }
 
+function diffInMilliseconds(tm1, tm2) {
+    return Math.round((tm2 - tm1) / 1000);
+}
+
 function toSeconds(tm) {
     return Math.round(tm / 1000000);
 }
 
+function toMilliseconds(tm) {
+    return Math.round(tm / 1000);
+}
+
 function addSeconds(tm, n) {
     return tm + n * 1000000;
 }
--- a/lib/ur/basis.urs	Sat Feb 04 10:42:18 2012 -0500
+++ b/lib/ur/basis.urs	Sat Feb 04 11:01:06 2012 -0500
@@ -161,6 +161,8 @@
 val toSeconds : time -> int
 val diffInSeconds : time -> time -> int
 (* Earlier time first *)
+val toMilliseconds : time -> int
+val diffInMilliseconds : time -> time -> int
 val timef : string -> time -> string (* Uses strftime() format string *)
 val readUtc : string -> option time
 
--- a/src/c/urweb.c	Sat Feb 04 10:42:18 2012 -0500
+++ b/src/c/urweb.c	Sat Feb 04 11:01:06 2012 -0500
@@ -3681,6 +3681,14 @@
   return difftime(tm2.seconds, tm1.seconds);
 }
 
+uw_Basis_int uw_Basis_toMilliseconds(uw_context ctx, uw_Basis_time tm) {
+  return tm.seconds * 1000 + tm.microseconds / 1000;
+}
+
+uw_Basis_int uw_Basis_diffInMilliseconds(uw_context ctx, uw_Basis_time tm1, uw_Basis_time tm2) {
+  return uw_Basis_toMilliseconds(ctx, tm2) - uw_Basis_toMilliseconds(ctx, tm1);
+}
+
 uw_Basis_int uw_Basis_toSeconds(uw_context ctx, uw_Basis_time tm) {
   return tm.seconds;
 }
--- a/src/settings.sml	Sat Feb 04 10:42:18 2012 -0500
+++ b/src/settings.sml	Sat Feb 04 11:01:06 2012 -0500
@@ -286,6 +286,8 @@
                           ("toSeconds", "toSeconds"),
                           ("addSeconds", "addSeconds"),
                           ("diffInSeconds", "diffInSeconds"),
+                          ("toMilliseconds", "toMilliseconds"),
+                          ("diffInMilliseconds", "diffInMilliseconds"),
 
                           ("onClick", "uw_onClick"),
                           ("onDblclick", "uw_onDblclick"),
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/millis.ur	Sat Feb 04 11:01:06 2012 -0500
@@ -0,0 +1,17 @@
+fun diffThem tm =
+    tm' <- now;
+    return <xml><body>
+      Diff: {[diffInMilliseconds tm tm']}
+    </body></xml>
+
+fun main () : transaction page =
+    tm <- now;
+
+    return <xml><body>
+      Now: {[toMilliseconds tm]}<br/>
+      <a link={diffThem tm}>Diff</a><br/>
+      
+      <button onclick={tm' <- now;
+                       alert ("Now: " ^ show (toMilliseconds tm'));
+                       alert ("Diff: " ^ show (diffInMilliseconds tm tm'))}/>
+    </body></xml>