@Override public Instant convert(Object val) throws ConverterException { Timestamp ts = (Timestamp) val; return ts.toInstant(); }
private static Instant getInstant(ResultSet r, int i) throws SQLException { Timestamp ts = r.getTimestamp(i); return ts == null ? null : ts.toInstant(); }
@Override @Nullable public Instant mapColumn(ResultSet r, int columnNumber, StatementContext ctx) throws SQLException { final Timestamp timestamp = calendar.isPresent() ? r.getTimestamp(columnNumber, cloneCalendar()) : r.getTimestamp(columnNumber); return timestamp == null ? null : timestamp.toInstant(); }
@Override @Nullable public Instant mapColumn(ResultSet r, String columnLabel, StatementContext ctx) throws SQLException { final Timestamp timestamp = calendar.isPresent() ? r.getTimestamp(columnLabel, cloneCalendar()) : r.getTimestamp(columnLabel); return timestamp == null ? null : timestamp.toInstant(); }
private static ZonedDateTime getZonedDateTime(ResultSet r, int i) throws SQLException { Timestamp ts = r.getTimestamp(i); return ts == null ? null : ZonedDateTime.ofInstant(ts.toInstant(), ZoneId.systemDefault()); }
@Override public ZonedDateTime convertToMapped(Class<? extends ZonedDateTime> type, Timestamp value) { if (value == null) { return null; } Instant instant = value.toInstant(); return ZonedDateTime.ofInstant(instant, ZoneOffset.systemDefault()); } }
private static OffsetDateTime getOffsetDateTime(ResultSet r, int i) throws SQLException { Timestamp ts = r.getTimestamp(i); return ts == null ? null : OffsetDateTime.ofInstant(ts.toInstant(), ZoneId.systemDefault()); }
/** * Get the ISO 8601 formatted representation of the given {@link java.sql.Timestamp}, which contains a date and time but * has no timezone information. * * @param timestamp the JDBC timestamp value * @param zoneId the timezone identifier or offset where the timestamp is defined * @param adjuster the optional component that adjusts the local date value before obtaining the epoch day; may be null if no * adjustment is necessary * @return the ISO 8601 formatted string */ public static String toIsoString(java.sql.Timestamp timestamp, ZoneId zoneId, TemporalAdjuster adjuster) { Instant instant = timestamp.toInstant(); if (adjuster != null) { instant = instant.with(adjuster); } ZonedDateTime zdt = instant.atZone(zoneId); return zdt.format(FORMATTER); }
/** * Get the ISO 8601 formatted representation of the given {@link java.sql.Timestamp}, which contains a date and time but * has no timezone information. * * @param timestamp the JDBC timestamp value; may not be null * @param zoneId the timezone identifier or offset where the timestamp is defined * @param adjuster the optional component that adjusts the local date value before obtaining the epoch day; may be null if no * adjustment is necessary * @return the ISO 8601 formatted string */ public static String toIsoString(java.sql.Timestamp timestamp, ZoneId zoneId, TemporalAdjuster adjuster) { ZonedDateTime zdt = timestamp.toInstant().atZone(zoneId); if (adjuster != null) { zdt = zdt.with(adjuster); } return zdt.format(FORMATTER); }
return ZonedDateTime.ofInstant( ts.toInstant(), ZoneId.systemDefault() );
return LocalDateTime.ofInstant( ts.toInstant(), ZoneId.systemDefault() );
return ts.toInstant();
/** * Converts a string object for an object type of {@link Timestamp}. * If the column definition allows null and default value is 0000-00-00 00:00:00, we need return null, * else 0000-00-00 00:00:00 will be replaced with 1970-01-01 00:00:00; * * @param column the column definition describing the {@code data} value; never null * @param value the string object to be converted into a {@link Timestamp} type; * @return the converted value; */ private Object convertToTimestamp(Column column, String value) { final boolean matches = EPOCH_EQUIVALENT_TIMESTAMP.matcher(value).matches() || "0".equals(value) || EPOCH_TIMESTAMP.equals(value); if (matches) { if (column.isOptional()) { return null; } return Timestamp.from(Instant.EPOCH); } return Timestamp.valueOf(value).toInstant().atZone(ZoneId.systemDefault()); }
@Override public OffsetDateTime convertToMapped(Class<? extends OffsetDateTime> type, Timestamp value) { if (value == null) { return null; } return OffsetDateTime.ofInstant(value.toInstant(), ZoneOffset.systemDefault()); } }
@Override public <X> LocalDate wrap(X value, WrapperOptions options) { if ( value == null ) { return null; } if ( LocalDate.class.isInstance( value ) ) { return (LocalDate) value; } if ( Timestamp.class.isInstance( value ) ) { final Timestamp ts = (Timestamp) value; return LocalDateTime.ofInstant( ts.toInstant(), ZoneId.systemDefault() ).toLocalDate(); } if ( Long.class.isInstance( value ) ) { final Instant instant = Instant.ofEpochMilli( (Long) value ); return LocalDateTime.ofInstant( instant, ZoneId.systemDefault() ).toLocalDate(); } if ( Calendar.class.isInstance( value ) ) { final Calendar calendar = (Calendar) value; return LocalDateTime.ofInstant( calendar.toInstant(), calendar.getTimeZone().toZoneId() ).toLocalDate(); } if ( Date.class.isInstance( value ) ) { if ( java.sql.Date.class.isInstance( value ) ) { return ((java.sql.Date) value).toLocalDate(); } else { return Instant.ofEpochMilli( ((Date) value).getTime() ).atZone( ZoneId.systemDefault() ).toLocalDate(); } } throw unknownWrap( value.getClass() ); }
return LocalDateTime.ofInstant( ts.toInstant(), ZoneId.systemDefault() ).toLocalTime();
return OffsetDateTime.ofInstant( ts.toInstant(), ZoneId.systemDefault() );
return OffsetTime.ofInstant( ts.toInstant(), ZoneId.systemDefault() );
@Override public IdCreateTime map(ResultSet rs, StatementContext ctx) throws SQLException { return new IdCreateTime( rs.getLong("id"), rs.getTimestamp("created_on").toInstant().atOffset(ZoneOffset.UTC)); } }
/** * Map a commit LSN to a point in time when the commit happened. * * @param lsn - LSN of the commit * @return time when the commit was recorded into the database log * @throws SQLException */ public Instant timestampOfLsn(Lsn lsn) throws SQLException { final String query = LSN_TO_TIMESTAMP; if (lsn.getBinary() == null) { return null; } Instant cachedInstant = lsnToInstantCache.get(lsn); if (cachedInstant != null) { return cachedInstant; } return prepareQueryAndMap(query, statement -> { statement.setBytes(1, lsn.getBinary()); }, singleResultMapper(rs -> { final Timestamp ts = rs.getTimestamp(1); final Instant ret = (ts == null) ? null : ts.toInstant(); LOGGER.trace("Timestamp of lsn {} is {}", lsn, ret); if (ret != null) { lsnToInstantCache.put(lsn, ret); } return ret; }, "LSN to timestamp query must return exactly one value")); }