-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Interval conversion from/to Duration
The conversion algorithm assumes a year last 12 months and a month lasts 30 days, as Postgres does and ISO 8601 suggests. See https://github.com/postgres/postgres/blob/5bbdfa8a18dc56d3e64aa723a68e02e897cb5ec3/src/include/datatype/timestamp.h#L116 Signed-off-by: Thomas Segismont <tsegismont@gmail.com>
- Loading branch information
1 parent
f763432
commit 009b8bf
Showing
2 changed files
with
117 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
67 changes: 67 additions & 0 deletions
67
vertx-pg-client/src/test/java/io/vertx/pgclient/data/IntervalTest.java
This file contains 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,67 @@ | ||
/* | ||
* Copyright (c) 2011-2024 Contributors to the Eclipse Foundation | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 | ||
* which is available at https://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 | ||
*/ | ||
|
||
package io.vertx.pgclient.data; | ||
|
||
import org.junit.Test; | ||
|
||
import java.time.Duration; | ||
import java.time.LocalDateTime; | ||
|
||
import static java.time.temporal.ChronoUnit.MICROS; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
public class IntervalTest { | ||
|
||
@Test | ||
public void testFromDuration() { | ||
assertEquals(Interval.of(), Interval.of(Duration.ZERO)); | ||
|
||
assertEquals( | ||
Interval.of(0, 0, 0, 0, 0, -1, 999999), | ||
Interval.of(Duration.ZERO.minus(1, MICROS))); | ||
|
||
assertEquals( | ||
Interval.of(0, 0, -15, 0, -12, -3), | ||
Interval.of(Duration.ofDays(-15) | ||
.minusMinutes(12) | ||
.minusSeconds(3))); | ||
|
||
assertEquals( | ||
Interval.of(1, 3, 14, 3, 55, 5, 463123), | ||
Interval.of(Duration.ofDays(12 * 30 + 3 * 30 + 14) | ||
.plusHours(3) | ||
.plusMinutes(55) | ||
.plusSeconds(5) | ||
.plusNanos(463123 * 1000))); | ||
} | ||
|
||
@Test | ||
public void testToDuration() { | ||
assertEquals(Duration.ZERO, Interval.of().toDuration()); | ||
|
||
assertEquals( | ||
Duration.ZERO.minus(1, MICROS), | ||
Interval.of(0, 0, 0, 0, 0, 0, -1).toDuration()); | ||
|
||
assertEquals( | ||
Duration.ofDays(-15) | ||
.minusMinutes(12) | ||
.minusSeconds(3), | ||
Interval.of(0, 0, -15, 0, -12, -3).toDuration()); | ||
|
||
assertEquals( | ||
Duration.between( | ||
LocalDateTime.of(2024, 2, 1, 13, 30), | ||
LocalDateTime.of(2024, 3, 1, 12, 0)), | ||
Interval.of(0, 0, 29, -1, -30, 0, 0).toDuration()); | ||
} | ||
} |