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 991373da2aa..8162b5aa591 100644 --- a/engine/time/src/main/java/io/deephaven/time/DateTimeUtils.java +++ b/engine/time/src/main/java/io/deephaven/time/DateTimeUtils.java @@ -2536,6 +2536,10 @@ public static int minuteOfHour(@Nullable final ZonedDateTime dateTime) { return dateTime.getMinute(); } + /////////////////////////////////////////////////////////////////////////////////////////////////// + // ↓↓↓↓↓↓↓ THE METHODS BELOW ARE DEPRECATED AND WILL BE REMOVED SOON ↓↓↓↓↓↓↓ + /////////////////////////////////////////////////////////////////////////////////////////////////// + /** * Returns the number of nanoseconds that have elapsed since the top of the day. *

@@ -2547,14 +2551,12 @@ public static int minuteOfHour(@Nullable final ZonedDateTime dateTime) { * @param timeZone time zone * @return {@link QueryConstants#NULL_LONG} if either input is {@code null}; otherwise, number of nanoseconds that * have elapsed since the top of the day + * @deprecated Use {@link #nanosOfDay(Instant, ZoneId, boolean)} instead. To be removed soon. */ @ScriptApi + @Deprecated public static long nanosOfDay(@Nullable final Instant instant, @Nullable final ZoneId timeZone) { - if (instant == null || timeZone == null) { - return NULL_LONG; - } - - return nanosOfDay(toZonedDateTime(instant, timeZone)); + return nanosOfDay(instant, timeZone, false); } /** @@ -2567,29 +2569,12 @@ public static long nanosOfDay(@Nullable final Instant instant, @Nullable final Z * @param dateTime time * @return {@link QueryConstants#NULL_LONG} if either input is {@code null}; otherwise, number of nanoseconds that * have elapsed since the top of the day + * @deprecated Use {@link #nanosOfDay(ZonedDateTime, boolean)} instead. To be removed soon. */ @ScriptApi + @Deprecated public static long nanosOfDay(@Nullable final ZonedDateTime dateTime) { - if (dateTime == null) { - return NULL_LONG; - } - - return epochNanos(dateTime) - epochNanos(atMidnight(dateTime)); - } - - /** - * Returns the number of nanoseconds that have elapsed since the top of the day. - * - * @param localTime time - * @return {@link QueryConstants#NULL_LONG} if input is {@code null}; otherwise, number of nanoseconds that have - * elapsed since the top of the day - */ - public static long nanosOfDay(@Nullable final LocalTime localTime) { - if (localTime == null) { - return NULL_LONG; - } - - return localTime.toNanoOfDay(); + return nanosOfDay(dateTime, false); } /** @@ -2603,14 +2588,12 @@ public static long nanosOfDay(@Nullable final LocalTime localTime) { * @param timeZone time zone * @return {@link QueryConstants#NULL_INT} if either input is {@code null}; otherwise, number of milliseconds that * have elapsed since the top of the day + * @deprecated Use {@link #millisOfDay(Instant, ZoneId, boolean)} instead. To be removed soon. */ @ScriptApi + @Deprecated public static int millisOfDay(@Nullable final Instant instant, @Nullable final ZoneId timeZone) { - if (instant == null || timeZone == null) { - return NULL_INT; - } - - return (int) nanosToMillis(nanosOfDay(instant, timeZone)); + return millisOfDay(instant, timeZone, false); } /** @@ -2623,14 +2606,12 @@ public static int millisOfDay(@Nullable final Instant instant, @Nullable final Z * @param dateTime time * @return {@link QueryConstants#NULL_INT} if either input is {@code null}; otherwise, number of milliseconds that * have elapsed since the top of the day + * @deprecated Use {@link #millisOfDay(ZonedDateTime, boolean)} instead. To be removed soon. */ @ScriptApi + @Deprecated public static int millisOfDay(@Nullable final ZonedDateTime dateTime) { - if (dateTime == null) { - return NULL_INT; - } - - return (int) nanosToMillis(nanosOfDay(dateTime)); + return millisOfDay(dateTime, false); } /** @@ -2644,14 +2625,12 @@ public static int millisOfDay(@Nullable final ZonedDateTime dateTime) { * @param timeZone time zone * @return {@link QueryConstants#NULL_INT} if either input is {@code null}; otherwise, number of seconds that have * elapsed since the top of the day + * @deprecated Use {@link #secondOfDay(Instant, ZoneId, boolean)} instead. To be removed soon. */ @ScriptApi + @Deprecated public static int secondOfDay(@Nullable final Instant instant, @Nullable final ZoneId timeZone) { - if (instant == null || timeZone == null) { - return NULL_INT; - } - - return (int) nanosToSeconds(nanosOfDay(instant, timeZone)); + return secondOfDay(instant, timeZone, false); } /** @@ -2664,14 +2643,12 @@ public static int secondOfDay(@Nullable final Instant instant, @Nullable final Z * @param dateTime time * @return {@link QueryConstants#NULL_INT} if either input is {@code null}; otherwise, number of seconds that have * elapsed since the top of the day + * @deprecated Use {@link #secondOfDay(ZonedDateTime, boolean)} instead. To be removed soon. */ @ScriptApi + @Deprecated public static int secondOfDay(@Nullable final ZonedDateTime dateTime) { - if (dateTime == null) { - return NULL_INT; - } - - return (int) nanosToSeconds(nanosOfDay(dateTime)); + return secondOfDay(dateTime, false); } /** @@ -2685,14 +2662,12 @@ public static int secondOfDay(@Nullable final ZonedDateTime dateTime) { * @param timeZone time zone * @return {@link QueryConstants#NULL_INT} if either input is {@code null}; otherwise, number of minutes that have * elapsed since the top of the day + * @deprecated Use {@link #minuteOfDay(Instant, ZoneId, boolean)} instead. To be removed soon. */ @ScriptApi + @Deprecated public static int minuteOfDay(@Nullable final Instant instant, @Nullable final ZoneId timeZone) { - if (instant == null || timeZone == null) { - return NULL_INT; - } - - return secondOfDay(instant, timeZone) / 60; + return minuteOfDay(instant, timeZone, false); } /** @@ -2705,14 +2680,12 @@ public static int minuteOfDay(@Nullable final Instant instant, @Nullable final Z * @param dateTime time * @return {@link QueryConstants#NULL_INT} if either input is {@code null}; otherwise, number of minutes that have * elapsed since the top of the day + * @deprecated Use {@link #minuteOfDay(ZonedDateTime, boolean)} instead. To be removed soon. */ @ScriptApi + @Deprecated public static int minuteOfDay(@Nullable final ZonedDateTime dateTime) { - if (dateTime == null) { - return NULL_INT; - } - - return secondOfDay(dateTime) / 60; + return minuteOfDay(dateTime, false); } /** @@ -2726,14 +2699,12 @@ public static int minuteOfDay(@Nullable final ZonedDateTime dateTime) { * @param timeZone time zone * @return {@link QueryConstants#NULL_INT} if either input is {@code null}; otherwise, number of hours that have * elapsed since the top of the day + * @deprecated Use {@link #hourOfDay(Instant, ZoneId, boolean)} instead. To be removed soon. */ @ScriptApi + @Deprecated public static int hourOfDay(@Nullable final Instant instant, @Nullable final ZoneId timeZone) { - if (instant == null || timeZone == null) { - return NULL_INT; - } - - return hourOfDay(toZonedDateTime(instant, timeZone)); + return hourOfDay(instant, timeZone, false); } /** @@ -2746,14 +2717,325 @@ public static int hourOfDay(@Nullable final Instant instant, @Nullable final Zon * @param dateTime time * @return {@link QueryConstants#NULL_INT} if either input is {@code null}; otherwise, number of hours that have * elapsed since the top of the day + * @deprecated Use {@link #hourOfDay(ZonedDateTime, boolean)} instead. To be removed soon. */ @ScriptApi + @Deprecated public static int hourOfDay(@Nullable final ZonedDateTime dateTime) { + return hourOfDay(dateTime, false); + } + + /////////////////////////////////////////////////////////////////////////////////////////////////// + // ↑↑↑↑↑↑↑ THE METHODS ABOVE ARE DEPRECATED AND WILL BE REMOVED SOON ↑↑↑↑↑↑↑ + /////////////////////////////////////////////////////////////////////////////////////////////////// + + /** + * Returns the number of nanoseconds that have elapsed since the start of the day. + * + * @param instant time + * @param timeZone time zone + * @param localTime if {@code true}, returns the number of nanos from the start of the day according to the local + * time. In this case, 9:30AM always returns the same value. If {@code false}, returns the number of nanos + * from the start of the day. On days when daylight savings time events occur, results may be different from + * what is expected based upon the local time. For example, on daylight savings time change days, 9:30AM may + * be earlier or later in the day based upon if the daylight savings time adjustment is forwards or + * backwards. On non DST days, the result is the same as if localTime is false. + * @return {@link QueryConstants#NULL_LONG} if either input is {@code null}; otherwise, number of nanoseconds that + * have elapsed since the start of the day + */ + @ScriptApi + public static long nanosOfDay(@Nullable final Instant instant, @Nullable final ZoneId timeZone, + final boolean localTime) { + if (instant == null || timeZone == null) { + return NULL_LONG; + } + + return nanosOfDay(toZonedDateTime(instant, timeZone), localTime); + } + + /** + * Returns the number of nanoseconds that have elapsed since the start of the day. + * + * @param dateTime time + * @param localTime if {@code true}, returns the number of nanos from the start of the day according to the local + * time. In this case, 9:30AM always returns the same value. If {@code false}, returns the number of nanos + * from the start of the day. On days when daylight savings time events occur, results may be different from + * what is expected based upon the local time. For example, on daylight savings time change days, 9:30AM may + * be earlier or later in the day based upon if the daylight savings time adjustment is forwards or + * backwards. On non DST days, the result is the same as if localTime is false. + * @return {@link QueryConstants#NULL_LONG} if either input is {@code null}; otherwise, number of nanoseconds that + * have elapsed since the start of the day + */ + @ScriptApi + public static long nanosOfDay(@Nullable final ZonedDateTime dateTime, final boolean localTime) { + if (dateTime == null) { + return NULL_LONG; + } + + if (localTime) { + return dateTime.toLocalTime().toNanoOfDay(); + } else { + return epochNanos(dateTime) - epochNanos(atMidnight(dateTime)); + } + } + + /** + * Returns the number of nanoseconds that have elapsed since the start of the day. + * + * @param localTime time + * @return {@link QueryConstants#NULL_LONG} if input is {@code null}; otherwise, number of nanoseconds that have + * elapsed since the start of the day + */ + public static long nanosOfDay(@Nullable final LocalTime localTime) { + if (localTime == null) { + return NULL_LONG; + } + + return localTime.toNanoOfDay(); + } + + /** + * Returns the number of milliseconds that have elapsed since the start of the day. + * + * @param instant time + * @param timeZone time zone + * @param localTime if {@code true}, returns the number of milliseconds from the start of the day according to the + * local time. In this case, 9:30AM always returns the same value. If {@code false}, returns the number of + * milliseconds from the start of the day. On days when daylight savings time events occur, results may be + * different from what is expected based upon the local time. For example, on daylight savings time change + * days, 9:30AM may be earlier or later in the day based upon if the daylight savings time adjustment is + * forwards or backwards. On non DST days, the result is the same as if localTime is false. + * @return {@link QueryConstants#NULL_INT} if either input is {@code null}; otherwise, number of milliseconds that + * have elapsed since the start of the day + */ + @ScriptApi + public static int millisOfDay(@Nullable final Instant instant, @Nullable final ZoneId timeZone, + final boolean localTime) { + if (instant == null || timeZone == null) { + return NULL_INT; + } + + return (int) nanosToMillis(nanosOfDay(instant, timeZone, localTime)); + } + + /** + * Returns the number of milliseconds that have elapsed since the start of the day. + * + * @param dateTime time + * @param localTime if {@code true}, returns the number of milliseconds from the start of the day according to the + * local time. In this case, 9:30AM always returns the same value. If {@code false}, returns the number of + * milliseconds from the start of the day. On days when daylight savings time events occur, results may be + * different from what is expected based upon the local time. For example, on daylight savings time change + * days, 9:30AM may be earlier or later in the day based upon if the daylight savings time adjustment is + * forwards or backwards. On non DST days, the result is the same as if localTime is false. + * @return {@link QueryConstants#NULL_INT} if either input is {@code null}; otherwise, number of milliseconds that + * have elapsed since the start of the day + */ + @ScriptApi + public static int millisOfDay(@Nullable final ZonedDateTime dateTime, final boolean localTime) { + if (dateTime == null) { + return NULL_INT; + } + + return (int) nanosToMillis(nanosOfDay(dateTime, localTime)); + } + + /** + * Returns the number of milliseconds that have elapsed since the start of the day. + * + * @param localTime time + * @return {@link QueryConstants#NULL_INT} if input is {@code null}; otherwise, number of milliseconds that have + * elapsed since the start of the day + */ + public static int millisOfDay(@Nullable final LocalTime localTime) { + if (localTime == null) { + return NULL_INT; + } + + return (int) nanosToMillis(nanosOfDay(localTime)); + } + + /** + * Returns the number of seconds that have elapsed since the start of the day. + * + * @param instant time + * @param timeZone time zone + * @param localTime if {@code true}, returns the number of nanos from the start of the day according to the local + * time. In this case, 9:30AM always returns the same value. If {@code false}, returns the number of nanos + * from the start of the day. On days when daylight savings time events occur, results may be different from + * what is expected based upon the local time. For example, on daylight savings time change days, 9:30AM may + * be earlier or later in the day based upon if the daylight savings time adjustment is forwards or + * backwards. On non DST days, the result is the same as if localTime is false. + * @return {@link QueryConstants#NULL_INT} if either input is {@code null}; otherwise, number of seconds that have + * elapsed since the start of the day + */ + @ScriptApi + public static int secondOfDay(@Nullable final Instant instant, @Nullable final ZoneId timeZone, + final boolean localTime) { + if (instant == null || timeZone == null) { + return NULL_INT; + } + + return (int) nanosToSeconds(nanosOfDay(instant, timeZone, localTime)); + } + + /** + * Returns the number of seconds that have elapsed since the start of the day. + * + * @param dateTime time + * @param localTime if {@code true}, returns the number of seconds from the start of the day according to the local + * time. In this case, 9:30AM always returns the same value. If {@code false}, returns the number of seconds + * from the start of the day. On days when daylight savings time events occur, results may be different from + * what is expected based upon the local time. For example, on daylight savings time change days, 9:30AM may + * be earlier or later in the day based upon if the daylight savings time adjustment is forwards or + * backwards. On non DST days, the result is the same as if localTime is false. + * @return {@link QueryConstants#NULL_INT} if either input is {@code null}; otherwise, number of seconds that have + * elapsed since the start of the day + */ + @ScriptApi + public static int secondOfDay(@Nullable final ZonedDateTime dateTime, final boolean localTime) { if (dateTime == null) { return NULL_INT; } - return minuteOfDay(dateTime) / 60; + return (int) nanosToSeconds(nanosOfDay(dateTime, localTime)); + } + + /** + * Returns the number of seconds that have elapsed since the start of the day. + * + * @param localTime time + * @return {@link QueryConstants#NULL_INT} if input is {@code null}; otherwise, number of seconds that have elapsed + * since the start of the day + */ + public static int secondOfDay(@Nullable final LocalTime localTime) { + if (localTime == null) { + return NULL_INT; + } + + return (int) nanosToSeconds(nanosOfDay(localTime)); + } + + /** + * Returns the number of minutes that have elapsed since the start of the day. + * + * @param instant time + * @param timeZone time zone + * @param localTime if {@code true}, returns the number of minutes from the start of the day according to the local + * time. In this case, 9:30AM always returns the same value. If {@code false}, returns the number of minutes + * from the start of the day. On days when daylight savings time events occur, results may be different from + * what is expected based upon the local time. For example, on daylight savings time change days, 9:30AM may + * be earlier or later in the day based upon if the daylight savings time adjustment is forwards or + * backwards. On non DST days, the result is the same as if localTime is false. + * @return {@link QueryConstants#NULL_INT} if either input is {@code null}; otherwise, number of minutes that have + * elapsed since the start of the day + */ + @ScriptApi + public static int minuteOfDay(@Nullable final Instant instant, @Nullable final ZoneId timeZone, + final boolean localTime) { + if (instant == null || timeZone == null) { + return NULL_INT; + } + + return secondOfDay(instant, timeZone, localTime) / 60; + } + + /** + * Returns the number of minutes that have elapsed since the start of the day. + * + * @param dateTime time + * @param localTime if {@code true}, returns the number of minutes from the start of the day according to the local + * time. In this case, 9:30AM always returns the same value. If {@code false}, returns the number of minutes + * from the start of the day. On days when daylight savings time events occur, results may be different from + * what is expected based upon the local time. For example, on daylight savings time change days, 9:30AM may + * be earlier or later in the day based upon if the daylight savings time adjustment is forwards or + * backwards. On non DST days, the result is the same as if localTime is false. + * @return {@link QueryConstants#NULL_INT} if either input is {@code null}; otherwise, number of minutes that have + * elapsed since the start of the day + */ + @ScriptApi + public static int minuteOfDay(@Nullable final ZonedDateTime dateTime, final boolean localTime) { + if (dateTime == null) { + return NULL_INT; + } + + return secondOfDay(dateTime, localTime) / 60; + } + + /** + * Returns the number of minutes that have elapsed since the start of the day. + * + * @param localTime time + * @return {@link QueryConstants#NULL_INT} if input is {@code null}; otherwise, number of minutes that have elapsed + * since the start of the day + */ + public static int minuteOfDay(@Nullable final LocalTime localTime) { + if (localTime == null) { + return NULL_INT; + } + + return secondOfDay(localTime) / 60; + } + + /** + * Returns the number of hours that have elapsed since the start of the day. + * + * @param instant time + * @param timeZone time zone + * @param localTime if {@code true}, returns the number of hours from the start of the day according to the local + * time. In this case, 9:30AM always returns the same value. If {@code false}, returns the number of hours + * from the start of the day. On days when daylight savings time events occur, results may be different from + * what is expected based upon the local time. For example, on daylight savings time change days, 9:30AM may + * be earlier or later in the day based upon if the daylight savings time adjustment is forwards or + * backwards. On non DST days, the result is the same as if localTime is false. + * @return {@link QueryConstants#NULL_INT} if either input is {@code null}; otherwise, number of hours that have + * elapsed since the start of the day + */ + @ScriptApi + public static int hourOfDay(@Nullable final Instant instant, @Nullable final ZoneId timeZone, + final boolean localTime) { + if (instant == null || timeZone == null) { + return NULL_INT; + } + + return hourOfDay(toZonedDateTime(instant, timeZone), localTime); + } + + /** + * Returns the number of hours that have elapsed since the start of the day. + * + * @param dateTime time + * @param localTime if {@code true}, returns the number of hours from the start of the day according to the local + * time. In this case, 9:30AM always returns the same value. If {@code false}, returns the number of hours + * from the start of the day. On days when daylight savings time events occur, results may be different from + * what is expected based upon the local time. For example, on daylight savings time change days, 9:30AM may + * be earlier or later in the day based upon if the daylight savings time adjustment is forwards or + * backwards. On non DST days, the result is the same as if localTime is false. + * @return {@link QueryConstants#NULL_INT} if either input is {@code null}; otherwise, number of hours that have + * elapsed since the start of the day + */ + @ScriptApi + public static int hourOfDay(@Nullable final ZonedDateTime dateTime, final boolean localTime) { + if (dateTime == null) { + return NULL_INT; + } + + return minuteOfDay(dateTime, localTime) / 60; + } + + /** + * Returns the number of hours that have elapsed since the start of the day. + * + * @param localTime time + * @return {@link QueryConstants#NULL_INT} if input is {@code null}; otherwise, number of hours that have elapsed + * since the start of the day + */ + public static int hourOfDay(@Nullable final LocalTime localTime) { + if (localTime == null) { + return NULL_INT; + } + + return minuteOfDay(localTime) / 60; } /** 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 be99c065aa0..68bf3c51a26 100644 --- a/engine/time/src/test/java/io/deephaven/time/TestDateTimeUtils.java +++ b/engine/time/src/test/java/io/deephaven/time/TestDateTimeUtils.java @@ -2609,13 +2609,22 @@ public void testDayOfYear() { public void testHourOfDay() { final Instant dt2 = DateTimeUtils.parseInstant("2023-01-02T11:23:45.123456789 JP"); final ZonedDateTime dt3 = dt2.atZone(TZ_JP); + final LocalTime lt = dt3.toLocalTime(); + + TestCase.assertEquals(11, DateTimeUtils.hourOfDay(dt2, TZ_JP, true)); + TestCase.assertEquals(11, DateTimeUtils.hourOfDay(dt2, TZ_JP, false)); + TestCase.assertEquals(NULL_INT, DateTimeUtils.hourOfDay(dt2, null, true)); + TestCase.assertEquals(NULL_INT, DateTimeUtils.hourOfDay(dt2, null, false)); + TestCase.assertEquals(NULL_INT, DateTimeUtils.hourOfDay(null, TZ_JP, true)); + TestCase.assertEquals(NULL_INT, DateTimeUtils.hourOfDay(null, TZ_JP, false)); - TestCase.assertEquals(11, DateTimeUtils.hourOfDay(dt2, TZ_JP)); - TestCase.assertEquals(NULL_INT, DateTimeUtils.hourOfDay(dt2, null)); - TestCase.assertEquals(NULL_INT, DateTimeUtils.hourOfDay(null, TZ_JP)); + TestCase.assertEquals(11, DateTimeUtils.hourOfDay(dt3, true)); + TestCase.assertEquals(11, DateTimeUtils.hourOfDay(dt3, false)); + TestCase.assertEquals(NULL_INT, DateTimeUtils.hourOfDay(null, true)); + TestCase.assertEquals(NULL_INT, DateTimeUtils.hourOfDay(null, false)); - TestCase.assertEquals(11, DateTimeUtils.hourOfDay(dt3)); - TestCase.assertEquals(NULL_INT, DateTimeUtils.hourOfDay(null)); + TestCase.assertEquals(11, DateTimeUtils.hourOfDay(lt)); + TestCase.assertEquals(NULL_INT, DateTimeUtils.hourOfDay((LocalTime) null)); // Test daylight savings time @@ -2623,36 +2632,54 @@ public void testHourOfDay() { final Instant dstI11 = DateTimeUtils.plus(dstMid1, DateTimeUtils.HOUR); final ZonedDateTime dstZdt11 = DateTimeUtils.toZonedDateTime(dstI11, ZoneId.of("America/Denver")); - TestCase.assertEquals(1, DateTimeUtils.hourOfDay(dstI11, ZoneId.of("America/Denver"))); - TestCase.assertEquals(1, DateTimeUtils.hourOfDay(dstZdt11)); + TestCase.assertEquals(1, DateTimeUtils.hourOfDay(dstI11, ZoneId.of("America/Denver"), true)); + TestCase.assertEquals(1, DateTimeUtils.hourOfDay(dstI11, ZoneId.of("America/Denver"), false)); + TestCase.assertEquals(1, DateTimeUtils.hourOfDay(dstZdt11, true)); + TestCase.assertEquals(1, DateTimeUtils.hourOfDay(dstZdt11, false)); + TestCase.assertEquals(1, DateTimeUtils.hourOfDay(dstZdt11.toLocalTime())); final Instant dstI12 = DateTimeUtils.plus(dstMid1, 2 * DateTimeUtils.HOUR); final ZonedDateTime dstZdt12 = DateTimeUtils.toZonedDateTime(dstI12, ZoneId.of("America/Denver")); - TestCase.assertEquals(2, DateTimeUtils.hourOfDay(dstI12, ZoneId.of("America/Denver"))); - TestCase.assertEquals(2, DateTimeUtils.hourOfDay(dstZdt12)); + TestCase.assertEquals(3, DateTimeUtils.hourOfDay(dstI12, ZoneId.of("America/Denver"), true)); + TestCase.assertEquals(2, DateTimeUtils.hourOfDay(dstI12, ZoneId.of("America/Denver"), false)); + TestCase.assertEquals(3, DateTimeUtils.hourOfDay(dstZdt12, true)); + TestCase.assertEquals(2, DateTimeUtils.hourOfDay(dstZdt12, false)); + TestCase.assertEquals(3, DateTimeUtils.hourOfDay(dstZdt12.toLocalTime())); final Instant dstI13 = DateTimeUtils.plus(dstMid1, 3 * DateTimeUtils.HOUR); final ZonedDateTime dstZdt13 = DateTimeUtils.toZonedDateTime(dstI13, ZoneId.of("America/Denver")); - TestCase.assertEquals(3, DateTimeUtils.hourOfDay(dstI13, ZoneId.of("America/Denver"))); - TestCase.assertEquals(3, DateTimeUtils.hourOfDay(dstZdt13)); + TestCase.assertEquals(4, DateTimeUtils.hourOfDay(dstI13, ZoneId.of("America/Denver"), true)); + TestCase.assertEquals(3, DateTimeUtils.hourOfDay(dstI13, ZoneId.of("America/Denver"), false)); + TestCase.assertEquals(4, DateTimeUtils.hourOfDay(dstZdt13, true)); + TestCase.assertEquals(3, DateTimeUtils.hourOfDay(dstZdt13, false)); + TestCase.assertEquals(4, DateTimeUtils.hourOfDay(dstZdt13.toLocalTime())); final Instant dstMid2 = DateTimeUtils.parseInstant("2023-11-05T00:00:00 America/Denver"); final Instant dstI21 = DateTimeUtils.plus(dstMid2, DateTimeUtils.HOUR); final ZonedDateTime dstZdt21 = DateTimeUtils.toZonedDateTime(dstI21, ZoneId.of("America/Denver")); - TestCase.assertEquals(1, DateTimeUtils.hourOfDay(dstI21, ZoneId.of("America/Denver"))); - TestCase.assertEquals(1, DateTimeUtils.hourOfDay(dstZdt21)); + TestCase.assertEquals(1, DateTimeUtils.hourOfDay(dstI21, ZoneId.of("America/Denver"), true)); + TestCase.assertEquals(1, DateTimeUtils.hourOfDay(dstI21, ZoneId.of("America/Denver"), false)); + TestCase.assertEquals(1, DateTimeUtils.hourOfDay(dstZdt21, true)); + TestCase.assertEquals(1, DateTimeUtils.hourOfDay(dstZdt21, false)); + TestCase.assertEquals(1, DateTimeUtils.hourOfDay(dstZdt21.toLocalTime())); final Instant dstI22 = DateTimeUtils.plus(dstMid2, 2 * DateTimeUtils.HOUR); final ZonedDateTime dstZdt22 = DateTimeUtils.toZonedDateTime(dstI22, ZoneId.of("America/Denver")); - TestCase.assertEquals(2, DateTimeUtils.hourOfDay(dstI22, ZoneId.of("America/Denver"))); - TestCase.assertEquals(2, DateTimeUtils.hourOfDay(dstZdt22)); + TestCase.assertEquals(1, DateTimeUtils.hourOfDay(dstI22, ZoneId.of("America/Denver"), true)); + TestCase.assertEquals(2, DateTimeUtils.hourOfDay(dstI22, ZoneId.of("America/Denver"), false)); + TestCase.assertEquals(1, DateTimeUtils.hourOfDay(dstZdt22, true)); + TestCase.assertEquals(2, DateTimeUtils.hourOfDay(dstZdt22, false)); + TestCase.assertEquals(1, DateTimeUtils.hourOfDay(dstZdt22.toLocalTime())); final Instant dstI23 = DateTimeUtils.plus(dstMid2, 3 * DateTimeUtils.HOUR); final ZonedDateTime dstZdt23 = DateTimeUtils.toZonedDateTime(dstI23, ZoneId.of("America/Denver")); - TestCase.assertEquals(3, DateTimeUtils.hourOfDay(dstI23, ZoneId.of("America/Denver"))); - TestCase.assertEquals(3, DateTimeUtils.hourOfDay(dstZdt23)); + TestCase.assertEquals(2, DateTimeUtils.hourOfDay(dstI23, ZoneId.of("America/Denver"), true)); + TestCase.assertEquals(3, DateTimeUtils.hourOfDay(dstI23, ZoneId.of("America/Denver"), false)); + TestCase.assertEquals(2, DateTimeUtils.hourOfDay(dstZdt23, true)); + TestCase.assertEquals(3, DateTimeUtils.hourOfDay(dstZdt23, false)); + TestCase.assertEquals(2, DateTimeUtils.hourOfDay(dstZdt23.toLocalTime())); } public void testMinuteOfHour() { @@ -2670,13 +2697,22 @@ public void testMinuteOfHour() { public void testMinuteOfDay() { final Instant dt2 = DateTimeUtils.parseInstant("2023-01-02T11:23:45.123456789 JP"); final ZonedDateTime dt3 = dt2.atZone(TZ_JP); + final LocalTime lt = dt3.toLocalTime(); - TestCase.assertEquals(11 * 60 + 23, DateTimeUtils.minuteOfDay(dt2, TZ_JP)); - TestCase.assertEquals(NULL_INT, DateTimeUtils.minuteOfDay(dt2, null)); - TestCase.assertEquals(NULL_INT, DateTimeUtils.minuteOfDay(null, TZ_JP)); + TestCase.assertEquals(11 * 60 + 23, DateTimeUtils.minuteOfDay(dt2, TZ_JP, true)); + TestCase.assertEquals(11 * 60 + 23, DateTimeUtils.minuteOfDay(dt2, TZ_JP, false)); + TestCase.assertEquals(NULL_INT, DateTimeUtils.minuteOfDay(dt2, null, true)); + TestCase.assertEquals(NULL_INT, DateTimeUtils.minuteOfDay(dt2, null, false)); + TestCase.assertEquals(NULL_INT, DateTimeUtils.minuteOfDay(null, TZ_JP, true)); + TestCase.assertEquals(NULL_INT, DateTimeUtils.minuteOfDay(null, TZ_JP, false)); - TestCase.assertEquals(11 * 60 + 23, DateTimeUtils.minuteOfDay(dt3)); - TestCase.assertEquals(NULL_INT, DateTimeUtils.minuteOfDay(null)); + TestCase.assertEquals(11 * 60 + 23, DateTimeUtils.minuteOfDay(dt3, true)); + TestCase.assertEquals(11 * 60 + 23, DateTimeUtils.minuteOfDay(dt3, false)); + TestCase.assertEquals(NULL_INT, DateTimeUtils.minuteOfDay(null, true)); + TestCase.assertEquals(NULL_INT, DateTimeUtils.minuteOfDay(null, false)); + + TestCase.assertEquals(11 * 60 + 23, DateTimeUtils.minuteOfDay(lt)); + TestCase.assertEquals(NULL_INT, DateTimeUtils.minuteOfDay((LocalTime) null)); // Test daylight savings time @@ -2685,20 +2721,37 @@ public void testMinuteOfDay() { final Instant dstI11 = DateTimeUtils.plus(dstMid1, DateTimeUtils.HOUR); final ZonedDateTime dstZdt11 = DateTimeUtils.toZonedDateTime(dstI11, ZoneId.of("America/Denver")); TestCase.assertEquals(DateTimeUtils.HOUR / DateTimeUtils.MINUTE, - DateTimeUtils.minuteOfDay(dstI11, ZoneId.of("America/Denver"))); - TestCase.assertEquals(DateTimeUtils.HOUR / DateTimeUtils.MINUTE, DateTimeUtils.minuteOfDay(dstZdt11)); + DateTimeUtils.minuteOfDay(dstI11, ZoneId.of("America/Denver"), true)); + TestCase.assertEquals(DateTimeUtils.HOUR / DateTimeUtils.MINUTE, + DateTimeUtils.minuteOfDay(dstI11, ZoneId.of("America/Denver"), false)); + TestCase.assertEquals(DateTimeUtils.HOUR / DateTimeUtils.MINUTE, DateTimeUtils.minuteOfDay(dstZdt11, true)); + TestCase.assertEquals(DateTimeUtils.HOUR / DateTimeUtils.MINUTE, DateTimeUtils.minuteOfDay(dstZdt11, false)); + TestCase.assertEquals(DateTimeUtils.HOUR / DateTimeUtils.MINUTE, + DateTimeUtils.minuteOfDay(dstZdt11.toLocalTime())); final Instant dstI12 = DateTimeUtils.plus(dstMid1, 2 * DateTimeUtils.HOUR); final ZonedDateTime dstZdt12 = DateTimeUtils.toZonedDateTime(dstI12, ZoneId.of("America/Denver")); + TestCase.assertEquals(3 * DateTimeUtils.HOUR / DateTimeUtils.MINUTE, + DateTimeUtils.minuteOfDay(dstI12, ZoneId.of("America/Denver"), true)); + TestCase.assertEquals(2 * DateTimeUtils.HOUR / DateTimeUtils.MINUTE, + DateTimeUtils.minuteOfDay(dstI12, ZoneId.of("America/Denver"), false)); + TestCase.assertEquals(3 * DateTimeUtils.HOUR / DateTimeUtils.MINUTE, DateTimeUtils.minuteOfDay(dstZdt12, true)); TestCase.assertEquals(2 * DateTimeUtils.HOUR / DateTimeUtils.MINUTE, - DateTimeUtils.minuteOfDay(dstI12, ZoneId.of("America/Denver"))); - TestCase.assertEquals(2 * DateTimeUtils.HOUR / DateTimeUtils.MINUTE, DateTimeUtils.minuteOfDay(dstZdt12)); + DateTimeUtils.minuteOfDay(dstZdt12, false)); + TestCase.assertEquals(3 * DateTimeUtils.HOUR / DateTimeUtils.MINUTE, + DateTimeUtils.minuteOfDay(dstZdt12.toLocalTime())); final Instant dstI13 = DateTimeUtils.plus(dstMid1, 3 * DateTimeUtils.HOUR); final ZonedDateTime dstZdt13 = DateTimeUtils.toZonedDateTime(dstI13, ZoneId.of("America/Denver")); + TestCase.assertEquals(4 * DateTimeUtils.HOUR / DateTimeUtils.MINUTE, + DateTimeUtils.minuteOfDay(dstI13, ZoneId.of("America/Denver"), true)); TestCase.assertEquals(3 * DateTimeUtils.HOUR / DateTimeUtils.MINUTE, - DateTimeUtils.minuteOfDay(dstI13, ZoneId.of("America/Denver"))); - TestCase.assertEquals(3 * DateTimeUtils.HOUR / DateTimeUtils.MINUTE, DateTimeUtils.minuteOfDay(dstZdt13)); + DateTimeUtils.minuteOfDay(dstI13, ZoneId.of("America/Denver"), false)); + TestCase.assertEquals(4 * DateTimeUtils.HOUR / DateTimeUtils.MINUTE, DateTimeUtils.minuteOfDay(dstZdt13, true)); + TestCase.assertEquals(3 * DateTimeUtils.HOUR / DateTimeUtils.MINUTE, + DateTimeUtils.minuteOfDay(dstZdt13, false)); + TestCase.assertEquals(4 * DateTimeUtils.HOUR / DateTimeUtils.MINUTE, + DateTimeUtils.minuteOfDay(dstZdt13.toLocalTime())); final Instant dstMid2 = DateTimeUtils.parseInstant("2023-11-05T00:00:00 America/Denver"); @@ -2706,20 +2759,37 @@ public void testMinuteOfDay() { final Instant dstI21 = DateTimeUtils.plus(dstMid2, DateTimeUtils.HOUR); final ZonedDateTime dstZdt21 = DateTimeUtils.toZonedDateTime(dstI21, ZoneId.of("America/Denver")); TestCase.assertEquals(DateTimeUtils.HOUR / DateTimeUtils.MINUTE, - DateTimeUtils.minuteOfDay(dstI21, ZoneId.of("America/Denver"))); - TestCase.assertEquals(DateTimeUtils.HOUR / DateTimeUtils.MINUTE, DateTimeUtils.minuteOfDay(dstZdt21)); + DateTimeUtils.minuteOfDay(dstI21, ZoneId.of("America/Denver"), true)); + TestCase.assertEquals(DateTimeUtils.HOUR / DateTimeUtils.MINUTE, + DateTimeUtils.minuteOfDay(dstI21, ZoneId.of("America/Denver"), false)); + TestCase.assertEquals(DateTimeUtils.HOUR / DateTimeUtils.MINUTE, DateTimeUtils.minuteOfDay(dstZdt21, true)); + TestCase.assertEquals(DateTimeUtils.HOUR / DateTimeUtils.MINUTE, DateTimeUtils.minuteOfDay(dstZdt21, false)); + TestCase.assertEquals(DateTimeUtils.HOUR / DateTimeUtils.MINUTE, + DateTimeUtils.minuteOfDay(dstZdt21.toLocalTime())); final Instant dstI22 = DateTimeUtils.plus(dstMid2, 2 * DateTimeUtils.HOUR); final ZonedDateTime dstZdt22 = DateTimeUtils.toZonedDateTime(dstI22, ZoneId.of("America/Denver")); + TestCase.assertEquals(DateTimeUtils.HOUR / DateTimeUtils.MINUTE, + DateTimeUtils.minuteOfDay(dstI22, ZoneId.of("America/Denver"), true)); + TestCase.assertEquals(2 * DateTimeUtils.HOUR / DateTimeUtils.MINUTE, + DateTimeUtils.minuteOfDay(dstI22, ZoneId.of("America/Denver"), false)); + TestCase.assertEquals(DateTimeUtils.HOUR / DateTimeUtils.MINUTE, DateTimeUtils.minuteOfDay(dstZdt22, true)); TestCase.assertEquals(2 * DateTimeUtils.HOUR / DateTimeUtils.MINUTE, - DateTimeUtils.minuteOfDay(dstI22, ZoneId.of("America/Denver"))); - TestCase.assertEquals(2 * DateTimeUtils.HOUR / DateTimeUtils.MINUTE, DateTimeUtils.minuteOfDay(dstZdt22)); + DateTimeUtils.minuteOfDay(dstZdt22, false)); + TestCase.assertEquals(DateTimeUtils.HOUR / DateTimeUtils.MINUTE, + DateTimeUtils.minuteOfDay(dstZdt22.toLocalTime())); final Instant dstI23 = DateTimeUtils.plus(dstMid2, 3 * DateTimeUtils.HOUR); final ZonedDateTime dstZdt23 = DateTimeUtils.toZonedDateTime(dstI23, ZoneId.of("America/Denver")); + TestCase.assertEquals(2 * DateTimeUtils.HOUR / DateTimeUtils.MINUTE, + DateTimeUtils.minuteOfDay(dstI23, ZoneId.of("America/Denver"), true)); + TestCase.assertEquals(3 * DateTimeUtils.HOUR / DateTimeUtils.MINUTE, + DateTimeUtils.minuteOfDay(dstI23, ZoneId.of("America/Denver"), false)); + TestCase.assertEquals(2 * DateTimeUtils.HOUR / DateTimeUtils.MINUTE, DateTimeUtils.minuteOfDay(dstZdt23, true)); TestCase.assertEquals(3 * DateTimeUtils.HOUR / DateTimeUtils.MINUTE, - DateTimeUtils.minuteOfDay(dstI23, ZoneId.of("America/Denver"))); - TestCase.assertEquals(3 * DateTimeUtils.HOUR / DateTimeUtils.MINUTE, DateTimeUtils.minuteOfDay(dstZdt23)); + DateTimeUtils.minuteOfDay(dstZdt23, false)); + TestCase.assertEquals(2 * DateTimeUtils.HOUR / DateTimeUtils.MINUTE, + DateTimeUtils.minuteOfDay(dstZdt23.toLocalTime())); } public void testSecondOfMinute() { @@ -2737,13 +2807,22 @@ public void testSecondOfMinute() { public void testSecondOfDay() { final Instant dt2 = DateTimeUtils.parseInstant("2023-01-02T11:23:45.123456789 JP"); final ZonedDateTime dt3 = dt2.atZone(TZ_JP); + final LocalTime lt = dt3.toLocalTime(); + + TestCase.assertEquals(11 * 60 * 60 + 23 * 60 + 45, DateTimeUtils.secondOfDay(dt2, TZ_JP, true)); + TestCase.assertEquals(11 * 60 * 60 + 23 * 60 + 45, DateTimeUtils.secondOfDay(dt2, TZ_JP, false)); + TestCase.assertEquals(NULL_INT, DateTimeUtils.secondOfDay(dt2, null, true)); + TestCase.assertEquals(NULL_INT, DateTimeUtils.secondOfDay(dt2, null, false)); + TestCase.assertEquals(NULL_INT, DateTimeUtils.secondOfDay(null, TZ_JP, true)); + TestCase.assertEquals(NULL_INT, DateTimeUtils.secondOfDay(null, TZ_JP, false)); - TestCase.assertEquals(11 * 60 * 60 + 23 * 60 + 45, DateTimeUtils.secondOfDay(dt2, TZ_JP)); - TestCase.assertEquals(NULL_INT, DateTimeUtils.secondOfDay(dt2, null)); - TestCase.assertEquals(NULL_INT, DateTimeUtils.secondOfDay(null, TZ_JP)); + TestCase.assertEquals(11 * 60 * 60 + 23 * 60 + 45, DateTimeUtils.secondOfDay(dt3, true)); + TestCase.assertEquals(11 * 60 * 60 + 23 * 60 + 45, DateTimeUtils.secondOfDay(dt3, false)); + TestCase.assertEquals(NULL_INT, DateTimeUtils.secondOfDay(null, true)); + TestCase.assertEquals(NULL_INT, DateTimeUtils.secondOfDay(null, false)); - TestCase.assertEquals(11 * 60 * 60 + 23 * 60 + 45, DateTimeUtils.secondOfDay(dt3)); - TestCase.assertEquals(NULL_INT, DateTimeUtils.secondOfDay(null)); + TestCase.assertEquals(11 * 60 * 60 + 23 * 60 + 45, DateTimeUtils.secondOfDay(lt)); + TestCase.assertEquals(NULL_INT, DateTimeUtils.secondOfDay((LocalTime) null)); // Test daylight savings time @@ -2752,20 +2831,37 @@ public void testSecondOfDay() { final Instant dstI11 = DateTimeUtils.plus(dstMid1, DateTimeUtils.HOUR); final ZonedDateTime dstZdt11 = DateTimeUtils.toZonedDateTime(dstI11, ZoneId.of("America/Denver")); TestCase.assertEquals(DateTimeUtils.HOUR / DateTimeUtils.SECOND, - DateTimeUtils.secondOfDay(dstI11, ZoneId.of("America/Denver"))); - TestCase.assertEquals(DateTimeUtils.HOUR / DateTimeUtils.SECOND, DateTimeUtils.secondOfDay(dstZdt11)); + DateTimeUtils.secondOfDay(dstI11, ZoneId.of("America/Denver"), true)); + TestCase.assertEquals(DateTimeUtils.HOUR / DateTimeUtils.SECOND, + DateTimeUtils.secondOfDay(dstI11, ZoneId.of("America/Denver"), false)); + TestCase.assertEquals(DateTimeUtils.HOUR / DateTimeUtils.SECOND, DateTimeUtils.secondOfDay(dstZdt11, true)); + TestCase.assertEquals(DateTimeUtils.HOUR / DateTimeUtils.SECOND, DateTimeUtils.secondOfDay(dstZdt11, false)); + TestCase.assertEquals(DateTimeUtils.HOUR / DateTimeUtils.SECOND, + DateTimeUtils.secondOfDay(dstZdt11.toLocalTime())); final Instant dstI12 = DateTimeUtils.plus(dstMid1, 2 * DateTimeUtils.HOUR); final ZonedDateTime dstZdt12 = DateTimeUtils.toZonedDateTime(dstI12, ZoneId.of("America/Denver")); + TestCase.assertEquals(3 * DateTimeUtils.HOUR / DateTimeUtils.SECOND, + DateTimeUtils.secondOfDay(dstI12, ZoneId.of("America/Denver"), true)); TestCase.assertEquals(2 * DateTimeUtils.HOUR / DateTimeUtils.SECOND, - DateTimeUtils.secondOfDay(dstI12, ZoneId.of("America/Denver"))); - TestCase.assertEquals(2 * DateTimeUtils.HOUR / DateTimeUtils.SECOND, DateTimeUtils.secondOfDay(dstZdt12)); + DateTimeUtils.secondOfDay(dstI12, ZoneId.of("America/Denver"), false)); + TestCase.assertEquals(3 * DateTimeUtils.HOUR / DateTimeUtils.SECOND, DateTimeUtils.secondOfDay(dstZdt12, true)); + TestCase.assertEquals(2 * DateTimeUtils.HOUR / DateTimeUtils.SECOND, + DateTimeUtils.secondOfDay(dstZdt12, false)); + TestCase.assertEquals(3 * DateTimeUtils.HOUR / DateTimeUtils.SECOND, + DateTimeUtils.secondOfDay(dstZdt12.toLocalTime())); final Instant dstI13 = DateTimeUtils.plus(dstMid1, 3 * DateTimeUtils.HOUR); final ZonedDateTime dstZdt13 = DateTimeUtils.toZonedDateTime(dstI13, ZoneId.of("America/Denver")); + TestCase.assertEquals(4 * DateTimeUtils.HOUR / DateTimeUtils.SECOND, + DateTimeUtils.secondOfDay(dstI13, ZoneId.of("America/Denver"), true)); + TestCase.assertEquals(3 * DateTimeUtils.HOUR / DateTimeUtils.SECOND, + DateTimeUtils.secondOfDay(dstI13, ZoneId.of("America/Denver"), false)); + TestCase.assertEquals(4 * DateTimeUtils.HOUR / DateTimeUtils.SECOND, DateTimeUtils.secondOfDay(dstZdt13, true)); TestCase.assertEquals(3 * DateTimeUtils.HOUR / DateTimeUtils.SECOND, - DateTimeUtils.secondOfDay(dstI13, ZoneId.of("America/Denver"))); - TestCase.assertEquals(3 * DateTimeUtils.HOUR / DateTimeUtils.SECOND, DateTimeUtils.secondOfDay(dstZdt13)); + DateTimeUtils.secondOfDay(dstZdt13, false)); + TestCase.assertEquals(4 * DateTimeUtils.HOUR / DateTimeUtils.SECOND, + DateTimeUtils.secondOfDay(dstZdt13.toLocalTime())); final Instant dstMid2 = DateTimeUtils.parseInstant("2023-11-05T00:00:00 America/Denver"); @@ -2773,20 +2869,37 @@ public void testSecondOfDay() { final Instant dstI21 = DateTimeUtils.plus(dstMid2, DateTimeUtils.HOUR); final ZonedDateTime dstZdt21 = DateTimeUtils.toZonedDateTime(dstI21, ZoneId.of("America/Denver")); TestCase.assertEquals(DateTimeUtils.HOUR / DateTimeUtils.SECOND, - DateTimeUtils.secondOfDay(dstI21, ZoneId.of("America/Denver"))); - TestCase.assertEquals(DateTimeUtils.HOUR / DateTimeUtils.SECOND, DateTimeUtils.secondOfDay(dstZdt21)); + DateTimeUtils.secondOfDay(dstI21, ZoneId.of("America/Denver"), true)); + TestCase.assertEquals(DateTimeUtils.HOUR / DateTimeUtils.SECOND, + DateTimeUtils.secondOfDay(dstI21, ZoneId.of("America/Denver"), false)); + TestCase.assertEquals(DateTimeUtils.HOUR / DateTimeUtils.SECOND, DateTimeUtils.secondOfDay(dstZdt21, true)); + TestCase.assertEquals(DateTimeUtils.HOUR / DateTimeUtils.SECOND, DateTimeUtils.secondOfDay(dstZdt21, false)); + TestCase.assertEquals(DateTimeUtils.HOUR / DateTimeUtils.SECOND, + DateTimeUtils.secondOfDay(dstZdt21.toLocalTime())); final Instant dstI22 = DateTimeUtils.plus(dstMid2, 2 * DateTimeUtils.HOUR); final ZonedDateTime dstZdt22 = DateTimeUtils.toZonedDateTime(dstI22, ZoneId.of("America/Denver")); + TestCase.assertEquals(DateTimeUtils.HOUR / DateTimeUtils.SECOND, + DateTimeUtils.secondOfDay(dstI22, ZoneId.of("America/Denver"), true)); TestCase.assertEquals(2 * DateTimeUtils.HOUR / DateTimeUtils.SECOND, - DateTimeUtils.secondOfDay(dstI22, ZoneId.of("America/Denver"))); - TestCase.assertEquals(2 * DateTimeUtils.HOUR / DateTimeUtils.SECOND, DateTimeUtils.secondOfDay(dstZdt22)); + DateTimeUtils.secondOfDay(dstI22, ZoneId.of("America/Denver"), false)); + TestCase.assertEquals(DateTimeUtils.HOUR / DateTimeUtils.SECOND, DateTimeUtils.secondOfDay(dstZdt22, true)); + TestCase.assertEquals(2 * DateTimeUtils.HOUR / DateTimeUtils.SECOND, + DateTimeUtils.secondOfDay(dstZdt22, false)); + TestCase.assertEquals(DateTimeUtils.HOUR / DateTimeUtils.SECOND, + DateTimeUtils.secondOfDay(dstZdt22.toLocalTime())); final Instant dstI23 = DateTimeUtils.plus(dstMid2, 3 * DateTimeUtils.HOUR); final ZonedDateTime dstZdt23 = DateTimeUtils.toZonedDateTime(dstI23, ZoneId.of("America/Denver")); + TestCase.assertEquals(2 * DateTimeUtils.HOUR / DateTimeUtils.SECOND, + DateTimeUtils.secondOfDay(dstI23, ZoneId.of("America/Denver"), true)); TestCase.assertEquals(3 * DateTimeUtils.HOUR / DateTimeUtils.SECOND, - DateTimeUtils.secondOfDay(dstI23, ZoneId.of("America/Denver"))); - TestCase.assertEquals(3 * DateTimeUtils.HOUR / DateTimeUtils.SECOND, DateTimeUtils.secondOfDay(dstZdt23)); + DateTimeUtils.secondOfDay(dstI23, ZoneId.of("America/Denver"), false)); + TestCase.assertEquals(2 * DateTimeUtils.HOUR / DateTimeUtils.SECOND, DateTimeUtils.secondOfDay(dstZdt23, true)); + TestCase.assertEquals(3 * DateTimeUtils.HOUR / DateTimeUtils.SECOND, + DateTimeUtils.secondOfDay(dstZdt23, false)); + TestCase.assertEquals(2 * DateTimeUtils.HOUR / DateTimeUtils.SECOND, + DateTimeUtils.secondOfDay(dstZdt23.toLocalTime())); } public void testNanosOfSecond() { @@ -2818,12 +2931,17 @@ public void testNanosOfDay() { final LocalTime lt = dt3.toLocalTime(); final long expectedNanos = 123456789L + 1_000_000_000L * (45 + 23 * 60 + 11 * 60 * 60); - TestCase.assertEquals(expectedNanos, DateTimeUtils.nanosOfDay(dt2, TZ_JP)); - TestCase.assertEquals(NULL_LONG, DateTimeUtils.nanosOfDay(dt2, null)); - TestCase.assertEquals(NULL_LONG, DateTimeUtils.nanosOfDay(null, TZ_JP)); + TestCase.assertEquals(expectedNanos, DateTimeUtils.nanosOfDay(dt2, TZ_JP, true)); + TestCase.assertEquals(expectedNanos, DateTimeUtils.nanosOfDay(dt2, TZ_JP, false)); + TestCase.assertEquals(NULL_LONG, DateTimeUtils.nanosOfDay(dt2, null, true)); + TestCase.assertEquals(NULL_LONG, DateTimeUtils.nanosOfDay(dt2, null, false)); + TestCase.assertEquals(NULL_LONG, DateTimeUtils.nanosOfDay(null, TZ_JP, true)); + TestCase.assertEquals(NULL_LONG, DateTimeUtils.nanosOfDay(null, TZ_JP, false)); - TestCase.assertEquals(expectedNanos, DateTimeUtils.nanosOfDay(dt3)); - TestCase.assertEquals(NULL_LONG, DateTimeUtils.nanosOfDay((ZonedDateTime) null)); + TestCase.assertEquals(expectedNanos, DateTimeUtils.nanosOfDay(dt3, true)); + TestCase.assertEquals(expectedNanos, DateTimeUtils.nanosOfDay(dt3, false)); + TestCase.assertEquals(NULL_LONG, DateTimeUtils.nanosOfDay((ZonedDateTime) null, true)); + TestCase.assertEquals(NULL_LONG, DateTimeUtils.nanosOfDay((ZonedDateTime) null, false)); TestCase.assertEquals(expectedNanos, DateTimeUtils.nanosOfDay(lt)); TestCase.assertEquals(NULL_LONG, DateTimeUtils.nanosOfDay((LocalTime) null)); @@ -2835,23 +2953,33 @@ public void testNanosOfDay() { final Instant dstI11 = DateTimeUtils.plus(dstMid1, DateTimeUtils.HOUR); final ZonedDateTime dstZdt11 = DateTimeUtils.toZonedDateTime(dstI11, ZoneId.of("America/Denver")); final LocalTime dstLt11 = dstZdt11.toLocalTime(); - TestCase.assertEquals(DateTimeUtils.HOUR, DateTimeUtils.nanosOfDay(dstI11, ZoneId.of("America/Denver"))); - TestCase.assertEquals(DateTimeUtils.HOUR, DateTimeUtils.nanosOfDay(dstZdt11)); + TestCase.assertEquals(DateTimeUtils.HOUR, DateTimeUtils.nanosOfDay(dstI11, ZoneId.of("America/Denver"), true)); + TestCase.assertEquals(DateTimeUtils.HOUR, DateTimeUtils.nanosOfDay(dstI11, ZoneId.of("America/Denver"), false)); + TestCase.assertEquals(DateTimeUtils.HOUR, DateTimeUtils.nanosOfDay(dstZdt11, true)); + TestCase.assertEquals(DateTimeUtils.HOUR, DateTimeUtils.nanosOfDay(dstZdt11, false)); TestCase.assertEquals(DateTimeUtils.HOUR, DateTimeUtils.nanosOfDay(dstLt11)); final Instant dstI12 = DateTimeUtils.plus(dstMid1, 2 * DateTimeUtils.HOUR); final ZonedDateTime dstZdt12 = DateTimeUtils.toZonedDateTime(dstI12, ZoneId.of("America/Denver")); final LocalTime dstLt12 = dstZdt12.toLocalTime(); - TestCase.assertEquals(2 * DateTimeUtils.HOUR, DateTimeUtils.nanosOfDay(dstI12, ZoneId.of("America/Denver"))); - TestCase.assertEquals(2 * DateTimeUtils.HOUR, DateTimeUtils.nanosOfDay(dstZdt12)); + TestCase.assertEquals(3 * DateTimeUtils.HOUR, + DateTimeUtils.nanosOfDay(dstI12, ZoneId.of("America/Denver"), true)); + TestCase.assertEquals(2 * DateTimeUtils.HOUR, + DateTimeUtils.nanosOfDay(dstI12, ZoneId.of("America/Denver"), false)); + TestCase.assertEquals(3 * DateTimeUtils.HOUR, DateTimeUtils.nanosOfDay(dstZdt12, true)); + TestCase.assertEquals(2 * DateTimeUtils.HOUR, DateTimeUtils.nanosOfDay(dstZdt12, false)); TestCase.assertEquals(3 * DateTimeUtils.HOUR, DateTimeUtils.nanosOfDay(dstLt12)); // Adjusted final Instant dstI13 = DateTimeUtils.plus(dstMid1, 3 * DateTimeUtils.HOUR); final ZonedDateTime dstZdt13 = DateTimeUtils.toZonedDateTime(dstI13, ZoneId.of("America/Denver")); final LocalTime dstLt13 = dstZdt13.toLocalTime(); - TestCase.assertEquals(3 * DateTimeUtils.HOUR, DateTimeUtils.nanosOfDay(dstI13, ZoneId.of("America/Denver"))); - TestCase.assertEquals(3 * DateTimeUtils.HOUR, DateTimeUtils.nanosOfDay(dstZdt13)); + TestCase.assertEquals(4 * DateTimeUtils.HOUR, + DateTimeUtils.nanosOfDay(dstI13, ZoneId.of("America/Denver"), true)); + TestCase.assertEquals(3 * DateTimeUtils.HOUR, + DateTimeUtils.nanosOfDay(dstI13, ZoneId.of("America/Denver"), false)); + TestCase.assertEquals(4 * DateTimeUtils.HOUR, DateTimeUtils.nanosOfDay(dstZdt13, true)); + TestCase.assertEquals(3 * DateTimeUtils.HOUR, DateTimeUtils.nanosOfDay(dstZdt13, false)); TestCase.assertEquals(4 * DateTimeUtils.HOUR, DateTimeUtils.nanosOfDay(dstLt13)); // Adjusted @@ -2860,22 +2988,31 @@ public void testNanosOfDay() { final Instant dstI21 = DateTimeUtils.plus(dstMid2, DateTimeUtils.HOUR); final ZonedDateTime dstZdt21 = DateTimeUtils.toZonedDateTime(dstI21, ZoneId.of("America/Denver")); final LocalTime dstLt21 = dstZdt21.toLocalTime(); - TestCase.assertEquals(DateTimeUtils.HOUR, DateTimeUtils.nanosOfDay(dstI21, ZoneId.of("America/Denver"))); - TestCase.assertEquals(DateTimeUtils.HOUR, DateTimeUtils.nanosOfDay(dstZdt21)); + TestCase.assertEquals(DateTimeUtils.HOUR, DateTimeUtils.nanosOfDay(dstI21, ZoneId.of("America/Denver"), true)); + TestCase.assertEquals(DateTimeUtils.HOUR, DateTimeUtils.nanosOfDay(dstI21, ZoneId.of("America/Denver"), false)); + TestCase.assertEquals(DateTimeUtils.HOUR, DateTimeUtils.nanosOfDay(dstZdt21, true)); + TestCase.assertEquals(DateTimeUtils.HOUR, DateTimeUtils.nanosOfDay(dstZdt21, false)); TestCase.assertEquals(DateTimeUtils.HOUR, DateTimeUtils.nanosOfDay(dstLt21)); final Instant dstI22 = DateTimeUtils.plus(dstMid2, 2 * DateTimeUtils.HOUR); final ZonedDateTime dstZdt22 = DateTimeUtils.toZonedDateTime(dstI22, ZoneId.of("America/Denver")); final LocalTime dstLt22 = dstZdt22.toLocalTime(); - TestCase.assertEquals(2 * DateTimeUtils.HOUR, DateTimeUtils.nanosOfDay(dstI22, ZoneId.of("America/Denver"))); - TestCase.assertEquals(2 * DateTimeUtils.HOUR, DateTimeUtils.nanosOfDay(dstZdt22)); + TestCase.assertEquals(DateTimeUtils.HOUR, DateTimeUtils.nanosOfDay(dstI22, ZoneId.of("America/Denver"), true)); + TestCase.assertEquals(2 * DateTimeUtils.HOUR, + DateTimeUtils.nanosOfDay(dstI22, ZoneId.of("America/Denver"), false)); + TestCase.assertEquals(DateTimeUtils.HOUR, DateTimeUtils.nanosOfDay(dstZdt22, true)); + TestCase.assertEquals(2 * DateTimeUtils.HOUR, DateTimeUtils.nanosOfDay(dstZdt22, false)); TestCase.assertEquals(DateTimeUtils.HOUR, DateTimeUtils.nanosOfDay(dstLt22)); // Adjusted final Instant dstI23 = DateTimeUtils.plus(dstMid2, 3 * DateTimeUtils.HOUR); final ZonedDateTime dstZdt23 = DateTimeUtils.toZonedDateTime(dstI23, ZoneId.of("America/Denver")); final LocalTime dstLt23 = dstZdt23.toLocalTime(); - TestCase.assertEquals(3 * DateTimeUtils.HOUR, DateTimeUtils.nanosOfDay(dstI23, ZoneId.of("America/Denver"))); - TestCase.assertEquals(3 * DateTimeUtils.HOUR, DateTimeUtils.nanosOfDay(dstZdt23)); + TestCase.assertEquals(2 * DateTimeUtils.HOUR, + DateTimeUtils.nanosOfDay(dstI23, ZoneId.of("America/Denver"), true)); + TestCase.assertEquals(3 * DateTimeUtils.HOUR, + DateTimeUtils.nanosOfDay(dstI23, ZoneId.of("America/Denver"), false)); + TestCase.assertEquals(2 * DateTimeUtils.HOUR, DateTimeUtils.nanosOfDay(dstZdt23, true)); + TestCase.assertEquals(3 * DateTimeUtils.HOUR, DateTimeUtils.nanosOfDay(dstZdt23, false)); TestCase.assertEquals(2 * DateTimeUtils.HOUR, DateTimeUtils.nanosOfDay(dstLt23)); // Adjusted } @@ -2894,13 +3031,24 @@ public void testMillisOfSecond() { public void testMillisOfDay() { final Instant dt2 = DateTimeUtils.parseInstant("2023-01-02T11:23:45.123456789 JP"); final ZonedDateTime dt3 = dt2.atZone(TZ_JP); + final LocalTime lt = dt3.toLocalTime(); - TestCase.assertEquals(123L + 1_000L * (45 + 23 * 60 + 11 * 60 * 60), DateTimeUtils.millisOfDay(dt2, TZ_JP)); - TestCase.assertEquals(NULL_INT, DateTimeUtils.millisOfDay(dt2, null)); - TestCase.assertEquals(NULL_INT, DateTimeUtils.millisOfDay(null, TZ_JP)); + TestCase.assertEquals(123L + 1_000L * (45 + 23 * 60 + 11 * 60 * 60), + DateTimeUtils.millisOfDay(dt2, TZ_JP, true)); + TestCase.assertEquals(123L + 1_000L * (45 + 23 * 60 + 11 * 60 * 60), + DateTimeUtils.millisOfDay(dt2, TZ_JP, false)); + TestCase.assertEquals(NULL_INT, DateTimeUtils.millisOfDay(dt2, null, true)); + TestCase.assertEquals(NULL_INT, DateTimeUtils.millisOfDay(dt2, null, false)); + TestCase.assertEquals(NULL_INT, DateTimeUtils.millisOfDay(null, TZ_JP, true)); + TestCase.assertEquals(NULL_INT, DateTimeUtils.millisOfDay(null, TZ_JP, false)); - TestCase.assertEquals(123L + 1_000L * (45 + 23 * 60 + 11 * 60 * 60), DateTimeUtils.millisOfDay(dt3)); - TestCase.assertEquals(NULL_INT, DateTimeUtils.millisOfDay(null)); + TestCase.assertEquals(123L + 1_000L * (45 + 23 * 60 + 11 * 60 * 60), DateTimeUtils.millisOfDay(dt3, true)); + TestCase.assertEquals(123L + 1_000L * (45 + 23 * 60 + 11 * 60 * 60), DateTimeUtils.millisOfDay(dt3, false)); + TestCase.assertEquals(NULL_INT, DateTimeUtils.millisOfDay(null, true)); + TestCase.assertEquals(NULL_INT, DateTimeUtils.millisOfDay(null, false)); + + TestCase.assertEquals(123L + 1_000L * (45 + 23 * 60 + 11 * 60 * 60), DateTimeUtils.millisOfDay(lt)); + TestCase.assertEquals(NULL_INT, DateTimeUtils.millisOfDay((LocalTime) null)); // Test daylight savings time @@ -2909,20 +3057,35 @@ public void testMillisOfDay() { final Instant dstI11 = DateTimeUtils.plus(dstMid1, DateTimeUtils.HOUR); final ZonedDateTime dstZdt11 = DateTimeUtils.toZonedDateTime(dstI11, ZoneId.of("America/Denver")); TestCase.assertEquals(DateTimeUtils.HOUR / DateTimeUtils.MILLI, - DateTimeUtils.millisOfDay(dstI11, ZoneId.of("America/Denver"))); - TestCase.assertEquals(DateTimeUtils.HOUR / DateTimeUtils.MILLI, DateTimeUtils.millisOfDay(dstZdt11)); + DateTimeUtils.millisOfDay(dstI11, ZoneId.of("America/Denver"), true)); + TestCase.assertEquals(DateTimeUtils.HOUR / DateTimeUtils.MILLI, + DateTimeUtils.millisOfDay(dstI11, ZoneId.of("America/Denver"), false)); + TestCase.assertEquals(DateTimeUtils.HOUR / DateTimeUtils.MILLI, DateTimeUtils.millisOfDay(dstZdt11, true)); + TestCase.assertEquals(DateTimeUtils.HOUR / DateTimeUtils.MILLI, DateTimeUtils.millisOfDay(dstZdt11, false)); + TestCase.assertEquals(DateTimeUtils.HOUR / DateTimeUtils.MILLI, + DateTimeUtils.millisOfDay(dstZdt11.toLocalTime())); final Instant dstI12 = DateTimeUtils.plus(dstMid1, 2 * DateTimeUtils.HOUR); final ZonedDateTime dstZdt12 = DateTimeUtils.toZonedDateTime(dstI12, ZoneId.of("America/Denver")); + TestCase.assertEquals(3 * DateTimeUtils.HOUR / DateTimeUtils.MILLI, + DateTimeUtils.millisOfDay(dstI12, ZoneId.of("America/Denver"), true)); TestCase.assertEquals(2 * DateTimeUtils.HOUR / DateTimeUtils.MILLI, - DateTimeUtils.millisOfDay(dstI12, ZoneId.of("America/Denver"))); - TestCase.assertEquals(2 * DateTimeUtils.HOUR / DateTimeUtils.MILLI, DateTimeUtils.millisOfDay(dstZdt12)); + DateTimeUtils.millisOfDay(dstI12, ZoneId.of("America/Denver"), false)); + TestCase.assertEquals(3 * DateTimeUtils.HOUR / DateTimeUtils.MILLI, DateTimeUtils.millisOfDay(dstZdt12, true)); + TestCase.assertEquals(2 * DateTimeUtils.HOUR / DateTimeUtils.MILLI, DateTimeUtils.millisOfDay(dstZdt12, false)); + TestCase.assertEquals(3 * DateTimeUtils.HOUR / DateTimeUtils.MILLI, + DateTimeUtils.millisOfDay(dstZdt12.toLocalTime())); final Instant dstI13 = DateTimeUtils.plus(dstMid1, 3 * DateTimeUtils.HOUR); final ZonedDateTime dstZdt13 = DateTimeUtils.toZonedDateTime(dstI13, ZoneId.of("America/Denver")); + TestCase.assertEquals(4 * DateTimeUtils.HOUR / DateTimeUtils.MILLI, + DateTimeUtils.millisOfDay(dstI13, ZoneId.of("America/Denver"), true)); TestCase.assertEquals(3 * DateTimeUtils.HOUR / DateTimeUtils.MILLI, - DateTimeUtils.millisOfDay(dstI13, ZoneId.of("America/Denver"))); - TestCase.assertEquals(3 * DateTimeUtils.HOUR / DateTimeUtils.MILLI, DateTimeUtils.millisOfDay(dstZdt13)); + DateTimeUtils.millisOfDay(dstI13, ZoneId.of("America/Denver"), false)); + TestCase.assertEquals(4 * DateTimeUtils.HOUR / DateTimeUtils.MILLI, DateTimeUtils.millisOfDay(dstZdt13, true)); + TestCase.assertEquals(3 * DateTimeUtils.HOUR / DateTimeUtils.MILLI, DateTimeUtils.millisOfDay(dstZdt13, false)); + TestCase.assertEquals(4 * DateTimeUtils.HOUR / DateTimeUtils.MILLI, + DateTimeUtils.millisOfDay(dstZdt13.toLocalTime())); final Instant dstMid2 = DateTimeUtils.parseInstant("2023-11-05T00:00:00 America/Denver"); @@ -2930,20 +3093,35 @@ public void testMillisOfDay() { final Instant dstI21 = DateTimeUtils.plus(dstMid2, DateTimeUtils.HOUR); final ZonedDateTime dstZdt21 = DateTimeUtils.toZonedDateTime(dstI21, ZoneId.of("America/Denver")); TestCase.assertEquals(DateTimeUtils.HOUR / DateTimeUtils.MILLI, - DateTimeUtils.millisOfDay(dstI21, ZoneId.of("America/Denver"))); - TestCase.assertEquals(DateTimeUtils.HOUR / DateTimeUtils.MILLI, DateTimeUtils.millisOfDay(dstZdt21)); + DateTimeUtils.millisOfDay(dstI21, ZoneId.of("America/Denver"), true)); + TestCase.assertEquals(DateTimeUtils.HOUR / DateTimeUtils.MILLI, + DateTimeUtils.millisOfDay(dstI21, ZoneId.of("America/Denver"), false)); + TestCase.assertEquals(DateTimeUtils.HOUR / DateTimeUtils.MILLI, DateTimeUtils.millisOfDay(dstZdt21, true)); + TestCase.assertEquals(DateTimeUtils.HOUR / DateTimeUtils.MILLI, DateTimeUtils.millisOfDay(dstZdt21, false)); + TestCase.assertEquals(DateTimeUtils.HOUR / DateTimeUtils.MILLI, + DateTimeUtils.millisOfDay(dstZdt21.toLocalTime())); final Instant dstI22 = DateTimeUtils.plus(dstMid2, 2 * DateTimeUtils.HOUR); final ZonedDateTime dstZdt22 = DateTimeUtils.toZonedDateTime(dstI22, ZoneId.of("America/Denver")); + TestCase.assertEquals(DateTimeUtils.HOUR / DateTimeUtils.MILLI, + DateTimeUtils.millisOfDay(dstI22, ZoneId.of("America/Denver"), true)); TestCase.assertEquals(2 * DateTimeUtils.HOUR / DateTimeUtils.MILLI, - DateTimeUtils.millisOfDay(dstI22, ZoneId.of("America/Denver"))); - TestCase.assertEquals(2 * DateTimeUtils.HOUR / DateTimeUtils.MILLI, DateTimeUtils.millisOfDay(dstZdt22)); + DateTimeUtils.millisOfDay(dstI22, ZoneId.of("America/Denver"), false)); + TestCase.assertEquals(DateTimeUtils.HOUR / DateTimeUtils.MILLI, DateTimeUtils.millisOfDay(dstZdt22, true)); + TestCase.assertEquals(2 * DateTimeUtils.HOUR / DateTimeUtils.MILLI, DateTimeUtils.millisOfDay(dstZdt22, false)); + TestCase.assertEquals(DateTimeUtils.HOUR / DateTimeUtils.MILLI, + DateTimeUtils.millisOfDay(dstZdt22.toLocalTime())); final Instant dstI23 = DateTimeUtils.plus(dstMid2, 3 * DateTimeUtils.HOUR); final ZonedDateTime dstZdt23 = DateTimeUtils.toZonedDateTime(dstI23, ZoneId.of("America/Denver")); + TestCase.assertEquals(2 * DateTimeUtils.HOUR / DateTimeUtils.MILLI, + DateTimeUtils.millisOfDay(dstI23, ZoneId.of("America/Denver"), true)); TestCase.assertEquals(3 * DateTimeUtils.HOUR / DateTimeUtils.MILLI, - DateTimeUtils.millisOfDay(dstI23, ZoneId.of("America/Denver"))); - TestCase.assertEquals(3 * DateTimeUtils.HOUR / DateTimeUtils.MILLI, DateTimeUtils.millisOfDay(dstZdt23)); + DateTimeUtils.millisOfDay(dstI23, ZoneId.of("America/Denver"), false)); + TestCase.assertEquals(2 * DateTimeUtils.HOUR / DateTimeUtils.MILLI, DateTimeUtils.millisOfDay(dstZdt23, true)); + TestCase.assertEquals(3 * DateTimeUtils.HOUR / DateTimeUtils.MILLI, DateTimeUtils.millisOfDay(dstZdt23, false)); + TestCase.assertEquals(2 * DateTimeUtils.HOUR / DateTimeUtils.MILLI, + DateTimeUtils.millisOfDay(dstZdt23.toLocalTime())); } public void testMicrosOfSecond() {