Skip to content

Commit

Permalink
Extend formats Parsed by Interval
Browse files Browse the repository at this point in the history
Allow years, months, weeks and days via `PeriodDuration`
See #70
  • Loading branch information
jodastephen committed Apr 2, 2017
1 parent 9a9622c commit 3f2bebf
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
4 changes: 4 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
Add PeriodDuration, combining Period and Duration.
See #74.
</action>
<action dev="jodastephen" type="add" >
Extend formats Parsed by Interval, allowing years, months, weeks and days.
See #70.
</action>
<action dev="jodastephen" type="add" >
Add Seconds temporal amount class.
See #73.
Expand Down
21 changes: 10 additions & 11 deletions src/main/java/org/threeten/extra/Interval.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,11 @@ public static Interval of(Instant startInclusive, Duration duration) {
* <li>a representations of an {@link OffsetDateTime}, followed by a forward slash,
* followed by a representation of a {@link OffsetDateTime}
* <li>a representation of an {@link OffsetDateTime}, followed by a forward slash,
* followed by a representation of a {@link Duration}
* <li>a representation of a {@link Duration}, followed by a forward slash,
* followed by a representation of a {@link PeriodDuration}
* <li>a representation of a {@link PeriodDuration}, followed by a forward slash,
* followed by a representation of an {@link OffsetDateTime}
* </ul>
*
*
* @param text the text to parse, not null
* @return the parsed interval, not null
* @throws DateTimeParseException if the text cannot be parsed
Expand All @@ -149,21 +148,21 @@ public static Interval parse(CharSequence text) {
char firstChar = text.charAt(0);
if (firstChar == 'P' || firstChar == 'p') {
// duration followed by instant
Duration duration = Duration.parse(text.subSequence(0, i));
Instant end = OffsetDateTime.parse(text.subSequence(i + 1, text.length())).toInstant();
return Interval.of(end.minus(duration), end);
PeriodDuration amount = PeriodDuration.parse(text.subSequence(0, i));
OffsetDateTime end = OffsetDateTime.parse(text.subSequence(i + 1, text.length()));
return Interval.of(end.minus(amount).toInstant(), end.toInstant());
} else {
// instant followed by instant or duration
Instant start = OffsetDateTime.parse(text.subSequence(0, i)).toInstant();
OffsetDateTime start = OffsetDateTime.parse(text.subSequence(0, i));
if (i + 1 < text.length()) {
char c = text.charAt(i + 1);
if (c == 'P' || c == 'p') {
Duration duration = Duration.parse(text.subSequence(i + 1, text.length()));
return Interval.of(start, start.plus(duration));
PeriodDuration amount = PeriodDuration.parse(text.subSequence(i + 1, text.length()));
return Interval.of(start.toInstant(), start.plus(amount).toInstant());
}
}
Instant end = OffsetDateTime.parse(text.subSequence(i + 1, text.length())).toInstant();
return Interval.of(start, end);
OffsetDateTime end = OffsetDateTime.parse(text.subSequence(i + 1, text.length()));
return Interval.of(start.toInstant(), end.toInstant());
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/org/threeten/extra/TestInterval.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
package org.threeten.extra;

import static java.time.temporal.ChronoUnit.HOURS;
import static java.time.temporal.ChronoUnit.MONTHS;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
Expand Down Expand Up @@ -165,6 +166,12 @@ public void test_parse_CharSequence_DurationInstant() {
assertEquals(test.getEnd(), NOW2);
}

public void test_parse_CharSequence_PeriodDurationInstant() {
Interval test = Interval.parse("P6MT5H/" + NOW2);
assertEquals(test.getStart(), NOW2.atZone(ZoneOffset.UTC).minus(6, MONTHS).minus(5, HOURS).toInstant());
assertEquals(test.getEnd(), NOW2);
}

public void test_parse_CharSequence_DurationInstant_caseInsensitive() {
Interval test = Interval.parse("pt6h/" + NOW2);
assertEquals(test.getStart(), NOW2.minus(6, HOURS));
Expand Down

0 comments on commit 3f2bebf

Please sign in to comment.