Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 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 @@ -2246,6 +2246,42 @@ public static Period multiply(final int scalar, final Period period) {
return multiply(period, 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
* @throws DateTimeOverflowException if the datetime arithmetic overflows or underflows
*/
public static Period multiply(final Period period, final long scalar) {
if (period == null || scalar == NULL_LONG) {
return null;
}

if (scalar > Integer.MAX_VALUE || scalar < Integer.MIN_VALUE) {
throw new DateTimeOverflowException("Scalar value is too large to be cast to an int");
}

try {
return period.multipliedBy((int) scalar);
} catch (DateTimeException | ArithmeticException ex) {
throw new DateTimeOverflowException(ex);
}
}

/**
* 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
* @throws DateTimeOverflowException if the datetime arithmetic overflows or underflows
*/
public static Period multiply(final long scalar, final Period period) {
return multiply(period, scalar);
}

/**
* Divide a duration by a scalar.
*
Expand Down
17 changes: 17 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 @@ -2524,6 +2524,23 @@ public void testMultiply() {
TestCase.assertNull(DateTimeUtils.multiply((Period) null, 3));
TestCase.assertNull(DateTimeUtils.multiply(p, NULL_INT));

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

try {
DateTimeUtils.multiply(p, Long.MAX_VALUE);
TestCase.fail("This should have overflowed");
} catch (DateTimeUtils.DateTimeOverflowException e) {
// ok
}

TestCase.assertEquals(Duration.ofNanos(246913578L), DateTimeUtils.multiply(d, 2));
TestCase.assertEquals(Duration.ofNanos(246913578L), DateTimeUtils.multiply(2, d));
Expand Down