Skip to content

Commit

Permalink
Fix broken calendar unit tests over midnight.
Browse files Browse the repository at this point in the history
Resolves #5024.
  • Loading branch information
chipkent committed Jan 10, 2024
1 parent eabf2f3 commit 64076f9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
13 changes: 9 additions & 4 deletions engine/time/src/main/java/io/deephaven/time/DateTimeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,7 @@ private CachedCurrentDate(@NotNull final ZoneId timeZone) {
synchronized void update(final long currentTimeMillis) {
date = toLocalDate(epochMillisToInstant(currentTimeMillis), timeZone);
str = formatDate(date);
valueExpirationTimeMillis =
epochMillis(atMidnight(epochMillisToZonedDateTime(currentTimeMillis, timeZone)).plusDays(1));
valueExpirationTimeMillis = epochMillis(date.plusDays(1).atStartOfDay(timeZone));
}
}

Expand All @@ -450,6 +449,10 @@ public ZoneId getKey(final CACHED_DATE_TYPE cachedDate) {
private static final KeyedObjectHashMap<ZoneId, CachedCurrentDate> cachedCurrentDates =
new KeyedObjectHashMap<>(new CachedDateKey<>());

private static CachedCurrentDate getCachedCurrentDate(@NotNull final ZoneId timeZone) {
return cachedCurrentDates.putIfAbsent(timeZone, CachedCurrentDate::new);
}

/**
* Provides the current date string according to the {@link #currentClock() current clock}. Under most
* circumstances, this method will return the date according to current system time, but during replay simulations,
Expand All @@ -468,7 +471,7 @@ public static String today(@Nullable final ZoneId timeZone) {
return null;
}

return cachedCurrentDates.putIfAbsent(timeZone, CachedCurrentDate::new).getStr();
return getCachedCurrentDate(timeZone).getStr();
}

/**
Expand All @@ -485,6 +488,7 @@ public static String today(@Nullable final ZoneId timeZone) {
@ScriptApi
@NotNull
public static String today() {
//noinspection ConstantConditions
return today(DateTimeUtils.timeZone());
}

Expand All @@ -506,7 +510,7 @@ public static LocalDate todayLocalDate(@Nullable final ZoneId timeZone) {
return null;
}

return cachedCurrentDates.putIfAbsent(timeZone, CachedCurrentDate::new).getLocalDate();
return getCachedCurrentDate(timeZone).getLocalDate();
}

/**
Expand All @@ -523,6 +527,7 @@ public static LocalDate todayLocalDate(@Nullable final ZoneId timeZone) {
@ScriptApi
@NotNull
public static LocalDate todayLocalDate() {
//noinspection ConstantConditions
return todayLocalDate(DateTimeUtils.timeZone());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1503,29 +1503,29 @@ public void testMinusNonBusinessDays() {
}

public void testFutureBusinessDate() {
assertEquals(bCalendar.plusBusinessDays(DateTimeUtils.todayLocalDate(), 3), bCalendar.futureBusinessDate(3));
assertEquals(bCalendar.plusBusinessDays(DateTimeUtils.todayLocalDate(), -3), bCalendar.futureBusinessDate(-3));
assertEquals(bCalendar.plusBusinessDays(DateTimeUtils.todayLocalDate(bCalendar.timeZone()), 3), bCalendar.futureBusinessDate(3));
assertEquals(bCalendar.plusBusinessDays(DateTimeUtils.todayLocalDate(bCalendar.timeZone()), -3), bCalendar.futureBusinessDate(-3));
assertNull(bCalendar.futureBusinessDate(NULL_INT));
}

public void testPastBusinessDate() {
assertEquals(bCalendar.minusBusinessDays(DateTimeUtils.todayLocalDate(), 3), bCalendar.pastBusinessDate(3));
assertEquals(bCalendar.minusBusinessDays(DateTimeUtils.todayLocalDate(), -3), bCalendar.pastBusinessDate(-3));
assertEquals(bCalendar.minusBusinessDays(DateTimeUtils.todayLocalDate(bCalendar.timeZone()), 3), bCalendar.pastBusinessDate(3));
assertEquals(bCalendar.minusBusinessDays(DateTimeUtils.todayLocalDate(bCalendar.timeZone()), -3), bCalendar.pastBusinessDate(-3));
assertNull(bCalendar.pastBusinessDate(NULL_INT));
}

public void testFutureNonBusinessDate() {
assertEquals(bCalendar.plusNonBusinessDays(DateTimeUtils.todayLocalDate(), 3),
assertEquals(bCalendar.plusNonBusinessDays(DateTimeUtils.todayLocalDate(bCalendar.timeZone()), 3),
bCalendar.futureNonBusinessDate(3));
assertEquals(bCalendar.plusNonBusinessDays(DateTimeUtils.todayLocalDate(), -3),
assertEquals(bCalendar.plusNonBusinessDays(DateTimeUtils.todayLocalDate(bCalendar.timeZone()), -3),
bCalendar.futureNonBusinessDate(-3));
assertNull(bCalendar.futureNonBusinessDate(NULL_INT));
}

public void testPastNonBusinessDate() {
assertEquals(bCalendar.minusNonBusinessDays(DateTimeUtils.todayLocalDate(), 3),
assertEquals(bCalendar.minusNonBusinessDays(DateTimeUtils.todayLocalDate(bCalendar.timeZone()), 3),
bCalendar.pastNonBusinessDate(3));
assertEquals(bCalendar.minusNonBusinessDays(DateTimeUtils.todayLocalDate(), -3),
assertEquals(bCalendar.minusNonBusinessDays(DateTimeUtils.todayLocalDate(bCalendar.timeZone()), -3),
bCalendar.pastNonBusinessDate(-3));
assertNull(bCalendar.pastNonBusinessDate(NULL_INT));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,18 +159,18 @@ public void testMinusDays() {
}

public void testCurrentDate() {
assertEquals(DateTimeUtils.todayLocalDate(), calendar.calendarDate());
assertEquals(DateTimeUtils.todayLocalDate(calendar.timeZone()), calendar.calendarDate());
}

public void testFutureDate() {
assertEquals(calendar.plusDays(DateTimeUtils.todayLocalDate(), 3), calendar.futureDate(3));
assertEquals(calendar.plusDays(DateTimeUtils.todayLocalDate(), -3), calendar.futureDate(-3));
assertEquals(calendar.plusDays(DateTimeUtils.todayLocalDate(calendar.timeZone()), 3), calendar.futureDate(3));
assertEquals(calendar.plusDays(DateTimeUtils.todayLocalDate(calendar.timeZone()), -3), calendar.futureDate(-3));
assertNull(calendar.futureDate(NULL_INT));
}

public void testPastDate() {
assertEquals(calendar.minusDays(DateTimeUtils.todayLocalDate(), 3), calendar.pastDate(3));
assertEquals(calendar.minusDays(DateTimeUtils.todayLocalDate(), -3), calendar.pastDate(-3));
assertEquals(calendar.minusDays(DateTimeUtils.todayLocalDate(calendar.timeZone()), 3), calendar.pastDate(3));
assertEquals(calendar.minusDays(DateTimeUtils.todayLocalDate(calendar.timeZone()), -3), calendar.pastDate(-3));
assertNull(calendar.pastDate(NULL_INT));
}

Expand Down

0 comments on commit 64076f9

Please sign in to comment.