Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implemented os:timestamp/0.

  • Loading branch information...
commit 6e6c2093ecdfdb036281114da282a806de652272 1 parent 1e4ca01
@eriksoe eriksoe authored
View
11 src/main/java/erjang/m/erlang/ErlBif.java
@@ -1050,7 +1050,7 @@ static public ENumber abs(ENumber v1) {
@BIF(name = "now")
static public ETuple3 now() {
- long now = now_micros();
+ long now = now_unique_micros();
int micros = (int)(now % 1000000); now /= 1000000;
int secs = (int)(now % 1000000); now /= 1000000;
int megas = (int)now;
@@ -1068,14 +1068,18 @@ static public ETuple3 now() {
final static long micros_from_epoch_to_nanotime =
System.currentTimeMillis() * 1000 - System.nanoTime() / 1000;
- static long now_micros() {
+ public static long now_raw_micros() {
+ return System.nanoTime() / 1000 + micros_from_epoch_to_nanotime;
+ }
+
+ static long now_unique_micros() {
/* now() must fulfill:
* - Any return value approximates the current time.
* - The return values are strictly increasing (and thus unique).
* We ensure the latter by (a) always increasing latest_now,
* (b) always returning what we set it to.
*/
- long micros = System.nanoTime() / 1000 + micros_from_epoch_to_nanotime;
+ long micros = now_raw_micros();
long prev;
while ((prev = latest_now.get()) < micros) {
if (latest_now.compareAndSet(prev,micros)) {
@@ -1085,6 +1089,7 @@ static long now_micros() {
return latest_now.incrementAndGet();
}
+
// tests
@BIF(name = "==", type = Type.GUARD)
View
17 src/main/java/erjang/m/os/Native.java
@@ -31,6 +31,7 @@
import erjang.ERT;
import erjang.EString;
import erjang.ETuple2;
+import erjang.ETuple3;
import erjang.ECons;
import erjang.NotImplemented;
@@ -148,4 +149,20 @@ private static Integer tryPattern1(String processName) {
}
+ @BIF
+ static public ETuple3 timestamp() {
+ long now = erjang.m.erlang.ErlBif.now_raw_micros();
+ int micros = (int)(now % 1000000); now /= 1000000;
+ int secs = (int)(now % 1000000); now /= 1000000;
+ int megas = (int)now;
+
+ ETuple3 res = new ETuple3();
+
+ res.elem1 = ERT.box(megas);
+ res.elem2 = ERT.box(secs);
+ res.elem3 = ERT.box(micros);
+
+ return res;
+ }
+
}
Please sign in to comment.
Something went wrong with that request. Please try again.