Skip to content
Permalink
Browse files
Fixing LocalTime rounding (losing precision) (#1570)
* Fixing LocalTime rounding

* added tests for toLocalTime as well
  • Loading branch information
franetw committed Jan 29, 2020
1 parent 91d422d commit a7480d20370fefa70c5df30a402b952d1216b63c
Showing with 79 additions and 1 deletion.
  1. +1 −1 pgjdbc/src/main/java/org/postgresql/jdbc/TimestampUtils.java
  2. +78 −0 pgjdbc/src/test/java/org/postgresql/jdbc/TimestampUtilsTest.java
@@ -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
@@ -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.