diff --git a/partiql-spi/src/main/kotlin/org/partiql/value/datetime/DateTimeUtil.kt b/partiql-spi/src/main/kotlin/org/partiql/value/datetime/DateTimeUtil.kt index 8471d0671..793301a33 100644 --- a/partiql-spi/src/main/kotlin/org/partiql/value/datetime/DateTimeUtil.kt +++ b/partiql-spi/src/main/kotlin/org/partiql/value/datetime/DateTimeUtil.kt @@ -15,6 +15,7 @@ package org.partiql.value.datetime import org.partiql.spi.value.Datum +import org.partiql.types.PType import org.partiql.value.datetime.impl.LocalTimeLowPrecision import org.partiql.value.datetime.impl.LocalTimeLowPrecision.Companion.forNano import org.partiql.value.datetime.impl.LocalTimestampLowPrecision.Companion.forDateTime @@ -28,6 +29,7 @@ import java.time.LocalDateTime import java.time.LocalTime import java.time.OffsetDateTime import java.time.OffsetTime +import java.time.ZoneOffset import java.util.regex.Pattern internal object DateTimeUtil { @@ -103,11 +105,29 @@ internal object DateTimeUtil { @JvmStatic fun toDatumTime(time: Time): Datum { - TODO("see if we can avoid this..") + // [0-59].000_000_000 + val ds = time.decimalSecond + val second: Int = ds.toInt() + val nanoOfSecond: Int = ds.remainder(BigDecimal.ONE).movePointRight(9).toInt() + // local + val local = LocalTime.of(time.hour, time.minute, second, nanoOfSecond) + // check offset + if (time.timeZone != null && time.timeZone is TimeZone.UtcOffset) { + val zone = time.timeZone as TimeZone.UtcOffset + val offset = ZoneOffset.ofHoursMinutes(zone.tzHour, zone.tzMinute) + return Datum.timez(local.atOffset(offset), 9) + } + return Datum.time(local, 9) } @JvmStatic fun toDatumTimestamp(timestamp: Timestamp): Datum { - TODO("see if we can avoid this..") + val date = toDatumDate(timestamp.toDate()).localDate + val time = toDatumTime(timestamp.toTime()) + return when (time.type.code()) { + PType.TIME -> Datum.timestamp(LocalDateTime.of(date, time.localTime), 9) + PType.TIMEZ -> Datum.timestampz(OffsetDateTime.of(date, time.localTime, time.offsetTime.offset), 9) + else -> throw IllegalArgumentException("unsupported timestamp type") + } } }