@Override protected Object convertTimestampWithZone(Column column, Field fieldDefn, Object data) { if (data instanceof Long) { LocalDateTime localDateTime = nanosToLocalDateTimeUTC((Long) data); data = OffsetDateTime.of(localDateTime, ZoneOffset.UTC); } else if (data instanceof java.util.Date) { // any Date like subclasses will be given to us by the JDBC driver, which uses the local VM TZ, so we need to go // back to GMT data = OffsetDateTime.ofInstant(Instant.ofEpochMilli(((Date) data).getTime()), ZoneOffset.UTC); } return super.convertTimestampWithZone(column, fieldDefn, data); }
/** * Converts a value object for an expected JDBC type of {@link Types#TIMESTAMP_WITH_TIMEZONE}. * The <a href="http://www.oracle.com/technetwork/articles/java/jf14-date-time-2125367.html">standard ANSI to Java 8 type * mappings</a> specify that the preferred mapping (when using JDBC's {@link java.sql.ResultSet#getObject(int) getObject(...)} * methods) in Java 8 is to return {@link OffsetDateTime} for these values. * <p> * This method handles several types of objects, including {@link OffsetDateTime}, {@link java.sql.Timestamp}, * {@link java.util.Date}, {@link java.time.LocalTime}, and {@link java.time.LocalDateTime}. * * @param column the column definition describing the {@code data} value; never null * @param fieldDefn the field definition; never null * @param data the data object to be converted into a {@link Date Kafka Connect date} type; never null * @return the converted value, or null if the conversion could not be made and the column allows nulls * @throws IllegalArgumentException if the value could not be converted but the column does not allow nulls */ protected Object convertTimestampWithZone(Column column, Field fieldDefn, Object data) { // epoch is the fallback value return convertValue(column, fieldDefn, data, OffsetDateTime.of(LocalDate.ofEpochDay(0), LocalTime.MIDNIGHT, defaultOffset), (r) -> { try { r.deliver(ZonedTimestamp.toIsoString(data, defaultOffset, adjuster)); } catch (IllegalArgumentException e) { } }); }
@Test public void testLegacy() { testLegacyToFormat(OffsetDateTime.of(2017, 2, 28, 2, 0, 45, 0, ZoneOffset.UTC).toInstant(), "%Y-%m-%dT%H:%M:%S %Z", "Asia/Tokyo", "2017-02-28T11:00:45 JST"); }
this.timestamp = OffsetDateTime.of(ldt, offset);
@Test public void testJava() { testJavaToFormat(OffsetDateTime.of(2017, 2, 28, 2, 0, 45, 0, ZoneOffset.UTC).toInstant(), "EEE MMM dd HH:mm:ss uuuu XXXXX", "-07:00", "Mon Feb 27 19:00:45 2017 -07:00"); }
@Test public void testRuby() { testRubyToFormat(OffsetDateTime.of(2017, 2, 28, 2, 0, 45, 0, ZoneOffset.UTC).toInstant(), "%Y-%m-%dT%H:%M:%S %Z", "-09:00", "2017-02-27T17:00:45 -09:00"); }
@Test public void testSimpleDateTime() { MapSettings settings = new MapSettings(); settings.appendProperty(CoreProperties.PROJECT_DATE_PROPERTY, "2017-01-01T12:13:14+0200"); settings.appendProperty(CoreProperties.PROJECT_VERSION_PROPERTY, "version"); Clock clock = mock(Clock.class); ProjectAnalysisInfo info = new ProjectAnalysisInfo(settings.asConfig(), clock); info.start(); OffsetDateTime date = OffsetDateTime.of(2017, 1, 1, 12, 13, 14, 0, ZoneOffset.ofHours(2)); assertThat(info.analysisDate()).isEqualTo(Date.from(date.toInstant())); assertThat(info.analysisVersion()).isEqualTo("version"); }
@Test public void testParseDateTime() { OffsetDateTime hourAfterEpoch = OffsetDateTime.of(1970, 1, 1, 1, 0, 0, 0, ZoneOffset.UTC); long parsedMilliseconds = DateTimeUtils.parseDateTime(hourAfterEpoch.format(DateTimeFormatter.ISO_DATE_TIME)); assertEquals(HOUR_IN_MILLISECONDS, parsedMilliseconds); }
return OffsetDateTime.of(year, monthOfYear, dayOfMonth, hourOfDay, minuteOfHour, secondOfMinute, nanoOfSecond, ZoneOffset.UTC);
/** * Best effort parsing of the given value */ public static LocalDateTime parseBest(String value) { TemporalAccessor parsed = getDateTimeFormatter().parse(value); LocalDate datePart; LocalTime timePart; ZoneOffset zoneOffset; long epochDay = 0, nanoSeconds = 0; int offsetSeconds = 0; // get different parsed parts if (parsed.isSupported(ChronoField.EPOCH_DAY)) { epochDay = parsed.getLong(ChronoField.EPOCH_DAY); } if (parsed.isSupported(ChronoField.NANO_OF_DAY)) { nanoSeconds = parsed.getLong(ChronoField.NANO_OF_DAY); } if (parsed.isSupported(ChronoField.OFFSET_SECONDS)) { offsetSeconds = parsed.get(ChronoField.OFFSET_SECONDS); } zoneOffset = ZoneOffset.ofTotalSeconds(offsetSeconds); datePart = LocalDate.ofEpochDay(epochDay); timePart = LocalTime.ofNanoOfDay(nanoSeconds); return OffsetDateTime.of(datePart, timePart, zoneOffset).toLocalDateTime(); }
@Override public <I extends InputShim> OffsetDateTime read(final KryoShim<I, ?> kryo, final I input, final Class<OffsetDateTime> clazz) { return OffsetDateTime.of(kryo.readObject(input, LocalDateTime.class), kryo.readObject(input, ZoneOffset.class)); } }
@Override protected OffsetDateTime readValue(final ByteBuf buffer, final GraphBinaryReader context) throws SerializationException { final LocalDateTime ldt = context.readValue(buffer, LocalDateTime.class, false); final ZoneOffset zo = context.readValue(buffer, ZoneOffset.class, false); return OffsetDateTime.of(ldt, zo); }
@Override public OffsetDateTime getRandomValue() { LocalDate randomLocalDate = localDateRandomizer.getRandomValue(); LocalTime randomLocalTime = localTimeRandomizer.getRandomValue(); ZoneOffset randomZoneOffset = zoneOffsetRandomizer.getRandomValue(); return OffsetDateTime.of(randomLocalDate, randomLocalTime, randomZoneOffset); }
/** * Returns an {@link java.time.OffsetDateTime} of this offset and the provided {@link java.time.LocalDateTime}. * * @param self a ZoneOffset * @param dateTime a LocalDateTime * @return an OffsetDateTime * @since 2.5.0 */ public static OffsetDateTime leftShift(final ZoneOffset self, LocalDateTime dateTime) { return OffsetDateTime.of(dateTime, self); }
public Instant convertTo(DateTime dt) { return OffsetDateTime.of(dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, dt.nanosecond, dt.utc ? ZoneOffset.UTC : ZoneOffset.of(TimeZoneUtil.DefaultTZ.getID())).toInstant(); }
/** * Returns an {@link java.time.OffsetDateTime} of this time and the provided {@link java.time.LocalDate}. * * @param self an OffsetTime * @param date a LocalDate * @return an OffsetDateTime * @since 2.5.0 */ public static OffsetDateTime leftShift(final OffsetTime self, LocalDate date) { return OffsetDateTime.of(date, self.toLocalTime(), self.getOffset()); }
/** * Converts the Calendar to a corresponding {@link java.time.OffsetDateTime}. * * @param self a Calendar * @return an OffsetDateTime * @since 2.5.0 */ public static OffsetDateTime toOffsetDateTime(final Calendar self) { return OffsetDateTime.of(toLocalDateTime(self), getZoneOffset(self)); }
public OffsetDateTime read (Kryo kryo, Input in, Class<OffsetDateTime> type) { LocalDate date = LocalDateSerializer.read(in); LocalTime time = LocalTimeSerializer.read(in); ZoneOffset offset = ZoneOffsetSerializer.read(in); return OffsetDateTime.of(date, time, offset); } }