Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Implemented os:timestamp/0.

  • Loading branch information...
commit 6e6c2093ecdfdb036281114da282a806de652272 1 parent 1e4ca01
Erik Søe Sørensen authored
11  src/main/java/erjang/m/erlang/ErlBif.java
@@ -1050,7 +1050,7 @@ static public ENumber abs(ENumber v1) {
1050 1050
 
1051 1051
 	@BIF(name = "now")
1052 1052
 	static public ETuple3 now() {
1053  
-		long now = now_micros();
  1053
+		long now = now_unique_micros();
1054 1054
 		int micros = (int)(now % 1000000); now /= 1000000;
1055 1055
 		int secs   = (int)(now % 1000000); now /= 1000000;
1056 1056
 		int megas  = (int)now;
@@ -1068,14 +1068,18 @@ static public ETuple3 now() {
1068 1068
 	final static long micros_from_epoch_to_nanotime =
1069 1069
 		System.currentTimeMillis() * 1000 - System.nanoTime() / 1000;
1070 1070
 
1071  
-	static long now_micros() {
  1071
+	public static long now_raw_micros() {
  1072
+		return System.nanoTime() / 1000 + micros_from_epoch_to_nanotime;
  1073
+	}
  1074
+
  1075
+	static long now_unique_micros() {
1072 1076
 		/* now() must fulfill:
1073 1077
 		 * - Any return value approximates the current time.
1074 1078
 		 * - The return values are strictly increasing (and thus unique).
1075 1079
 		 * We ensure the latter by (a) always increasing latest_now,
1076 1080
 		 * (b) always returning what we set it to.
1077 1081
 		 */
1078  
-		long micros = System.nanoTime() / 1000 + micros_from_epoch_to_nanotime;
  1082
+		long micros = now_raw_micros();
1079 1083
 		long prev;
1080 1084
 		while ((prev = latest_now.get()) < micros) {
1081 1085
 			if (latest_now.compareAndSet(prev,micros)) {
@@ -1085,6 +1089,7 @@ static long now_micros() {
1085 1089
 		return latest_now.incrementAndGet();
1086 1090
 	}
1087 1091
 
  1092
+
1088 1093
 	// tests
1089 1094
 
1090 1095
 	@BIF(name = "==", type = Type.GUARD)
17  src/main/java/erjang/m/os/Native.java
@@ -31,6 +31,7 @@
31 31
 import erjang.ERT;
32 32
 import erjang.EString;
33 33
 import erjang.ETuple2;
  34
+import erjang.ETuple3;
34 35
 import erjang.ECons;
35 36
 import erjang.NotImplemented;
36 37
 
@@ -148,4 +149,20 @@ private static Integer tryPattern1(String processName) {
148 149
 		
149 150
 	}
150 151
 
  152
+	@BIF
  153
+	static public ETuple3 timestamp() {
  154
+		long now = erjang.m.erlang.ErlBif.now_raw_micros();
  155
+		int micros = (int)(now % 1000000); now /= 1000000;
  156
+		int secs   = (int)(now % 1000000); now /= 1000000;
  157
+		int megas  = (int)now;
  158
+
  159
+		ETuple3 res = new ETuple3();
  160
+
  161
+		res.elem1 = ERT.box(megas);
  162
+		res.elem2 = ERT.box(secs);
  163
+		res.elem3 = ERT.box(micros);
  164
+
  165
+		return res;
  166
+	}
  167
+
151 168
 }

0 notes on commit 6e6c209

Please sign in to comment.
Something went wrong with that request. Please try again.