jaulib v1.3.0
Jau Support Library (C++, Java, ..)
Clock.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2020 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24package org.jau.sys;
25
26import java.time.Instant;
27
28public class Clock {
29 private static long t0;
30 static {
31 t0 = startupTimeMillisImpl();
32 }
33 private static native long startupTimeMillisImpl();
34
35 /**
36 * Returns current monotonic time since Unix Epoch `00:00:00 UTC on 1970-01-01`.
37 *
38 * Returned fraction_timespec is passing machine precision and range of the underlying native API.
39 *
40 * Monotonic time shall be used for high-performance measurements of durations,
41 * since the underlying OS shall support fast calls.
42 *
43 * @see getWallClockTime()
44 */
45 public static Instant getMonotonicTime() {
46 final long[/*2*/] val = { 0, 0 };
47 getMonotonicTimeImpl(val);
48 return Instant.ofEpochSecond(val[0], val[1]);
49 }
50 private static native void getMonotonicTimeImpl(final long[/*2*/] val);
51
52 /**
53 * Returns current wall-clock real-time since Unix Epoch `00:00:00 UTC on 1970-01-01`.
54 *
55 * Returned Instant is passing machine precision and range of the underlying native API.
56 *
57 * Wall-Clock time shall be used for accurate measurements of the actual time only,
58 * since the underlying OS unlikely supports fast calls.
59 *
60 * @see getMonotonicTime()
61 */
62 public static Instant getWallClockTime() {
63 final long[/*2*/] val = { 0, 0 };
64 getWallClockTimeImpl(val);
65 return Instant.ofEpochSecond(val[0], val[1]);
66 }
67 private static native void getWallClockTimeImpl(final long[/*2*/] val);
68
69 /**
70 * Returns current monotonic time in milliseconds.
71 */
72 public static native long currentTimeMillis();
73
74 /**
75 * Returns current wall-clock system `time of day` in seconds since Unix Epoch
76 * `00:00:00 UTC on 1 January 1970`.
77 */
78 public static native long wallClockSeconds();
79
80 /**
81 * Returns the startup time in monotonic time in milliseconds of the native module.
82 */
83 public static long startupTimeMillis() { return t0; }
84
85 /**
86 * Returns current elapsed monotonic time in milliseconds since module startup, see {@link #startupTimeMillis()}.
87 */
88 public static long elapsedTimeMillis() { return currentTimeMillis() - t0; }
89
90 /**
91 * Returns elapsed monotonic time in milliseconds since module startup comparing against the given timestamp, see {@link #startupTimeMillis()}.
92 */
93 public static long elapsedTimeMillis(final long current_ts) { return current_ts - t0; }
94
95}
static Instant getWallClockTime()
Returns current wall-clock real-time since Unix Epoch 00:00:00 UTC on 1970-01-01.
Definition: Clock.java:62
static long elapsedTimeMillis()
Returns current elapsed monotonic time in milliseconds since module startup, see startupTimeMillis().
Definition: Clock.java:88
static native long currentTimeMillis()
Returns current monotonic time in milliseconds.
static long startupTimeMillis()
Returns the startup time in monotonic time in milliseconds of the native module.
Definition: Clock.java:83
static Instant getMonotonicTime()
Returns current monotonic time since Unix Epoch 00:00:00 UTC on 1970-01-01.
Definition: Clock.java:45
static native long wallClockSeconds()
Returns current wall-clock system time of day in seconds since Unix Epoch 00:00:00 UTC on 1 January 1...
static long elapsedTimeMillis(final long current_ts)
Returns elapsed monotonic time in milliseconds since module startup comparing against the given times...
Definition: Clock.java:93