Skip to content

Commit 4e45388

Browse files
committed
GH-359 - Add Now.instant().
1 parent 20a7474 commit 4e45388

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

spring-modulith-moments/src/main/java/org/springframework/modulith/moments/support/Moments.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,7 @@ Moments shiftBy(Duration duration) {
129129
*/
130130
@Override
131131
public LocalDateTime now() {
132-
133-
Instant instant = clock.instant().plus(shift);
134-
135-
return LocalDateTime.ofInstant(instant, properties.getZoneId());
132+
return LocalDateTime.ofInstant(instant(), properties.getZoneId());
136133
}
137134

138135
/*
@@ -144,6 +141,15 @@ public LocalDate today() {
144141
return now().toLocalDate();
145142
}
146143

144+
/*
145+
* (non-Javadoc)
146+
* @see org.springframework.modulith.moments.support.Now#instant()
147+
*/
148+
@Override
149+
public Instant instant() {
150+
return clock.instant().plus(shift);
151+
}
152+
147153
private void emitEventsFor(LocalDateTime time) {
148154
events.publishEvent(HourHasPassed.of(time.truncatedTo(ChronoUnit.HOURS)));
149155
}

spring-modulith-moments/src/main/java/org/springframework/modulith/moments/support/Now.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.modulith.moments.support;
1717

18+
import java.time.Instant;
1819
import java.time.LocalDate;
1920
import java.time.LocalDateTime;
2021

@@ -41,4 +42,11 @@ public interface Now {
4142
* @return will never be {@literal null}.
4243
*/
4344
LocalDate today();
45+
46+
/**
47+
* Returns the current Instant.
48+
*
49+
* @return will never be {@literal null}.
50+
*/
51+
Instant instant();
4452
}

spring-modulith-moments/src/test/java/org/springframework/modulith/moments/support/MomentsUnitTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.time.LocalDate;
2525
import java.time.LocalDateTime;
2626
import java.time.Year;
27+
import java.time.ZoneOffset;
2728
import java.time.temporal.ChronoUnit;
2829
import java.time.temporal.WeekFields;
2930
import java.util.Locale;
@@ -173,6 +174,15 @@ void shiftsTimeForDuration() {
173174
assertThat(before.plus(duration)).isCloseTo(after, within(200, ChronoUnit.MILLIS));
174175
}
175176

177+
@Test // GH-359
178+
void returnsInstant() {
179+
180+
var instant = hourly.instant();
181+
var now = hourly.now();
182+
183+
assertThat(LocalDateTime.ofInstant(instant, ZoneOffset.UTC)).isCloseTo(now, within(50, ChronoUnit.MILLIS));
184+
}
185+
176186
private Duration getNumberOfDaysForThreeMonth(LocalDate date) {
177187

178188
int days = 0;

0 commit comments

Comments
 (0)