|
| 1 | +/* |
| 2 | + * Copyright (c) 2022 Airbyte, Inc., all rights reserved. |
| 3 | + */ |
| 4 | + |
| 5 | +package io.airbyte.db.jdbc; |
| 6 | + |
| 7 | +import static io.airbyte.db.DataTypeUtils.DATE_FORMATTER; |
| 8 | +import static io.airbyte.db.DataTypeUtils.TIMESTAMPTZ_FORMATTER; |
| 9 | +import static io.airbyte.db.DataTypeUtils.TIMESTAMP_FORMATTER; |
| 10 | +import static io.airbyte.db.DataTypeUtils.TIMETZ_FORMATTER; |
| 11 | +import static io.airbyte.db.DataTypeUtils.TIME_FORMATTER; |
| 12 | +import static io.airbyte.db.jdbc.AbstractJdbcCompatibleSourceOperations.resolveEra; |
| 13 | +import static java.time.ZoneOffset.UTC; |
| 14 | + |
| 15 | +import java.sql.Date; |
| 16 | +import java.sql.Time; |
| 17 | +import java.sql.Timestamp; |
| 18 | +import java.time.Duration; |
| 19 | +import java.time.Instant; |
| 20 | +import java.time.LocalDate; |
| 21 | +import java.time.LocalDateTime; |
| 22 | +import java.time.LocalTime; |
| 23 | +import java.time.OffsetDateTime; |
| 24 | +import java.time.OffsetTime; |
| 25 | +import java.time.ZonedDateTime; |
| 26 | +import java.time.format.DateTimeFormatter; |
| 27 | +import java.util.concurrent.TimeUnit; |
| 28 | +import org.slf4j.Logger; |
| 29 | +import org.slf4j.LoggerFactory; |
| 30 | + |
| 31 | +public class DateTimeConverter { |
| 32 | + |
| 33 | + private static final Logger LOGGER = LoggerFactory.getLogger(DateTimeConverter.class); |
| 34 | + public static final DateTimeFormatter TIME_WITH_TIMEZONE_FORMATTER = DateTimeFormatter.ofPattern( |
| 35 | + "HH:mm:ss[.][SSSSSSSSS][SSSSSSS][SSSSSS][SSSSS][SSSS][SSS][SS][S][''][XXX][XX][X]"); |
| 36 | + |
| 37 | + public static String convertToTimeWithTimezone(final Object time) { |
| 38 | + if (time instanceof final java.time.OffsetTime timetz) { |
| 39 | + return timetz.format(TIMETZ_FORMATTER); |
| 40 | + } |
| 41 | + final OffsetTime timetz = OffsetTime.parse(time.toString(), TIME_WITH_TIMEZONE_FORMATTER); |
| 42 | + return timetz.format(TIMETZ_FORMATTER); |
| 43 | + } |
| 44 | + |
| 45 | + public static String convertToTimestampWithTimezone(final Object timestamp) { |
| 46 | + if (timestamp instanceof final Timestamp t) { |
| 47 | + // In snapshot mode, debezium produces a java.sql.Timestamp object for the TIMESTAMPTZ type. |
| 48 | + // Conceptually, a timestamp with timezone is an Instant. But t.toInstant() actually mangles the |
| 49 | + // value for ancient dates, because leap years weren't applied consistently in ye olden days. |
| 50 | + // Additionally, toInstant() (and toLocalDateTime()) actually lose the era indicator, so we can't |
| 51 | + // rely on their getEra() methods. |
| 52 | + // So we have special handling for this case, which sidesteps the toInstant conversion. |
| 53 | + final ZonedDateTime timestamptz = t.toLocalDateTime().atZone(UTC); |
| 54 | + final String value = timestamptz.format(TIMESTAMPTZ_FORMATTER); |
| 55 | + return resolveEra(t, value); |
| 56 | + } else if (timestamp instanceof final OffsetDateTime t) { |
| 57 | + return resolveEra(t.toLocalDate(), t.format(TIMESTAMPTZ_FORMATTER)); |
| 58 | + } else if (timestamp instanceof final ZonedDateTime timestamptz) { |
| 59 | + return resolveEra(timestamptz.toLocalDate(), timestamptz.format(TIMESTAMPTZ_FORMATTER)); |
| 60 | + } else { |
| 61 | + // This case probably isn't strictly necessary, but I'm leaving it just in case there's some weird |
| 62 | + // situation that I'm not aware of. |
| 63 | + final Instant instant = Instant.parse(timestamp.toString()); |
| 64 | + final OffsetDateTime offsetDateTime = OffsetDateTime.ofInstant(instant, UTC); |
| 65 | + final ZonedDateTime timestamptz = ZonedDateTime.from(offsetDateTime); |
| 66 | + final LocalDate localDate = timestamptz.toLocalDate(); |
| 67 | + final String value = timestamptz.format(TIMESTAMPTZ_FORMATTER); |
| 68 | + return resolveEra(localDate, value); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * See {@link #convertToTimestampWithTimezone(Object)} for explanation of the weird things happening |
| 74 | + * here. |
| 75 | + */ |
| 76 | + public static String convertToTimestamp(final Object timestamp) { |
| 77 | + if (timestamp instanceof final Timestamp t) { |
| 78 | + // Snapshot mode |
| 79 | + final LocalDateTime localDateTime = t.toLocalDateTime(); |
| 80 | + final String value = localDateTime.format(TIMESTAMP_FORMATTER); |
| 81 | + return resolveEra(t, value); |
| 82 | + } else if (timestamp instanceof final Instant i) { |
| 83 | + // Incremental mode |
| 84 | + return resolveEra(i.atZone(UTC).toLocalDate(), i.atOffset(UTC).toLocalDateTime().format(TIMESTAMP_FORMATTER)); |
| 85 | + } else { |
| 86 | + final LocalDateTime localDateTime = LocalDateTime.parse(timestamp.toString()); |
| 87 | + final LocalDate date = localDateTime.toLocalDate(); |
| 88 | + final String value = localDateTime.format(TIMESTAMP_FORMATTER); |
| 89 | + return resolveEra(date, value); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * See {@link #convertToTimestampWithTimezone(Object)} for explanation of the weird things happening |
| 95 | + * here. |
| 96 | + */ |
| 97 | + public static String convertToDate(final Object date) { |
| 98 | + if (date instanceof final Date d) { |
| 99 | + // Snapshot mode |
| 100 | + final LocalDate localDate = ((Date) date).toLocalDate(); |
| 101 | + return resolveEra(d, localDate.format(DATE_FORMATTER)); |
| 102 | + } else if (date instanceof LocalDate d) { |
| 103 | + // Incremental mode |
| 104 | + return resolveEra(d, d.format(DATE_FORMATTER)); |
| 105 | + } else { |
| 106 | + final LocalDate localDate = LocalDate.parse(date.toString()); |
| 107 | + return resolveEra(localDate, localDate.format(DATE_FORMATTER)); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + public static String convertToTime(final Object time) { |
| 112 | + if (time instanceof final Time sqlTime) { |
| 113 | + return sqlTime.toLocalTime().format(TIME_FORMATTER); |
| 114 | + } else if (time instanceof final LocalTime localTime) { |
| 115 | + return localTime.format(TIME_FORMATTER); |
| 116 | + } else if (time instanceof java.time.Duration) { |
| 117 | + long value = ((Duration) time).toNanos(); |
| 118 | + if (value >= 0 && value <= TimeUnit.DAYS.toNanos(1)) { |
| 119 | + return LocalTime.ofNanoOfDay(value).format(TIME_FORMATTER); |
| 120 | + } else { |
| 121 | + final long updatedValue = 0 > value ? Math.abs(value) : TimeUnit.DAYS.toNanos(1); |
| 122 | + LOGGER.debug("Time values must use number of milliseconds greater than 0 and less than 86400000000000 but its {}, converting to {} ", value, |
| 123 | + updatedValue); |
| 124 | + return LocalTime.ofNanoOfDay(updatedValue).format(TIME_FORMATTER); |
| 125 | + } |
| 126 | + } else { |
| 127 | + return LocalTime.parse(time.toString()).format(TIME_FORMATTER); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | +} |
0 commit comments