aboutsummaryrefslogtreecommitdiffstats
path: root/java_jni/org
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2022-07-08 03:21:29 +0200
committerSven Gothel <[email protected]>2022-07-08 03:21:29 +0200
commitd292e02a525077efd0b3262e214fb23c58552b72 (patch)
tree5c86c516c801b9c85a5d2b36849dff7fe342eda7 /java_jni/org
parentfe8daabb541e1307764df5acda403f490a9b14a1 (diff)
Clock.java: Add Instant get[Monotonic|Wallclock]Time() and wallClockSeconds() matching C++ lib, using Java Instant for C++ fraction_timespec
Diffstat (limited to 'java_jni/org')
-rw-r--r--java_jni/org/jau/sys/Clock.java42
1 files changed, 42 insertions, 0 deletions
diff --git a/java_jni/org/jau/sys/Clock.java b/java_jni/org/jau/sys/Clock.java
index 43beb63..ec51886 100644
--- a/java_jni/org/jau/sys/Clock.java
+++ b/java_jni/org/jau/sys/Clock.java
@@ -23,6 +23,8 @@
*/
package org.jau.sys;
+import java.time.Instant;
+
public class Clock {
private static long t0;
static {
@@ -31,11 +33,51 @@ public class Clock {
private static native long startupTimeMillisImpl();
/**
+ * Returns current monotonic time since Unix Epoch `00:00:00 UTC on 1970-01-01`.
+ *
+ * Returned fraction_timespec is passing machine precision and range of the underlying native API.
+ *
+ * Monotonic time shall be used for high-performance measurements of durations,
+ * since the underlying OS shall support fast calls.
+ *
+ * @see getWallClockTime()
+ */
+ public static Instant getMonotonicTime() {
+ final long[/*2*/] val = { 0, 0 };
+ getMonotonicTimeImpl(val);
+ return Instant.ofEpochSecond(val[0], val[1]);
+ }
+ private static native void getMonotonicTimeImpl(final long[/*2*/] val);
+
+ /**
+ * Returns current wall-clock real-time since Unix Epoch `00:00:00 UTC on 1970-01-01`.
+ *
+ * Returned Instant is passing machine precision and range of the underlying native API.
+ *
+ * Wall-Clock time shall be used for accurate measurements of the actual time only,
+ * since the underlying OS unlikely supports fast calls.
+ *
+ * @see getMonotonicTime()
+ */
+ public static Instant getWallClockTime() {
+ final long[/*2*/] val = { 0, 0 };
+ getWallClockTimeImpl(val);
+ return Instant.ofEpochSecond(val[0], val[1]);
+ }
+ private static native void getWallClockTimeImpl(final long[/*2*/] val);
+
+ /**
* Returns current monotonic time in milliseconds.
*/
public static native long currentTimeMillis();
/**
+ * Returns current wall-clock system `time of day` in seconds since Unix Epoch
+ * `00:00:00 UTC on 1 January 1970`.
+ */
+ public static native long wallClockSeconds();
+
+ /**
* Returns the startup time in monotonic time in milliseconds of the native module.
*/
public static long startupTimeMillis() { return t0; }