Skip to content

Commit 96c9d3d

Browse files
emilwihlanderemil-wihlander-zynka
authored andcommitted
Implement support for interval units.
[#566][resolves #565] Co-authored-by: Emil Wihlander <[email protected]>
1 parent 7207133 commit 96c9d3d

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/main/java/io/r2dbc/postgresql/codec/Interval.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -828,16 +828,24 @@ private static Interval parsePostgresIntervalValue(String value) {
828828

829829
token = st.nextToken();
830830

831-
// This handles years, months, days for both, ISO and
831+
// This handles millenniums, centuries, decades, years, months, weeks, days for both, ISO and
832832
// Non-ISO intervals. Hours, minutes, seconds and microseconds
833833
// are handled for Non-ISO intervals here.
834834

835-
if (token.startsWith("year") || token.startsWith("yr")) {
836-
years = Integer.parseInt(valueToken);
835+
if (token.startsWith("mill")) {
836+
years = Math.addExact(years, Math.toIntExact(Math.multiplyExact(Integer.parseInt(valueToken), 1000)));
837+
} else if (token.startsWith("cent") || token.equals("c")) {
838+
years = Math.addExact(years, Math.toIntExact(Math.multiplyExact(Integer.parseInt(valueToken), 100)));
839+
} else if (token.startsWith("dec")) {
840+
years = Math.addExact(years, Math.toIntExact(Math.multiplyExact(Integer.parseInt(valueToken), 10)));
841+
} else if (token.startsWith("year") || token.startsWith("yr")) {
842+
years = Math.addExact(years, Math.toIntExact(Integer.parseInt(valueToken)));
837843
} else if (token.startsWith("mon")) {
838844
months = Integer.parseInt(valueToken);
845+
} else if (token.startsWith("week") || token.equals("w")) {
846+
days = Math.addExact(days, Math.toIntExact(Math.multiplyExact(Integer.parseInt(valueToken), 7)));
839847
} else if (token.startsWith("day")) {
840-
days = Integer.parseInt(valueToken);
848+
days = Math.addExact(days, Math.toIntExact(Integer.parseInt(valueToken)));
841849
} else if (token.startsWith("hour") || token.startsWith("hr")) {
842850
hours = Integer.parseInt(valueToken);
843851
} else if (token.startsWith("min")) {

src/test/java/io/r2dbc/postgresql/codec/IntervalUnitTests.java

+6
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ static Stream<Object[]> parseValues() {
6363
new Object[]{"8 years 2 mons 3 days 04:05", Interval.of(8, 2, 3, 4, 5, 0, 0)},
6464
new Object[]{"00:00:00", Interval.ZERO},
6565
new Object[]{"-1 years -2 mons +3 days -04:05:06", Interval.of(-1, -2, 3, -4, -5, -6, 0)},
66+
new Object[]{"4 weeks", Interval.of(0, 0, 28, 0, 0, 0.0)},
67+
new Object[]{"1 w 3 days", Interval.of(0, 0, 10, 0, 0, 0.0)},
68+
new Object[]{"2 millenniums 4 years", Interval.of(2004, 0, 0, 0, 0, 0.0)},
69+
new Object[]{"1 dec 8 yrs", Interval.of(18, 0, 0, 0, 0, 0.0)},
70+
new Object[]{"2 c 2 decade", Interval.of(220, 0, 0, 0, 0, 0.0)},
71+
new Object[]{"1 century 4 days", Interval.of(100, 0, 4, 0, 0, 0.0)},
6672

6773
// intervalstyle=postgres_verbose
6874
new Object[]{"@ 3 days 4 hours 5 mins 6 secs", Interval.of(0, 0, 3, 4, 5, 6, 0)},

0 commit comments

Comments
 (0)