Skip to content

Commit

Permalink
Fixing LocalTime rounding (losing precision) (#1570)
Browse files Browse the repository at this point in the history
* Fixing LocalTime rounding

* added tests for toLocalTime as well
  • Loading branch information
franetw committed Jan 29, 2020
1 parent 91d422d commit a7480d2
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class TimestampUtils {
private static final Duration ONE_MICROSECOND = Duration.ofNanos(1000);
// LocalTime.MAX is 23:59:59.999_999_999, and it wraps to 24:00:00 when nanos exceed 999_999_499
// since PostgreSQL has microsecond resolution only
private static final LocalTime MAX_TIME = LocalTime.MAX.minus(Duration.ofMillis(500));
private static final LocalTime MAX_TIME = LocalTime.MAX.minus(Duration.ofNanos(500));
private static final OffsetDateTime MAX_OFFSET_DATETIME = OffsetDateTime.MAX.minus(Duration.ofMillis(500));
private static final LocalDateTime MAX_LOCAL_DATETIME = LocalDateTime.MAX.minus(Duration.ofMillis(500));
// low value for dates is 4713 BC
Expand Down
78 changes: 78 additions & 0 deletions pgjdbc/src/test/java/org/postgresql/jdbc/TimestampUtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright (c) 2019, PostgreSQL Global Development Group
* See the LICENSE file in the project root for more information.
*/

package org.postgresql.jdbc;

import static org.junit.Assert.assertEquals;

import org.postgresql.core.Provider;

import org.junit.Test;

import java.sql.SQLException;
//#if mvn.project.property.postgresql.jdbc.spec >= "JDBC4.2"
import java.time.LocalTime;
//#endif
import java.util.TimeZone;

public class TimestampUtilsTest {

//#if mvn.project.property.postgresql.jdbc.spec >= "JDBC4.2"
@Test
public void testToStringOfLocalTime() {
TimestampUtils timestampUtils = createTimestampUtils();

assertEquals("00:00:00", timestampUtils.toString(LocalTime.parse("00:00:00")));
assertEquals("00:00:00.1", timestampUtils.toString(LocalTime.parse("00:00:00.1")));
assertEquals("00:00:00.12", timestampUtils.toString(LocalTime.parse("00:00:00.12")));
assertEquals("00:00:00.123", timestampUtils.toString(LocalTime.parse("00:00:00.123")));
assertEquals("00:00:00.1234", timestampUtils.toString(LocalTime.parse("00:00:00.1234")));
assertEquals("00:00:00.12345", timestampUtils.toString(LocalTime.parse("00:00:00.12345")));
assertEquals("00:00:00.123456", timestampUtils.toString(LocalTime.parse("00:00:00.123456")));

assertEquals("00:00:00.999999", timestampUtils.toString(LocalTime.parse("00:00:00.999999")));
assertEquals("00:00:00.999999", timestampUtils.toString(LocalTime.parse("00:00:00.999999499"))); // 499 NanoSeconds
assertEquals("00:00:01", timestampUtils.toString(LocalTime.parse("00:00:00.999999500"))); // 500 NanoSeconds

assertEquals("23:59:59", timestampUtils.toString(LocalTime.parse("23:59:59")));
assertEquals("23:59:59.999999", timestampUtils.toString(LocalTime.parse("23:59:59.999999"))); // 0 NanoSeconds
assertEquals("23:59:59.999999", timestampUtils.toString(LocalTime.parse("23:59:59.999999499"))); // 499 NanoSeconds
assertEquals("24:00:00", timestampUtils.toString(LocalTime.parse("23:59:59.999999500")));// 500 NanoSeconds
assertEquals("24:00:00", timestampUtils.toString(LocalTime.parse("23:59:59.999999999")));// 999 NanoSeconds
}

@Test
public void testToLocalTime() throws SQLException {
TimestampUtils timestampUtils = createTimestampUtils();

assertEquals(LocalTime.parse("00:00:00"), timestampUtils.toLocalTime("00:00:00"));

assertEquals(LocalTime.parse("00:00:00.1"), timestampUtils.toLocalTime("00:00:00.1"));
assertEquals(LocalTime.parse("00:00:00.12"), timestampUtils.toLocalTime("00:00:00.12"));
assertEquals(LocalTime.parse("00:00:00.123"), timestampUtils.toLocalTime("00:00:00.123"));
assertEquals(LocalTime.parse("00:00:00.1234"), timestampUtils.toLocalTime("00:00:00.1234"));
assertEquals(LocalTime.parse("00:00:00.12345"), timestampUtils.toLocalTime("00:00:00.12345"));
assertEquals(LocalTime.parse("00:00:00.123456"), timestampUtils.toLocalTime("00:00:00.123456"));
assertEquals(LocalTime.parse("00:00:00.999999"), timestampUtils.toLocalTime("00:00:00.999999"));

assertEquals(LocalTime.parse("23:59:59"), timestampUtils.toLocalTime("23:59:59"));
assertEquals(LocalTime.parse("23:59:59.999999"), timestampUtils.toLocalTime("23:59:59.999999")); // 0 NanoSeconds
assertEquals(LocalTime.parse("23:59:59.9999999"), timestampUtils.toLocalTime("23:59:59.9999999")); // 900 NanoSeconds
assertEquals(LocalTime.parse("23:59:59.99999999"), timestampUtils.toLocalTime("23:59:59.99999999")); // 990 NanoSeconds
assertEquals(LocalTime.parse("23:59:59.999999998"), timestampUtils.toLocalTime("23:59:59.999999998")); // 998 NanoSeconds
assertEquals(LocalTime.parse("23:59:59.999999999"), timestampUtils.toLocalTime("24:00:00"));
}

private TimestampUtils createTimestampUtils() {
return new TimestampUtils(true, new Provider<TimeZone>() {
@Override
public TimeZone get() {
return TimeZone.getDefault();
}
});
}
//#endif

}

0 comments on commit a7480d2

Please sign in to comment.