Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save daicorrea-tw/b0813b9293e83a1b540ff21b092bbcd7 to your computer and use it in GitHub Desktop.
Save daicorrea-tw/b0813b9293e83a1b540ff21b092bbcd7 to your computer and use it in GitHub Desktop.
package uk.tw.energy.domain;
import java.time.*;
import java.time.temporal.ChronoUnit;
public class ElectricityReadingInterval {
private final Instant start;
private final Instant end;
final static int A_DAY_IN_SECONDS = 60 * 60 * 24;
final static int A_WEEK_BEFORE_IN_SECONDS = A_DAY_IN_SECONDS * 7;
private ElectricityReadingInterval(Instant start, Instant end) {
this.start = start;
this.end = end;
}
public Instant getStart() {
return start;
}
public Instant getEnd() {
return end;
}
public static ElectricityReadingInterval previousWeekInterval() {
return previousWeekInterval(Clock.systemUTC());
}
public static ElectricityReadingInterval previousWeekInterval(Clock clock) {
Instant now = Instant.now(clock);
Instant sunday = ElectricityReadingInterval.getPreviousSundayOf(now);
return new ElectricityReadingInterval(sunday.minusSeconds(A_WEEK_BEFORE_IN_SECONDS), sunday);
}
private static Instant getPreviousSundayOf(Instant instant) {
Instant sunday = instant.truncatedTo(ChronoUnit.DAYS);
while (!DayOfWeek.from(sunday.atZone(ZoneOffset.UTC)).equals(DayOfWeek.SUNDAY)) {
sunday = sunday.minusSeconds(A_DAY_IN_SECONDS);
}
return sunday;
}
}
package uk.tw.energy.domain;
import org.junit.jupiter.api.Test;
import java.time.Clock;
import java.time.Instant;
import java.time.ZoneOffset;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
class ElectricityReadingIntervalTest {
@Test
public void shouldReturnTheCurrentWeekStartAsIntervalEnding() {
Clock clock = Clock.fixed(
Instant.parse("2021-09-25T10:00:00Z"),
ZoneOffset.UTC
);
ElectricityReadingInterval electricityReadingInterval = ElectricityReadingInterval.previousWeekInterval(clock);
assertThat(electricityReadingInterval.getEnd())
.isEqualTo(Instant.parse("2021-09-19T00:00:00Z"));
}
@Test
public void shouldReturnThePreviousWeekStartAsIntervalStart() {
Clock clock = Clock.fixed(
Instant.parse("2021-09-25T10:00:00Z"),
ZoneOffset.UTC
);
ElectricityReadingInterval electricityReadingInterval = ElectricityReadingInterval.previousWeekInterval(clock);
assertThat(electricityReadingInterval.getStart())
.isEqualTo(Instant.parse("2021-09-12T00:00:00Z"));
}
@Test
public void whenClockIsNotProvidedItTakesTheDefaultOne() {
final int A_DAY_IN_SECONDS = 60 * 60 * 24;
final int A_WEEK_BEFORE_IN_SECONDS = A_DAY_IN_SECONDS * 7;
ElectricityReadingInterval electricityReadingInterval = ElectricityReadingInterval.previousWeekInterval();
assertThat(electricityReadingInterval.getStart()).isBefore(Instant.now());
assertThat(electricityReadingInterval.getEnd()).isBefore(Instant.now());
Instant aWeekBefore = Instant.now().minusSeconds(A_WEEK_BEFORE_IN_SECONDS);
Instant twoWeekBefore = aWeekBefore.minusSeconds(A_WEEK_BEFORE_IN_SECONDS);
assertThat(electricityReadingInterval.getEnd()).isAfter(aWeekBefore);
assertThat(electricityReadingInterval.getStart()).isBefore(aWeekBefore);
assertThat(electricityReadingInterval.getStart()).isAfter(twoWeekBefore);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment