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
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,12 @@ public static long applyCalendarOffset(long milliseconds, Calendar calendar) {
* @return a {@link Timestamp} object representing the given Epoch millis
*/
public static Timestamp getTimestampValue(long millisWithCalendar) {
long milliseconds = millisWithCalendar;
if (milliseconds < 0) {
// LocalTime#ofNanoDay only accepts positive values
milliseconds -= ((milliseconds / MILLIS_PER_DAY) - 1) * MILLIS_PER_DAY;
}

// Millis are negative before 1970, where only floor semantics keep the epoch day
// and the time-of-day remainder on the same day (and the remainder non-negative).
return Timestamp.valueOf(
LocalDateTime.of(
LocalDate.ofEpochDay(millisWithCalendar / MILLIS_PER_DAY),
LocalTime.ofNanoOfDay(TimeUnit.MILLISECONDS.toNanos(milliseconds % MILLIS_PER_DAY))));
LocalDate.ofEpochDay(Math.floorDiv(millisWithCalendar, MILLIS_PER_DAY)),
LocalTime.ofNanoOfDay(
TimeUnit.MILLISECONDS.toNanos(Math.floorMod(millisWithCalendar, MILLIS_PER_DAY)))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,15 @@ public void testShouldGetTimestampNegative() {

assertThat(expected, is(actual));
}

@Test
public void testShouldGetTimestampNegativeNotAlignedToDay() {
final long epochMilli = negativeEpochMilli + 3600000L; // 1950-06-01 01:00:00 UTC
final Instant instant = Instant.ofEpochMilli(epochMilli);

final Timestamp expected = Timestamp.from(instant);
final Timestamp actual = DateTimeUtils.getTimestampValue(epochMilli);

assertThat(expected, is(actual));
}
}
Loading