Skip to content

Commit

Permalink
Implemented os:timestamp/0.
Browse files Browse the repository at this point in the history
  • Loading branch information
esstrifork committed Aug 19, 2011
1 parent 1e4ca01 commit 6e6c209
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/main/java/erjang/m/erlang/ErlBif.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1050,7 +1050,7 @@ static public ENumber abs(ENumber v1) {


@BIF(name = "now") @BIF(name = "now")
static public ETuple3 now() { static public ETuple3 now() {
long now = now_micros(); long now = now_unique_micros();
int micros = (int)(now % 1000000); now /= 1000000; int micros = (int)(now % 1000000); now /= 1000000;
int secs = (int)(now % 1000000); now /= 1000000; int secs = (int)(now % 1000000); now /= 1000000;
int megas = (int)now; int megas = (int)now;
Expand All @@ -1068,14 +1068,18 @@ static public ETuple3 now() {
final static long micros_from_epoch_to_nanotime = final static long micros_from_epoch_to_nanotime =
System.currentTimeMillis() * 1000 - System.nanoTime() / 1000; 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: /* now() must fulfill:
* - Any return value approximates the current time. * - Any return value approximates the current time.
* - The return values are strictly increasing (and thus unique). * - The return values are strictly increasing (and thus unique).
* We ensure the latter by (a) always increasing latest_now, * We ensure the latter by (a) always increasing latest_now,
* (b) always returning what we set it to. * (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; long prev;
while ((prev = latest_now.get()) < micros) { while ((prev = latest_now.get()) < micros) {
if (latest_now.compareAndSet(prev,micros)) { if (latest_now.compareAndSet(prev,micros)) {
Expand All @@ -1085,6 +1089,7 @@ static long now_micros() {
return latest_now.incrementAndGet(); return latest_now.incrementAndGet();
} }



// tests // tests


@BIF(name = "==", type = Type.GUARD) @BIF(name = "==", type = Type.GUARD)
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/erjang/m/os/Native.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import erjang.ERT; import erjang.ERT;
import erjang.EString; import erjang.EString;
import erjang.ETuple2; import erjang.ETuple2;
import erjang.ETuple3;
import erjang.ECons; import erjang.ECons;
import erjang.NotImplemented; import erjang.NotImplemented;


Expand Down Expand Up @@ -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;
}

} }

0 comments on commit 6e6c209

Please sign in to comment.