summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Adam Chlipala <adam@chlipala.net>2012-02-04 11:01:06 -0500
committerGravatar Adam Chlipala <adam@chlipala.net>2012-02-04 11:01:06 -0500
commite0e4f3c3978c8cf18f6e2c11203051c2ef4d4a68 (patch)
treee85b1d872c1f40a0b7ba471cf80d0787e65c7eb7
parent08e55a0472abe5a5464996402c398517422514fd (diff)
Basis.toMilliseconds and diffInMilliseconds, based on a patch from Gergely Buday
-rw-r--r--include/urweb.h2
-rw-r--r--lib/js/urweb.js8
-rw-r--r--lib/ur/basis.urs2
-rw-r--r--src/c/urweb.c8
-rw-r--r--src/settings.sml2
-rw-r--r--tests/millis.ur17
6 files changed, 39 insertions, 0 deletions
diff --git a/include/urweb.h b/include/urweb.h
index 4230da1a..9cdc7e8a 100644
--- a/include/urweb.h
+++ b/include/urweb.h
@@ -256,6 +256,8 @@ uw_Basis_time uw_Basis_now(uw_context);
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);
diff --git a/lib/js/urweb.js b/lib/js/urweb.js
index b557147b..a5ab90c5 100644
--- a/lib/js/urweb.js
+++ b/lib/js/urweb.js
@@ -132,10 +132,18 @@ function diffInSeconds(tm1, tm2) {
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;
}
diff --git a/lib/ur/basis.urs b/lib/ur/basis.urs
index 3afb4985..67e6abc6 100644
--- a/lib/ur/basis.urs
+++ b/lib/ur/basis.urs
@@ -161,6 +161,8 @@ val addSeconds : time -> int -> time
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
diff --git a/src/c/urweb.c b/src/c/urweb.c
index 738c765c..0b04f665 100644
--- a/src/c/urweb.c
+++ b/src/c/urweb.c
@@ -3681,6 +3681,14 @@ uw_Basis_int uw_Basis_diffInSeconds(uw_context ctx, uw_Basis_time tm1, uw_Basis_
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;
}
diff --git a/src/settings.sml b/src/settings.sml
index 017c5095..3adf81c8 100644
--- a/src/settings.sml
+++ b/src/settings.sml
@@ -286,6 +286,8 @@ val jsFuncsBase = basisM [("alert", "alert"),
("toSeconds", "toSeconds"),
("addSeconds", "addSeconds"),
("diffInSeconds", "diffInSeconds"),
+ ("toMilliseconds", "toMilliseconds"),
+ ("diffInMilliseconds", "diffInMilliseconds"),
("onClick", "uw_onClick"),
("onDblclick", "uw_onDblclick"),
diff --git a/tests/millis.ur b/tests/millis.ur
new file mode 100644
index 00000000..0ba22b9f
--- /dev/null
+++ b/tests/millis.ur
@@ -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>