Skip to content

Commit

Permalink
Added functions to multiply durations and periods.
Browse files Browse the repository at this point in the history
  • Loading branch information
chipkent committed May 20, 2024
1 parent b180263 commit 98bb980
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
61 changes: 61 additions & 0 deletions engine/time/src/main/java/io/deephaven/time/DateTimeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -1871,6 +1871,67 @@ public static long minus(@Nullable final ZonedDateTime dateTime1, @Nullable fina
return checkUnderflowMinus(epochNanos(dateTime1), epochNanos(dateTime2), true);
}

/**
* Multiply a duration by a scalar.
*
* @param duration the duration to multiply
* @param scalar the scalar to multiply by
* @return {@code null} if either input is {@code null}; otherwise the duration multiplied by the scalar
*/
public static Duration multiply(final Duration duration, final long scalar) {
if (duration == null || scalar == NULL_LONG) {
return null;
}

return duration.multipliedBy(scalar);
}

/**
* Multiply a duration by a scalar.
*
* @param duration the duration to multiply
* @param scalar the scalar to multiply by
* @return {@code null} if either input is {@code null}; otherwise the duration multiplied by the scalar
*/
public static Duration multiply(final long scalar, final Duration duration) {
if (duration == null || scalar == NULL_LONG) {
return null;
}

return duration.multipliedBy(scalar);
}

/**
* Multiply a period by a scalar.
*
* @param period the period to multiply
* @param scalar the scalar to multiply by
* @return {@code null} if either input is {@code null}; otherwise the period multiplied by the scalar
*/
public static Period multiply(final Period period, final int scalar) {
if (period == null || scalar == NULL_INT) {
return null;
}

return period.multipliedBy(scalar);
}

/**
* Multiply a period by a scalar.
*
* @param period the period to multiply
* @param scalar the scalar to multiply by
* @return {@code null} if either input is {@code null}; otherwise the period multiplied by the scalar
*/
public static Period multiply(final int scalar, final Period period) {
if (period == null || scalar == NULL_INT) {
return null;
}

return period.multipliedBy(scalar);
}


/**
* Returns the difference in nanoseconds between two instant values.
*
Expand Down
25 changes: 25 additions & 0 deletions engine/time/src/test/java/io/deephaven/time/TestDateTimeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2347,6 +2347,31 @@ public void testMinus() {

}

public void testMultiply() {
final Period p = Period.ofDays(3);
final Duration d = Duration.ofNanos(123456789L);

TestCase.assertEquals(Period.ofDays(6), DateTimeUtils.multiply(p, 2));
TestCase.assertEquals(Period.ofDays(9), DateTimeUtils.multiply(p, 3));
TestCase.assertEquals(Period.ofDays(3), DateTimeUtils.multiply(p, 1));
TestCase.assertEquals(Period.ofDays(0), DateTimeUtils.multiply(p, 0));
TestCase.assertEquals(Period.ofDays(-3), DateTimeUtils.multiply(p, -1));
TestCase.assertEquals(Period.ofDays(-6), DateTimeUtils.multiply(p, -2));
TestCase.assertEquals(Period.ofDays(-9), DateTimeUtils.multiply(p, -3));
TestCase.assertNull(DateTimeUtils.multiply((Period) null, 3));
TestCase.assertNull(DateTimeUtils.multiply(p, NULL_INT));

TestCase.assertEquals(Duration.ofNanos(246913578L), DateTimeUtils.multiply(d, 2));
TestCase.assertEquals(Duration.ofNanos(370370367L), DateTimeUtils.multiply(d, 3));
TestCase.assertEquals(Duration.ofNanos(123456789L), DateTimeUtils.multiply(d, 1));
TestCase.assertEquals(Duration.ofNanos(0), DateTimeUtils.multiply(d, 0));
TestCase.assertEquals(Duration.ofNanos(-123456789L), DateTimeUtils.multiply(d, -1));
TestCase.assertEquals(Duration.ofNanos(-246913578L), DateTimeUtils.multiply(d, -2));
TestCase.assertEquals(Duration.ofNanos(-370370367L), DateTimeUtils.multiply(d, -3));
TestCase.assertNull(DateTimeUtils.multiply((Duration) null, 3));
TestCase.assertNull(DateTimeUtils.multiply(d, NULL_LONG));
}

public void testDiffNanos() {
final Instant i1 = DateTimeUtils.epochNanosToInstant(12345678987654321L);
final Instant i2 = DateTimeUtils.epochNanosToInstant(98765432123456789L);
Expand Down

0 comments on commit 98bb980

Please sign in to comment.