postgresql-jdbc.git / summary / log / commit / refs
commit f06a3328ee8a00d9a2f671c1803e8317e1da4007
Author: Philippe Marschall <philippe.marschall@gmail.com>
Commit: GitHub <noreply@github.com>
Date: Thu Dec 30 19:07:53 2021 +0000
style: import java.time types in TimestampUtils (#2380)
Use imports for java.time types in TimestampUtils.
.../java/org/postgresql/jdbc/TimestampUtils.java | 113 +++++++++++----------
1 file changed, 62 insertions(+), 51 deletions(-)
diff --git a/pgjdbc/src/main/java/org/postgresql/jdbc/TimestampUtils.java b/pgjdbc/src/main/java/org/postgresql/jdbc/TimestampUtils.java
index 3e554a22..ef70e9e2 100644
--- a/pgjdbc/src/main/java/org/postgresql/jdbc/TimestampUtils.java
+++ b/pgjdbc/src/main/java/org/postgresql/jdbc/TimestampUtils.java
@@ -24,6 +24,17 @@ import java.sql.Date;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.OffsetDateTime;
+import java.time.ZoneOffset;
+import java.time.ZonedDateTime;
+import java.time.chrono.IsoEra;
+import java.time.format.DateTimeParseException;
+import java.time.temporal.ChronoField;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
@@ -42,16 +53,16 @@ public class TimestampUtils {
private static final char[][] NUMBERS;
private static final HashMap<String, TimeZone> GMT_ZONES = new HashMap<String, TimeZone>();
private static final int MAX_NANOS_BEFORE_WRAP_ON_ROUND = 999999500;
- private static final java.time.Duration ONE_MICROSECOND = java.time.Duration.ofNanos(1000);
+ 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 java.time.LocalTime MAX_TIME = java.time.LocalTime.MAX.minus(java.time.Duration.ofNanos(500));
- private static final java.time.OffsetDateTime MAX_OFFSET_DATETIME = java.time.OffsetDateTime.MAX.minus(java.time.Duration.ofMillis(500));
- private static final java.time.LocalDateTime MAX_LOCAL_DATETIME = java.time.LocalDateTime.MAX.minus(java.time.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
- private static final java.time.LocalDate MIN_LOCAL_DATE = java.time.LocalDate.of(4713, 1, 1).with(java.time.temporal.ChronoField.ERA, java.time.chrono.IsoEra.BCE.getValue());
- private static final java.time.LocalDateTime MIN_LOCAL_DATETIME = MIN_LOCAL_DATE.atStartOfDay();
- private static final java.time.OffsetDateTime MIN_OFFSET_DATETIME = MIN_LOCAL_DATETIME.atOffset(java.time.ZoneOffset.UTC);
+ private static final LocalDate MIN_LOCAL_DATE = LocalDate.of(4713, 1, 1).with(ChronoField.ERA, IsoEra.BCE.getValue());
+ private static final LocalDateTime MIN_LOCAL_DATETIME = MIN_LOCAL_DATE.atStartOfDay();
+ private static final OffsetDateTime MIN_OFFSET_DATETIME = MIN_LOCAL_DATETIME.atOffset(ZoneOffset.UTC);
private static final @Nullable Field DEFAULT_TIME_ZONE_FIELD;
@@ -412,18 +423,18 @@ public class TimestampUtils {
* @return null if s is null or a LocalTime of the parsed string s.
* @throws SQLException if there is a problem parsing s.
*/
- public java.time.@PolyNull LocalTime toLocalTime(@PolyNull String s) throws SQLException {
+ public @PolyNull LocalTime toLocalTime(@PolyNull String s) throws SQLException {
if (s == null) {
return null;
}
if (s.equals("24:00:00")) {
- return java.time.LocalTime.MAX;
+ return LocalTime.MAX;
}
try {
- return java.time.LocalTime.parse(s);
- } catch (java.time.format.DateTimeParseException nfe) {
+ return LocalTime.parse(s);
+ } catch (DateTimeParseException nfe) {
throw new PSQLException(
GT.tr("Bad value for type timestamp/date/time: {1}", s),
PSQLState.BAD_DATETIME_FORMAT, nfe);
@@ -438,7 +449,7 @@ public class TimestampUtils {
* @return null if s is null or a LocalDateTime of the parsed string s.
* @throws SQLException if there is a problem parsing s.
*/
- public java.time.@PolyNull LocalDateTime toLocalDateTime(@PolyNull String s) throws SQLException {
+ public @PolyNull LocalDateTime toLocalDateTime(@PolyNull String s) throws SQLException {
if (s == null) {
return null;
}
@@ -447,20 +458,20 @@ public class TimestampUtils {
// convert postgres's infinity values to internal infinity magic value
if (slen == 8 && s.equals("infinity")) {
- return java.time.LocalDateTime.MAX;
+ return LocalDateTime.MAX;
}
if (slen == 9 && s.equals("-infinity")) {
- return java.time.LocalDateTime.MIN;
+ return LocalDateTime.MIN;
}
ParsedTimestamp ts = parseBackendTimestamp(s);
// intentionally ignore time zone
// 2004-10-19 10:23:54+03:00 is 2004-10-19 10:23:54 locally
- java.time.LocalDateTime result = java.time.LocalDateTime.of(ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second, ts.nanos);
+ LocalDateTime result = LocalDateTime.of(ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second, ts.nanos);
if (ts.era == GregorianCalendar.BC) {
- return result.with(java.time.temporal.ChronoField.ERA, java.time.chrono.IsoEra.BCE.getValue());
+ return result.with(ChronoField.ERA, IsoEra.BCE.getValue());
} else {
return result;
}
@@ -473,7 +484,7 @@ public class TimestampUtils {
* @return null if s is null or a LocalDateTime of the parsed string s.
* @throws SQLException if there is a problem parsing s.
*/
- public java.time.@PolyNull OffsetDateTime toOffsetDateTime(
+ public @PolyNull OffsetDateTime toOffsetDateTime(
@PolyNull String s) throws SQLException {
if (s == null) {
return null;
@@ -483,11 +494,11 @@ public class TimestampUtils {
// convert postgres's infinity values to internal infinity magic value
if (slen == 8 && s.equals("infinity")) {
- return java.time.OffsetDateTime.MAX;
+ return OffsetDateTime.MAX;
}
if (slen == 9 && s.equals("-infinity")) {
- return java.time.OffsetDateTime.MIN;
+ return OffsetDateTime.MIN;
}
ParsedTimestamp ts = parseBackendTimestamp(s);
@@ -499,12 +510,12 @@ public class TimestampUtils {
} else {
offsetSeconds = tz.get(Calendar.ZONE_OFFSET) / 1000;
}
- java.time.ZoneOffset zoneOffset = java.time.ZoneOffset.ofTotalSeconds(offsetSeconds);
+ ZoneOffset zoneOffset = ZoneOffset.ofTotalSeconds(offsetSeconds);
// Postgres is always UTC
- java.time.OffsetDateTime result = java.time.OffsetDateTime.of(ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second, ts.nanos, zoneOffset)
- .withOffsetSameInstant(java.time.ZoneOffset.UTC);
+ OffsetDateTime result = OffsetDateTime.of(ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second, ts.nanos, zoneOffset)
+ .withOffsetSameInstant(ZoneOffset.UTC);
if (ts.era == GregorianCalendar.BC) {
- return result.with(java.time.temporal.ChronoField.ERA, java.time.chrono.IsoEra.BCE.getValue());
+ return result.with(ChronoField.ERA, IsoEra.BCE.getValue());
} else {
return result;
}
@@ -516,10 +527,10 @@ public class TimestampUtils {
* @param t the time value
* @return the matching offset date time
*/
- public java.time.OffsetDateTime toOffsetDateTime(Time t) {
+ public OffsetDateTime toOffsetDateTime(Time t) {
// hardcode utc because the backend does not provide us the timezone
// hardcode UNIX epoch, JDBC requires OffsetDateTime but doesn't describe what date should be used
- return t.toLocalTime().atDate(java.time.LocalDate.of(1970, 1, 1)).atOffset(java.time.ZoneOffset.UTC);
+ return t.toLocalTime().atDate(LocalDate.of(1970, 1, 1)).atOffset(ZoneOffset.UTC);
}
/**
@@ -529,18 +540,18 @@ public class TimestampUtils {
* @return The parsed local date time object.
* @throws PSQLException If binary format could not be parsed.
*/
- public java.time.OffsetDateTime toOffsetDateTimeBin(byte[] bytes) throws PSQLException {
+ public OffsetDateTime toOffsetDateTimeBin(byte[] bytes) throws PSQLException {
ParsedBinaryTimestamp parsedTimestamp = this.toProlepticParsedTimestampBin(bytes);
if (parsedTimestamp.infinity == Infinity.POSITIVE) {
- return java.time.OffsetDateTime.MAX;
+ return OffsetDateTime.MAX;
} else if (parsedTimestamp.infinity == Infinity.NEGATIVE) {
- return java.time.OffsetDateTime.MIN;
+ return OffsetDateTime.MIN;
}
// hardcode utc because the backend does not provide us the timezone
// Postgres is always UTC
- java.time.Instant instant = java.time.Instant.ofEpochSecond(parsedTimestamp.millis / 1000L, parsedTimestamp.nanos);
- return java.time.OffsetDateTime.ofInstant(instant, java.time.ZoneOffset.UTC);
+ Instant instant = Instant.ofEpochSecond(parsedTimestamp.millis / 1000L, parsedTimestamp.nanos);
+ return OffsetDateTime.ofInstant(instant, ZoneOffset.UTC);
}
public synchronized @PolyNull Time toTime(
@@ -828,8 +839,8 @@ public class TimestampUtils {
}
}
- public synchronized String toString(java.time.LocalDate localDate) {
- if (java.time.LocalDate.MAX.equals(localDate)) {
+ public synchronized String toString(LocalDate localDate) {
+ if (LocalDate.MAX.equals(localDate)) {
return "infinity";
} else if (localDate.isBefore(MIN_LOCAL_DATE)) {
return "-infinity";
@@ -843,7 +854,7 @@ public class TimestampUtils {
return sbuf.toString();
}
- public synchronized String toString(java.time.LocalTime localTime) {
+ public synchronized String toString(LocalTime localTime) {
sbuf.setLength(0);
@@ -862,7 +873,7 @@ public class TimestampUtils {
return sbuf.toString();
}
- public synchronized String toString(java.time.OffsetDateTime offsetDateTime) {
+ public synchronized String toString(OffsetDateTime offsetDateTime) {
if (offsetDateTime.isAfter(MAX_OFFSET_DATETIME)) {
return "infinity";
} else if (offsetDateTime.isBefore(MIN_OFFSET_DATETIME)) {
@@ -877,8 +888,8 @@ public class TimestampUtils {
// it relies on the fact that appendTime just truncates 000..999 nanosecond part
offsetDateTime = offsetDateTime.plus(ONE_MICROSECOND);
}
- java.time.LocalDateTime localDateTime = offsetDateTime.toLocalDateTime();
- java.time.LocalDate localDate = localDateTime.toLocalDate();
+ LocalDateTime localDateTime = offsetDateTime.toLocalDateTime();
+ LocalDate localDate = localDateTime.toLocalDate();
appendDate(sbuf, localDate);
sbuf.append(' ');
appendTime(sbuf, localDateTime.toLocalTime());
@@ -889,12 +900,12 @@ public class TimestampUtils {
}
/**
- * Formats {@link java.time.LocalDateTime} to be sent to the backend, thus it adds time zone.
+ * Formats {@link LocalDateTime} to be sent to the backend, thus it adds time zone.
* Do not use this method in {@link java.sql.ResultSet#getString(int)}
* @param localDateTime The local date to format as a String
* @return The formatted local date
*/
- public synchronized String toString(java.time.LocalDateTime localDateTime) {
+ public synchronized String toString(LocalDateTime localDateTime) {
if (localDateTime.isAfter(MAX_LOCAL_DATETIME)) {
return "infinity";
} else if (localDateTime.isBefore(MIN_LOCAL_DATETIME)) {
@@ -902,18 +913,18 @@ public class TimestampUtils {
}
// LocalDateTime is always passed with time zone so backend can decide between timestamp and timestamptz
- java.time.ZonedDateTime zonedDateTime = localDateTime.atZone(getDefaultTz().toZoneId());
+ ZonedDateTime zonedDateTime = localDateTime.atZone(getDefaultTz().toZoneId());
return toString(zonedDateTime.toOffsetDateTime());
}
- private static void appendDate(StringBuilder sb, java.time.LocalDate localDate) {
- int year = localDate.get(java.time.temporal.ChronoField.YEAR_OF_ERA);
+ private static void appendDate(StringBuilder sb, LocalDate localDate) {
+ int year = localDate.get(ChronoField.YEAR_OF_ERA);
int month = localDate.getMonthValue();
int day = localDate.getDayOfMonth();
appendDate(sb, year, month, day);
}
- private static void appendTime(StringBuilder sb, java.time.LocalTime localTime) {
+ private static void appendTime(StringBuilder sb, LocalTime localTime) {
int hours = localTime.getHour();
int minutes = localTime.getMinute();
int seconds = localTime.getSecond();
@@ -921,14 +932,14 @@ public class TimestampUtils {
appendTime(sb, hours, minutes, seconds, nanos);
}
- private void appendTimeZone(StringBuilder sb, java.time.ZoneOffset offset) {
+ private void appendTimeZone(StringBuilder sb, ZoneOffset offset) {
int offsetSeconds = offset.getTotalSeconds();
appendTimeZone(sb, offsetSeconds);
}
- private static void appendEra(StringBuilder sb, java.time.LocalDate localDate) {
- if (localDate.get(java.time.temporal.ChronoField.ERA) == java.time.chrono.IsoEra.BCE.getValue()) {
+ private static void appendEra(StringBuilder sb, LocalDate localDate) {
+ if (localDate.get(ChronoField.ERA) == IsoEra.BCE.getValue()) {
sb.append(" BC");
}
}
@@ -1081,7 +1092,7 @@ public class TimestampUtils {
* @return The parsed time object.
* @throws PSQLException If binary format could not be parsed.
*/
- public java.time.LocalTime toLocalTimeBin(byte[] bytes) throws PSQLException {
+ public LocalTime toLocalTimeBin(byte[] bytes) throws PSQLException {
if (bytes.length != 8) {
throw new PSQLException(GT.tr("Unsupported binary encoding of {0}.", "time"),
PSQLState.BAD_DATETIME_FORMAT);
@@ -1097,7 +1108,7 @@ public class TimestampUtils {
micros = ByteConverter.int8(bytes, 0);
}
- return java.time.LocalTime.ofNanoOfDay(micros * 1000);
+ return LocalTime.ofNanoOfDay(micros * 1000);
}
/**
@@ -1233,18 +1244,18 @@ public class TimestampUtils {
* @return The parsed local date time object.
* @throws PSQLException If binary format could not be parsed.
*/
- public java.time.LocalDateTime toLocalDateTimeBin(byte[] bytes) throws PSQLException {
+ public LocalDateTime toLocalDateTimeBin(byte[] bytes) throws PSQLException {
ParsedBinaryTimestamp parsedTimestamp = this.toProlepticParsedTimestampBin(bytes);
if (parsedTimestamp.infinity == Infinity.POSITIVE) {
- return java.time.LocalDateTime.MAX;
+ return LocalDateTime.MAX;
} else if (parsedTimestamp.infinity == Infinity.NEGATIVE) {
- return java.time.LocalDateTime.MIN;
+ return LocalDateTime.MIN;
}
// hardcode utc because the backend does not provide us the timezone
// Postgres is always UTC
- return java.time.LocalDateTime.ofEpochSecond(parsedTimestamp.millis / 1000L, parsedTimestamp.nanos, java.time.ZoneOffset.UTC);
+ return LocalDateTime.ofEpochSecond(parsedTimestamp.millis / 1000L, parsedTimestamp.nanos, ZoneOffset.UTC);
}
/**
[parent: 1bb8f6f2e6f4]