Skip to content

Commit

Permalink
GH-359 - Add Now.instant().
Browse files Browse the repository at this point in the history
  • Loading branch information
odrotbohm committed Nov 3, 2023
1 parent 20a7474 commit 4e45388
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/*
Expand All @@ -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)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.springframework.modulith.moments.support;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;

Expand All @@ -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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 4e45388

Please sign in to comment.