-
Notifications
You must be signed in to change notification settings - Fork 2
Added DateTimeRange #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ALWAL12
wants to merge
1
commit into
master
Choose a base branch
from
feature/date-time-range
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| package ca.nexapp.core.dates; | ||
|
|
||
| import java.time.Duration; | ||
| import java.time.Instant; | ||
| import java.time.LocalDateTime; | ||
| import java.time.ZoneId; | ||
| import java.time.ZonedDateTime; | ||
| import java.util.Objects; | ||
| import java.util.Optional; | ||
|
|
||
| public class DateTimeRange { | ||
|
|
||
| private final ZonedDateTime from; | ||
| private final ZonedDateTime to; | ||
|
|
||
| private DateTimeRange(ZonedDateTime from, ZonedDateTime to) throws InvalidDateRangeException { | ||
| if (from.isAfter(to)) { | ||
| throw new InvalidDateRangeException(from, to); | ||
| } | ||
|
|
||
| this.from = from; | ||
| this.to = to; | ||
| } | ||
|
|
||
| public boolean isOverlapping(DateTimeRange other) { | ||
| return !(from.isAfter(other.to) || to.isBefore(other.from)); | ||
| } | ||
|
|
||
| public Optional<DateTimeRange> findOverlappingPeriod(DateTimeRange other) { | ||
| if (!isOverlapping(other)) { | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| ZonedDateTime startDate = from.compareTo(other.from) > 0 ? from : other.from; | ||
| ZonedDateTime endDate = to.compareTo(other.to) < 0 ? to : other.to; | ||
| DateTimeRange dateRange = DateTimeRange.of(startDate, endDate); | ||
| return Optional.of(dateRange); | ||
| } | ||
|
|
||
| public Duration asDuration() { | ||
| return Duration.between(from, to); | ||
| } | ||
|
|
||
| public ZoneId getZone() { | ||
| return from.getZone(); | ||
| } | ||
|
|
||
| public ZonedDateTime getFrom() { | ||
| return from; | ||
| } | ||
|
|
||
| public ZonedDateTime getTo() { | ||
| return to; | ||
| } | ||
|
|
||
| public boolean includes(Optional<ZonedDateTime> date) { | ||
| return date.map(this::includes).orElse(false); | ||
| } | ||
|
|
||
| public boolean includes(ZonedDateTime date) { | ||
| boolean after = date.isAfter(from) || date.isEqual(from); | ||
| boolean before = date.isBefore(to) || date.isEqual(to); | ||
| return before && after; | ||
| } | ||
|
|
||
| public static DateTimeRange of(Instant from, Instant to, ZoneId zoneId) { | ||
| ZonedDateTime fromDateTime = ZonedDateTime.ofInstant(from, zoneId); | ||
| ZonedDateTime toDateTime = ZonedDateTime.ofInstant(to, zoneId); | ||
| return new DateTimeRange(fromDateTime, toDateTime); | ||
| } | ||
|
|
||
| public static DateTimeRange of(LocalDateTime from, LocalDateTime to, ZoneId zoneId) { | ||
| ZonedDateTime zonedFrom = ZonedDateTime.of(from, zoneId); | ||
| ZonedDateTime zonedTo = ZonedDateTime.of(to, zoneId); | ||
| return new DateTimeRange(zonedFrom, zonedTo); | ||
| } | ||
|
|
||
| public static DateTimeRange of(ZonedDateTime from, ZonedDateTime to) { | ||
| if (!from.getZone().equals(to.getZone())) { | ||
| throw new InvalidDateRangeException(from, to); | ||
| } | ||
| return new DateTimeRange(from, to); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(from, to); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object obj) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's good practice to check for nullity first |
||
| if (!(obj instanceof DateTimeRange)) { | ||
| return false; | ||
| } | ||
| DateTimeRange dateTimeRange = (DateTimeRange) obj; | ||
| return Objects.equals(from, dateTimeRange.from) | ||
| && Objects.equals(to, dateTimeRange.to); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return String.format("%s => %s [%s]", from, to, getZone()); | ||
| } | ||
| } | ||
16 changes: 16 additions & 0 deletions
16
src/main/java/ca/nexapp/core/dates/InvalidDateRangeException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package ca.nexapp.core.dates; | ||
|
|
||
| import java.time.ZonedDateTime; | ||
|
|
||
| public class InvalidDateRangeException extends RuntimeException { | ||
|
|
||
| private static final long serialVersionUID = 728483949226608247L; | ||
|
|
||
| public final ZonedDateTime from; | ||
| public final ZonedDateTime to; | ||
|
|
||
| public InvalidDateRangeException(ZonedDateTime from, ZonedDateTime to) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| this.from = from; | ||
| this.to = to; | ||
| } | ||
| } | ||
243 changes: 243 additions & 0 deletions
243
src/test/java/ca/nexapp/core/dates/DateTimeRangeTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,243 @@ | ||
| package ca.nexapp.core.dates; | ||
|
|
||
| import static com.google.common.truth.Truth.assertThat; | ||
|
|
||
| import java.time.Duration; | ||
| import java.time.Instant; | ||
| import java.time.LocalDateTime; | ||
| import java.time.Month; | ||
| import java.time.ZoneId; | ||
| import java.time.ZoneOffset; | ||
| import java.time.ZonedDateTime; | ||
| import java.util.Optional; | ||
|
|
||
| import org.junit.Test; | ||
|
|
||
| public class DateTimeRangeTest { | ||
|
|
||
| private static final ZoneId A_ZONE = ZoneOffset.UTC; | ||
|
|
||
| private static final ZonedDateTime NOW = ZonedDateTime.now(A_ZONE); | ||
| private static final ZonedDateTime EARLIER = NOW.minusDays(1); | ||
| private static final ZonedDateTime LATER = NOW.plusDays(1); | ||
|
|
||
| @Test | ||
| public void canCreateFromTwoZonedDateTime() { | ||
| DateTimeRange range = DateTimeRange.of(NOW, LATER); | ||
|
|
||
| assertThat(NOW).isEqualTo(range.getFrom()); | ||
| assertThat(LATER).isEqualTo(range.getTo()); | ||
| } | ||
|
|
||
| @Test | ||
| public void canCreateARangeFromTheSameDateTime() { | ||
| DateTimeRange range = DateTimeRange.of(NOW, NOW); | ||
|
|
||
| assertThat(NOW).isEqualTo(range.getFrom()); | ||
| assertThat(NOW).isEqualTo(range.getTo()); | ||
| } | ||
|
|
||
| @Test | ||
| public void canCreateARangeFromTwoInstant() { | ||
| Instant fromInstant = NOW.toInstant(); | ||
| Instant toInstant = LATER.toInstant(); | ||
|
|
||
| DateTimeRange range = DateTimeRange.of(fromInstant, toInstant, A_ZONE); | ||
|
|
||
| assertThat(NOW.withZoneSameInstant(A_ZONE)).isEqualTo(range.getFrom()); | ||
| assertThat(LATER.withZoneSameInstant(A_ZONE)).isEqualTo(range.getTo()); | ||
| } | ||
|
|
||
| @Test | ||
| public void canCreateARangeFromTwoLocalDateTime() { | ||
| LocalDateTime from = NOW.toLocalDateTime(); | ||
| LocalDateTime to = LATER.toLocalDateTime(); | ||
|
|
||
| DateTimeRange range = DateTimeRange.of(from, to, A_ZONE); | ||
|
|
||
| assertThat(NOW).isEqualTo(range.getFrom()); | ||
| assertThat(LATER).isEqualTo(range.getTo()); | ||
| } | ||
|
|
||
| @Test(expected = InvalidDateRangeException.class) | ||
| public void cannotCreateARangeThatGoesToThePast() throws InvalidDateRangeException { | ||
| DateTimeRange.of(NOW, EARLIER); | ||
| } | ||
|
|
||
| @Test(expected = InvalidDateRangeException.class) | ||
| public void cannotCreateARangeFromDifferentZones() { | ||
| ZonedDateTime montreal = ZonedDateTime.now(ZoneId.of("America/Montreal")); | ||
| ZonedDateTime vancouver = ZonedDateTime.now(ZoneId.of("America/Vancouver")); | ||
|
|
||
| DateTimeRange.of(montreal, vancouver); | ||
| } | ||
|
|
||
| @Test | ||
| public void canRetrieveTheZone() { | ||
| ZonedDateTime from = ZonedDateTime.now(A_ZONE); | ||
| ZonedDateTime to = from.plusDays(555); | ||
| DateTimeRange range = DateTimeRange.of(from, to); | ||
|
|
||
| assertThat(range.getZone()).isEqualTo(A_ZONE); | ||
| } | ||
|
|
||
| @Test | ||
| public void canRetrieveTheDuration() { | ||
| ZonedDateTime from = ZonedDateTime.now(A_ZONE); | ||
| ZonedDateTime to = from.plusMinutes(50); | ||
|
|
||
| DateTimeRange dateTimeRange = DateTimeRange.of(from, to); | ||
|
|
||
| Duration expected = Duration.ofMinutes(50); | ||
| assertThat(dateTimeRange.asDuration()).isEqualTo(expected); | ||
| } | ||
|
|
||
| private static final LocalDateTime MARCH_1ST = LocalDateTime.of(2017, Month.MARCH, 1, 0, 0); | ||
| private static final LocalDateTime MARCH_2ND = LocalDateTime.of(2017, Month.MARCH, 2, 0, 0); | ||
| private static final LocalDateTime MARCH_3RD = LocalDateTime.of(2017, Month.MARCH, 3, 0, 0); | ||
| private static final LocalDateTime MARCH_5TH = LocalDateTime.of(2017, Month.MARCH, 5, 0, 0); | ||
| private static final LocalDateTime MARCH_6TH = LocalDateTime.of(2017, Month.MARCH, 6, 0, 0); | ||
| private static final LocalDateTime APRIL_1ST = LocalDateTime.of(2017, Month.APRIL, 1, 0, 0); | ||
| private static final LocalDateTime APRIL_2ND = LocalDateTime.of(2017, Month.APRIL, 2, 0, 0); | ||
| private static final LocalDateTime MAY_1ST = LocalDateTime.of(2017, Month.MAY, 1, 0, 0); | ||
| private static final LocalDateTime JANUARY_1ST = LocalDateTime.of(2017, Month.JANUARY, 1, 0, 0); | ||
| private static final LocalDateTime FEBRUARY_2ND = LocalDateTime.of(2017, Month.FEBRUARY, 2, 0, 0); | ||
|
|
||
| @Test | ||
| public void givenNotOverlappingDateTime_ShouldNotOverlap() { | ||
| DateTimeRange range1 = DateTimeRange.of(MARCH_1ST, MARCH_2ND, A_ZONE); | ||
| DateTimeRange range2 = DateTimeRange.of(APRIL_1ST, APRIL_2ND, A_ZONE); | ||
|
|
||
| assertThat(range1.isOverlapping(range2)).isFalse(); | ||
| } | ||
|
|
||
| @Test | ||
| public void givenATimeRangeOverflowingAtTheEnd_ShouldOverlap() { | ||
| DateTimeRange range1 = DateTimeRange.of(MARCH_1ST, MARCH_2ND, A_ZONE); | ||
| DateTimeRange range2 = DateTimeRange.of(MARCH_2ND, APRIL_2ND, A_ZONE); | ||
|
|
||
| assertThat(range1.isOverlapping(range2)).isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| public void givenATimeRangeOverflowingAtTheBeginning_ShouldOverlap() { | ||
| DateTimeRange range1 = DateTimeRange.of(MARCH_1ST, MARCH_2ND, A_ZONE); | ||
| DateTimeRange range2 = DateTimeRange.of(FEBRUARY_2ND, MARCH_1ST, A_ZONE); | ||
|
|
||
| assertThat(range1.isOverlapping(range2)).isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| public void givenAContainedRangeInsideAnotherRange_ShouldOverlap() { | ||
| DateTimeRange range = DateTimeRange.of(MARCH_1ST, APRIL_2ND, A_ZONE); | ||
| DateTimeRange contained = DateTimeRange.of(MARCH_5TH, MARCH_6TH, A_ZONE); | ||
|
|
||
| assertThat(range.isOverlapping(contained)).isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| public void overlappingDateRange_ShouldBeCloseAtTheBeginning() { | ||
| DateTimeRange range1 = DateTimeRange.of(MARCH_1ST, APRIL_2ND, A_ZONE); | ||
| DateTimeRange range2 = DateTimeRange.of(FEBRUARY_2ND, MARCH_1ST, A_ZONE); | ||
|
|
||
| assertThat(range1.isOverlapping(range2)).isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| public void overlappingDateRange_ShouldBeCloseAtTheEnd() { | ||
| DateTimeRange range1 = DateTimeRange.of(MARCH_1ST, APRIL_1ST, A_ZONE); | ||
| DateTimeRange range2 = DateTimeRange.of(APRIL_1ST, MAY_1ST, A_ZONE); | ||
|
|
||
| assertThat(range1.isOverlapping(range2)).isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| public void cannotFindOverlappingPeriodOfNonOverlappingRanges() { | ||
| DateTimeRange range1 = DateTimeRange.of(MARCH_1ST, MARCH_2ND, A_ZONE); | ||
| DateTimeRange range2 = DateTimeRange.of(APRIL_1ST, APRIL_2ND, A_ZONE); | ||
|
|
||
| Optional<DateTimeRange> overlap = range1.findOverlappingPeriod(range2); | ||
|
|
||
| assertThat(overlap).isEqualTo(Optional.empty()); | ||
| } | ||
|
|
||
| @Test | ||
| public void givenOverlappingRangesAtTheEnd_CanRetrieveTheOverlappingPeriod() { | ||
| DateTimeRange range1 = DateTimeRange.of(MARCH_1ST, MARCH_3RD, A_ZONE); | ||
| DateTimeRange range2 = DateTimeRange.of(MARCH_2ND, APRIL_2ND, A_ZONE); | ||
|
|
||
| Optional<DateTimeRange> overlap = range1.findOverlappingPeriod(range2); | ||
|
|
||
| DateTimeRange expected = DateTimeRange.of(MARCH_2ND, MARCH_3RD, A_ZONE); | ||
| assertThat(overlap).isEqualTo(Optional.of(expected)); | ||
| } | ||
|
|
||
| @Test | ||
| public void givenOverlappingRangesAtTheBeginning_CanRetrieveTheOverlappingPeriod() { | ||
| DateTimeRange range1 = DateTimeRange.of(MARCH_1ST, MARCH_5TH, A_ZONE); | ||
| DateTimeRange range2 = DateTimeRange.of(FEBRUARY_2ND, MARCH_2ND, A_ZONE); | ||
|
|
||
| Optional<DateTimeRange> overlap = range1.findOverlappingPeriod(range2); | ||
|
|
||
| DateTimeRange expected = DateTimeRange.of(MARCH_1ST, MARCH_2ND, A_ZONE); | ||
| assertThat(overlap).isEqualTo(Optional.of(expected)); | ||
| } | ||
|
|
||
| @Test | ||
| public void givenOverlappingInside_TheOverlappingPeriodIsTheInnerDate() { | ||
| DateTimeRange range = DateTimeRange.of(MARCH_1ST, APRIL_2ND, A_ZONE); | ||
| DateTimeRange innerRange = DateTimeRange.of(MARCH_5TH, MARCH_6TH, A_ZONE); | ||
|
|
||
| Optional<DateTimeRange> overlap = range.findOverlappingPeriod(innerRange); | ||
|
|
||
| assertThat(overlap).isEqualTo(Optional.of(innerRange)); | ||
| } | ||
|
|
||
| @Test | ||
| public void givenADateAfterTheDateTimeRange_ShouldNotBeIncluded() { | ||
| DateTimeRange range = DateTimeRange.of(JANUARY_1ST, FEBRUARY_2ND, A_ZONE); | ||
| ZonedDateTime date = ZonedDateTime.of(MARCH_3RD, A_ZONE); | ||
|
|
||
| assertThat(range.includes(date)).isFalse(); | ||
| } | ||
|
|
||
| @Test | ||
| public void givenADateAtTheEndOfTheRange_ShouldBeIncluded() { | ||
| DateTimeRange range = DateTimeRange.of(JANUARY_1ST, FEBRUARY_2ND, A_ZONE); | ||
| ZonedDateTime date = FEBRUARY_2ND.atZone(A_ZONE); | ||
|
|
||
| assertThat(range.includes(date)).isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| public void givenADateBeforeTheRange_ShouldBeIncluded() { | ||
| DateTimeRange range = DateTimeRange.of(JANUARY_1ST, FEBRUARY_2ND, A_ZONE); | ||
| ZonedDateTime date = JANUARY_1ST.minusYears(1).atZone(A_ZONE); | ||
|
|
||
| assertThat(range.includes(date)).isFalse(); | ||
| } | ||
|
|
||
| @Test | ||
| public void givenADateAtTheStartOfADateTimeRange_ShouldBeIncluded() { | ||
| DateTimeRange range = DateTimeRange.of(JANUARY_1ST, FEBRUARY_2ND, A_ZONE); | ||
| ZonedDateTime date = ZonedDateTime.of(JANUARY_1ST, A_ZONE); | ||
|
|
||
| assertThat(range.includes(date)).isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| public void givenADateBetweenTheStartAndTheEndOfATimeRange_ShouldBeIncluded() { | ||
| DateTimeRange range = DateTimeRange.of(MARCH_1ST, MARCH_3RD, A_ZONE); | ||
| ZonedDateTime date = ZonedDateTime.of(MARCH_2ND, A_ZONE); | ||
|
|
||
| assertThat(range.includes(date)).isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| public void givenNoDate_ShouldNotBeIncluded() { | ||
| DateTimeRange range = DateTimeRange.of(JANUARY_1ST, FEBRUARY_2ND, A_ZONE); | ||
| Optional<ZonedDateTime> noDate = Optional.empty(); | ||
|
|
||
| assertThat(range.includes(noDate)).isFalse(); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
which situations do you see this method be used? wouldn't it be better to let the callee do that by themself?