From 4e453885ee6cf2ae703e8e8e8d7553aa210d47af Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Fri, 3 Nov 2023 10:42:30 +0100 Subject: [PATCH] GH-359 - Add Now.instant(). --- .../modulith/moments/support/Moments.java | 14 ++++++++++---- .../modulith/moments/support/Now.java | 8 ++++++++ .../modulith/moments/support/MomentsUnitTests.java | 10 ++++++++++ 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/spring-modulith-moments/src/main/java/org/springframework/modulith/moments/support/Moments.java b/spring-modulith-moments/src/main/java/org/springframework/modulith/moments/support/Moments.java index 27dde30b3..9cf3583f6 100644 --- a/spring-modulith-moments/src/main/java/org/springframework/modulith/moments/support/Moments.java +++ b/spring-modulith-moments/src/main/java/org/springframework/modulith/moments/support/Moments.java @@ -129,10 +129,7 @@ Moments shiftBy(Duration duration) { */ @Override public LocalDateTime now() { - - Instant instant = clock.instant().plus(shift); - - return LocalDateTime.ofInstant(instant, properties.getZoneId()); + return LocalDateTime.ofInstant(instant(), properties.getZoneId()); } /* @@ -144,6 +141,15 @@ public LocalDate today() { return now().toLocalDate(); } + /* + * (non-Javadoc) + * @see org.springframework.modulith.moments.support.Now#instant() + */ + @Override + public Instant instant() { + return clock.instant().plus(shift); + } + private void emitEventsFor(LocalDateTime time) { events.publishEvent(HourHasPassed.of(time.truncatedTo(ChronoUnit.HOURS))); } diff --git a/spring-modulith-moments/src/main/java/org/springframework/modulith/moments/support/Now.java b/spring-modulith-moments/src/main/java/org/springframework/modulith/moments/support/Now.java index 1b461e65b..3b8c26783 100644 --- a/spring-modulith-moments/src/main/java/org/springframework/modulith/moments/support/Now.java +++ b/spring-modulith-moments/src/main/java/org/springframework/modulith/moments/support/Now.java @@ -15,6 +15,7 @@ */ package org.springframework.modulith.moments.support; +import java.time.Instant; import java.time.LocalDate; import java.time.LocalDateTime; @@ -41,4 +42,11 @@ public interface Now { * @return will never be {@literal null}. */ LocalDate today(); + + /** + * Returns the current Instant. + * + * @return will never be {@literal null}. + */ + Instant instant(); } diff --git a/spring-modulith-moments/src/test/java/org/springframework/modulith/moments/support/MomentsUnitTests.java b/spring-modulith-moments/src/test/java/org/springframework/modulith/moments/support/MomentsUnitTests.java index 202bbe572..1b1819df6 100644 --- a/spring-modulith-moments/src/test/java/org/springframework/modulith/moments/support/MomentsUnitTests.java +++ b/spring-modulith-moments/src/test/java/org/springframework/modulith/moments/support/MomentsUnitTests.java @@ -24,6 +24,7 @@ import java.time.LocalDate; import java.time.LocalDateTime; import java.time.Year; +import java.time.ZoneOffset; import java.time.temporal.ChronoUnit; import java.time.temporal.WeekFields; import java.util.Locale; @@ -173,6 +174,15 @@ void shiftsTimeForDuration() { assertThat(before.plus(duration)).isCloseTo(after, within(200, ChronoUnit.MILLIS)); } + @Test // GH-359 + void returnsInstant() { + + var instant = hourly.instant(); + var now = hourly.now(); + + assertThat(LocalDateTime.ofInstant(instant, ZoneOffset.UTC)).isCloseTo(now, within(50, ChronoUnit.MILLIS)); + } + private Duration getNumberOfDaysForThreeMonth(LocalDate date) { int days = 0;