|
24 | 24 | import java.math.BigDecimal; |
25 | 25 | import java.math.BigInteger; |
26 | 26 | import java.time.Instant; |
| 27 | +import java.time.LocalDateTime; |
| 28 | +import java.time.ZoneOffset; |
27 | 29 | import java.util.HashMap; |
28 | 30 | import java.util.Map; |
| 31 | +import java.util.concurrent.TimeUnit; |
29 | 32 | import java.util.function.Function; |
| 33 | +import javax.annotation.Nonnull; |
30 | 34 | import javax.annotation.Nullable; |
31 | 35 |
|
32 | 36 | import com.influxdb.v3.client.write.WritePrecision; |
| 37 | +import org.apache.arrow.vector.types.pojo.ArrowType; |
| 38 | +import org.apache.arrow.vector.types.pojo.Field; |
33 | 39 |
|
34 | 40 | import static java.util.function.Function.identity; |
35 | 41 |
|
@@ -111,4 +117,40 @@ public static BigInteger convert(final Instant instant, final WritePrecision pre |
111 | 117 |
|
112 | 118 | return FROM_NANOS.get(precision).apply(nanos); |
113 | 119 | } |
| 120 | + |
| 121 | + public static BigInteger getTimestamp(@Nonnull final Object value, @Nonnull final Field schema) { |
| 122 | + BigInteger result = null; |
| 123 | + |
| 124 | + if (value instanceof Long) { |
| 125 | + if (schema.getFieldType().getType() instanceof ArrowType.Timestamp) { |
| 126 | + ArrowType.Timestamp type = (ArrowType.Timestamp) schema.getFieldType().getType(); |
| 127 | + TimeUnit timeUnit; |
| 128 | + switch (type.getUnit()) { |
| 129 | + case SECOND: |
| 130 | + timeUnit = TimeUnit.SECONDS; |
| 131 | + break; |
| 132 | + case MILLISECOND: |
| 133 | + timeUnit = TimeUnit.MILLISECONDS; |
| 134 | + break; |
| 135 | + case MICROSECOND: |
| 136 | + timeUnit = TimeUnit.MICROSECONDS; |
| 137 | + break; |
| 138 | + default: |
| 139 | + case NANOSECOND: |
| 140 | + timeUnit = TimeUnit.NANOSECONDS; |
| 141 | + break; |
| 142 | + } |
| 143 | + long nanoseconds = TimeUnit.NANOSECONDS.convert((Long) value, timeUnit); |
| 144 | + BigInteger convertedTime = NanosecondConverter.convert(Instant.ofEpochSecond(0, nanoseconds), WritePrecision.NS); |
| 145 | + result = NanosecondConverter.convertToNanos(convertedTime, WritePrecision.NS); |
| 146 | + } else { |
| 147 | + BigInteger convertedTime = NanosecondConverter.convert(Instant.ofEpochMilli((Long) value), WritePrecision.NS); |
| 148 | + result = NanosecondConverter.convertToNanos(convertedTime, WritePrecision.NS); |
| 149 | + } |
| 150 | + } else if (value instanceof LocalDateTime) { |
| 151 | + BigInteger convertedTime = NanosecondConverter.convert(((LocalDateTime) value).toInstant(ZoneOffset.UTC), WritePrecision.NS); |
| 152 | + result = NanosecondConverter.convertToNanos(convertedTime, WritePrecision.NS); |
| 153 | + } |
| 154 | + return result; |
| 155 | + } |
114 | 156 | } |
0 commit comments