Skip to content

Commit 4a91ad4

Browse files
committed
GH-335 - Moments now implements Now.
1 parent 9b79e09 commit 4a91ad4

File tree

2 files changed

+60
-2
lines changed
  • spring-modulith-moments/src/main/java/org/springframework/modulith/moments/support

2 files changed

+60
-2
lines changed

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
*
4444
* @author Oliver Drotbohm
4545
*/
46-
public class Moments {
46+
public class Moments implements Now {
4747

4848
private static final MonthDay DEC_31ST = MonthDay.of(Month.DECEMBER, 31);
4949

@@ -123,13 +123,27 @@ Moments shiftBy(Duration duration) {
123123
return this;
124124
}
125125

126-
LocalDateTime now() {
126+
/*
127+
* (non-Javadoc)
128+
* @see org.springframework.modulith.moments.support.Now#now()
129+
*/
130+
@Override
131+
public LocalDateTime now() {
127132

128133
Instant instant = clock.instant().plus(shift);
129134

130135
return LocalDateTime.ofInstant(instant, properties.getZoneId());
131136
}
132137

138+
/*
139+
* (non-Javadoc)
140+
* @see org.springframework.modulith.moments.support.Now#today()
141+
*/
142+
@Override
143+
public LocalDate today() {
144+
return now().toLocalDate();
145+
}
146+
133147
private void emitEventsFor(LocalDateTime time) {
134148
events.publishEvent(HourHasPassed.of(time.truncatedTo(ChronoUnit.HOURS)));
135149
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.modulith.moments.support;
17+
18+
import java.time.LocalDate;
19+
import java.time.LocalDateTime;
20+
21+
/**
22+
* An abstraction of the current point in time and today. Useful if application code needs to look them up, but you need
23+
* a mechanism to shift the time for testing local development purposes.
24+
*
25+
* @author Oliver Drotbohm
26+
* @since 1.1
27+
* @see TimeMachine
28+
*/
29+
public interface Now {
30+
31+
/**
32+
* Returns the current point in time.
33+
*
34+
* @return will never be {@literal null}.
35+
*/
36+
LocalDateTime now();
37+
38+
/**
39+
* Returns the current date.
40+
*
41+
* @return will never be {@literal null}.
42+
*/
43+
LocalDate today();
44+
}

0 commit comments

Comments
 (0)