Skip to content

Commit

Permalink
Convert Duration to java.time.Duration
Browse files Browse the repository at this point in the history
  • Loading branch information
findepi committed Jul 3, 2023
1 parent ee4ca0c commit 5bd34b8
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/main/java/io/airlift/units/Duration.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;

import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit;
import java.util.Locale;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static io.airlift.units.Preconditions.checkArgument;
import static java.lang.Math.floor;
import static java.lang.Math.log;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;
import static java.util.concurrent.TimeUnit.DAYS;
Expand Down Expand Up @@ -130,6 +135,28 @@ public Duration convertToMostSuccinctTimeUnit()
return convertTo(unitToUse);
}

public java.time.Duration toJavaTime()
{
long seconds;
long nanoAdjustment;
long secondsPerUnit = SECONDS.convert(1, unit);
long nanosPerUnit = NANOSECONDS.convert(1, unit);
if (secondsPerUnit > 1) {
seconds = (long) floor(value * secondsPerUnit);
nanoAdjustment = (long) floor((value - (double) seconds / secondsPerUnit) * nanosPerUnit);
}
else {
long unitsPerSecond = unit.convert(1, SECONDS);
seconds = (long) floor(value / unitsPerSecond);
nanoAdjustment = (long) floor((value - (double) seconds * unitsPerSecond) * nanosPerUnit);
}

if (seconds == Long.MAX_VALUE) {
nanoAdjustment = 0;
}
return java.time.Duration.ofSeconds(seconds, nanoAdjustment);
}

@JsonValue
@Override
public String toString()
Expand Down
76 changes: 76 additions & 0 deletions src/test/java/io/airlift/units/TestDuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,82 @@ public void testEquivalence()
.check();
}

@Test
public void testToJavaTime()
{
assertThat(new Duration(0, DAYS).toJavaTime())
.isEqualTo(java.time.Duration.ZERO);

assertThat(new Duration(1, NANOSECONDS).toJavaTime())
.isEqualTo(java.time.Duration.ofNanos(1));
assertThat(new Duration(1, MICROSECONDS).toJavaTime())
.isEqualTo(java.time.Duration.ofNanos(1000));
assertThat(new Duration(1, MILLISECONDS).toJavaTime())
.isEqualTo(java.time.Duration.ofMillis(1));
assertThat(new Duration(1, SECONDS).toJavaTime())
.isEqualTo(java.time.Duration.ofSeconds(1));
assertThat(new Duration(1, MINUTES).toJavaTime())
.isEqualTo(java.time.Duration.ofMinutes(1));
assertThat(new Duration(1, HOURS).toJavaTime())
.isEqualTo(java.time.Duration.ofHours(1));
assertThat(new Duration(1, DAYS).toJavaTime())
.isEqualTo(java.time.Duration.ofDays(1));

assertThat(new Duration(42, NANOSECONDS).toJavaTime())
.isEqualTo(java.time.Duration.ofNanos(42));
assertThat(new Duration(42, MICROSECONDS).toJavaTime())
.isEqualTo(java.time.Duration.ofNanos(42000));
assertThat(new Duration(42, MILLISECONDS).toJavaTime())
.isEqualTo(java.time.Duration.ofMillis(42));
assertThat(new Duration(42, SECONDS).toJavaTime())
.isEqualTo(java.time.Duration.ofSeconds(42));
assertThat(new Duration(42, MINUTES).toJavaTime())
.isEqualTo(java.time.Duration.ofMinutes(42));
assertThat(new Duration(42, HOURS).toJavaTime())
.isEqualTo(java.time.Duration.ofHours(42));
assertThat(new Duration(42, DAYS).toJavaTime())
.isEqualTo(java.time.Duration.ofDays(42));

assertThat(new Duration(123456789, MILLISECONDS).toJavaTime())
.isEqualTo(java.time.Duration.ofMillis(123456789));
assertThat(new Duration(123456789, SECONDS).toJavaTime())
.isEqualTo(java.time.Duration.ofSeconds(123456789));
assertThat(new Duration(123456789, DAYS).toJavaTime())
.isEqualTo(java.time.Duration.ofDays(123456789));

assertThat(new Duration(1234567890123456789d, MILLISECONDS).toJavaTime())
.isBetween(
java.time.Duration.ofMillis(1234567890123456700L),
java.time.Duration.ofMillis(1234567890123456800L));
assertThat(new Duration(1234567890123456789d, SECONDS).toJavaTime())
.isBetween(
java.time.Duration.ofSeconds(1234567890123456700L),
java.time.Duration.ofSeconds(1234567890123456800L));
assertThat(new Duration(1234567890123456789d, DAYS).toJavaTime())
.isEqualTo(java.time.Duration.ofSeconds(Long.MAX_VALUE));

assertThat(new Duration(0.01, MILLISECONDS).toJavaTime())
.isEqualTo(java.time.Duration.ofNanos(10_000));
assertThat(new Duration(0.01, SECONDS).toJavaTime())
.isEqualTo(java.time.Duration.ofMillis(10));
assertThat(new Duration(0.01, DAYS).toJavaTime())
.isEqualTo(java.time.Duration.ofSeconds(86400 / 100));

assertThat(new Duration(0.25, MILLISECONDS).toJavaTime())
.isEqualTo(java.time.Duration.ofNanos(250_000));
assertThat(new Duration(0.25, SECONDS).toJavaTime())
.isEqualTo(java.time.Duration.ofNanos(250_000_000));
assertThat(new Duration(0.25, DAYS).toJavaTime())
.isEqualTo(java.time.Duration.ofSeconds(86400 / 4));

assertThat(new Duration(0.5, MILLISECONDS).toJavaTime())
.isEqualTo(java.time.Duration.ofNanos(500_000));
assertThat(new Duration(0.5, SECONDS).toJavaTime())
.isEqualTo(java.time.Duration.ofNanos(500_000_000));
assertThat(new Duration(0.5, DAYS).toJavaTime())
.isEqualTo(java.time.Duration.ofSeconds(86400 / 2));
}

private static List<Duration> generateTimeBucket(double seconds)
{
List<Duration> bucket = new ArrayList<>();
Expand Down

0 comments on commit 5bd34b8

Please sign in to comment.