From 98bb980c5354021376ac0223a8946fb3a99c94cc Mon Sep 17 00:00:00 2001 From: Chip Kent Date: Mon, 20 May 2024 13:18:26 -0600 Subject: [PATCH] Added functions to multiply durations and periods. --- .../java/io/deephaven/time/DateTimeUtils.java | 61 +++++++++++++++++++ .../io/deephaven/time/TestDateTimeUtils.java | 25 ++++++++ 2 files changed, 86 insertions(+) diff --git a/engine/time/src/main/java/io/deephaven/time/DateTimeUtils.java b/engine/time/src/main/java/io/deephaven/time/DateTimeUtils.java index 934ebcc6c0a..ea06842f56e 100644 --- a/engine/time/src/main/java/io/deephaven/time/DateTimeUtils.java +++ b/engine/time/src/main/java/io/deephaven/time/DateTimeUtils.java @@ -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. * diff --git a/engine/time/src/test/java/io/deephaven/time/TestDateTimeUtils.java b/engine/time/src/test/java/io/deephaven/time/TestDateTimeUtils.java index 68bf3c51a26..b3d583b3e98 100644 --- a/engine/time/src/test/java/io/deephaven/time/TestDateTimeUtils.java +++ b/engine/time/src/test/java/io/deephaven/time/TestDateTimeUtils.java @@ -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);