Skip to content

Commit

Permalink
Adds old datetime to datum conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
RCHowell committed Dec 24, 2024
1 parent b337934 commit d07e02f
Showing 1 changed file with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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")
}
}
}

0 comments on commit d07e02f

Please sign in to comment.