Skip to content

Commit

Permalink
Add delta to ClockUtil.
Browse files Browse the repository at this point in the history
Prevent delta calculation when start time isn't available.

Signed-off-by: Achim Kraus <achim.kraus@cloudcoap.net>
  • Loading branch information
boaks committed Feb 7, 2024
1 parent 958e83b commit 829bd28
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
******************************************************************************/
package org.eclipse.californium.elements.util;

import java.util.concurrent.TimeUnit;

/**
* Clock utility. Provides a {@linkplain ClockUtil#handler handler}.
*
Expand Down Expand Up @@ -79,4 +81,21 @@ public static void setRealtimeHandler(Realtime systemHandler) {
public static long nanoRealtime() {
return handler.nanoRealtime();
}

/**
* Calculate the delta from the provided past nano-realtime.
*
* @param pastNanoRealtime past value of {@link #nanoRealtime()}. Maybe
* {@code 0}, if the past nano-realtime isn't available.
* @param unit unit of result
* @return the difference of the current nano-realtime to the provided one,
* or {@code 0}, if {@code 0} is provided.
* @since 3.11
*/
public static long delta(long pastNanoRealtime, TimeUnit unit) {
if (pastNanoRealtime > 0) {
return unit.convert(handler.nanoRealtime() - pastNanoRealtime, TimeUnit.NANOSECONDS);
}
return 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/********************************************************************************
* Copyright (c) 2024 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* https://www.eclipse.org/legal/epl-2.0, or the Eclipse Distribution License
* v1.0 which is available at
* https://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
********************************************************************************/
package org.eclipse.californium.elements.util;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.is;

import java.util.concurrent.TimeUnit;

import org.eclipse.californium.elements.category.Small;
import org.junit.Test;
import org.junit.experimental.categories.Category;

@Category(Small.class)
public class ClockUtilTest {

@Test
public void testNanoRealtime() {
long now1 = ClockUtil.nanoRealtime();
try {
Thread.sleep(200);
long now2 = ClockUtil.nanoRealtime();
assertThat(now2, is(greaterThanOrEqualTo(now1)));
} catch (InterruptedException e) {
}
}

@Test
public void testDelta() {
long now = ClockUtil.nanoRealtime();
try {
Thread.sleep(200);
long delta = ClockUtil.delta(now, TimeUnit.MILLISECONDS);
assertThat(delta, is(greaterThan(100L)));
} catch (InterruptedException e) {
}
long delta2 = ClockUtil.delta(0, TimeUnit.MILLISECONDS);
assertThat(delta2, is(0L));
}

}

0 comments on commit 829bd28

Please sign in to comment.