Skip to content

Commit

Permalink
Merge pull request #958 from Kotlin/local-time-converters
Browse files Browse the repository at this point in the history
Adding some missing converters
  • Loading branch information
Jolanrensen authored Nov 26, 2024
2 parents 7d48035 + 58303bf commit 510680c
Show file tree
Hide file tree
Showing 5 changed files with 814 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import kotlinx.datetime.toJavaLocalTime
import kotlinx.datetime.toKotlinInstant
import kotlinx.datetime.toKotlinLocalDate
import kotlinx.datetime.toKotlinLocalDateTime
import kotlinx.datetime.toKotlinLocalTime
import kotlinx.datetime.toLocalDateTime
import org.jetbrains.kotlinx.dataframe.AnyCol
import org.jetbrains.kotlinx.dataframe.DataColumn
Expand All @@ -40,6 +41,7 @@ import org.jetbrains.kotlinx.dataframe.impl.createStarProjectedType
import org.jetbrains.kotlinx.dataframe.path
import org.jetbrains.kotlinx.dataframe.type
import java.math.BigDecimal
import java.math.BigInteger
import java.net.URL
import java.util.Locale
import kotlin.math.roundToInt
Expand Down Expand Up @@ -332,6 +334,8 @@ internal fun createConverter(from: KType, to: KType, options: ParserOptions? = n

BigDecimal::class -> convert<Boolean> { if (it) BigDecimal.ONE else BigDecimal.ZERO }

BigInteger::class -> convert<Boolean> { if (it) BigInteger.ONE else BigInteger.ZERO }

else -> null
}

Expand All @@ -343,10 +347,19 @@ internal fun createConverter(from: KType, to: KType, options: ParserOptions? = n
Short::class -> convert<Number> { it.toShort() }
Long::class -> convert<Number> { it.toLong() }
Boolean::class -> convert<Number> { it.toDouble() != 0.0 }
BigDecimal::class -> convert<Number> { it.toBigDecimal() }
BigInteger::class -> convert<Number> { it.toBigInteger() }
else -> null
}

Char::class -> when (toClass) {
Int::class -> convert<Char> { it.code }
else -> null
}

Int::class -> when (toClass) {
Char::class -> convert<Int> { it.toChar() }

Double::class -> convert<Int> { it.toDouble() }

Float::class -> convert<Int> { it.toFloat() }
Expand All @@ -359,6 +372,8 @@ internal fun createConverter(from: KType, to: KType, options: ParserOptions? = n

BigDecimal::class -> convert<Int> { it.toBigDecimal() }

BigInteger::class -> convert<Int> { it.toBigInteger() }

Boolean::class -> convert<Int> { it != 0 }

LocalDateTime::class -> convert<Int> { it.toLong().toLocalDateTime(defaultTimeZone) }
Expand All @@ -382,12 +397,90 @@ internal fun createConverter(from: KType, to: KType, options: ParserOptions? = n
else -> null
}

Byte::class -> when (toClass) {
Double::class -> convert<Byte> { it.toDouble() }

Float::class -> convert<Byte> { it.toFloat() }

Int::class -> convert<Byte> { it.toInt() }

Short::class -> convert<Byte> { it.toShort() }

Long::class -> convert<Byte> { it.toLong() }

BigDecimal::class -> convert<Byte> { it.toBigDecimal() }

BigInteger::class -> convert<Byte> { it.toBigInteger() }

Boolean::class -> convert<Byte> { it != 0.toByte() }

LocalDateTime::class -> convert<Byte> { it.toLong().toLocalDateTime(defaultTimeZone) }

LocalDate::class -> convert<Byte> { it.toLong().toLocalDate(defaultTimeZone) }

LocalTime::class -> convert<Byte> { it.toLong().toLocalTime(defaultTimeZone) }

Instant::class -> convert<Byte> { Instant.fromEpochMilliseconds(it.toLong()) }

JavaLocalDateTime::class -> convert<Byte> {
it.toLong().toLocalDateTime(defaultTimeZone).toJavaLocalDateTime()
}

JavaLocalDate::class -> convert<Byte> { it.toLong().toLocalDate(defaultTimeZone).toJavaLocalDate() }

JavaLocalTime::class -> convert<Byte> { it.toLong().toLocalTime(defaultTimeZone).toJavaLocalTime() }

JavaInstant::class -> convert<Byte> { JavaInstant.ofEpochMilli(it.toLong()) }

else -> null
}

Short::class -> when (toClass) {
Double::class -> convert<Short> { it.toDouble() }

Float::class -> convert<Short> { it.toFloat() }

Int::class -> convert<Short> { it.toInt() }

Byte::class -> convert<Short> { it.toByte() }

Long::class -> convert<Short> { it.toLong() }

BigDecimal::class -> convert<Short> { it.toBigDecimal() }

BigInteger::class -> convert<Short> { it.toBigInteger() }

Boolean::class -> convert<Short> { it != 0.toShort() }

LocalDateTime::class -> convert<Short> { it.toLong().toLocalDateTime(defaultTimeZone) }

LocalDate::class -> convert<Short> { it.toLong().toLocalDate(defaultTimeZone) }

LocalTime::class -> convert<Short> { it.toLong().toLocalTime(defaultTimeZone) }

Instant::class -> convert<Short> { Instant.fromEpochMilliseconds(it.toLong()) }

JavaLocalDateTime::class -> convert<Short> {
it.toLong().toLocalDateTime(defaultTimeZone).toJavaLocalDateTime()
}

JavaLocalDate::class -> convert<Short> { it.toLong().toLocalDate(defaultTimeZone).toJavaLocalDate() }

JavaLocalTime::class -> convert<Short> { it.toLong().toLocalTime(defaultTimeZone).toJavaLocalTime() }

JavaInstant::class -> convert<Short> { JavaInstant.ofEpochMilli(it.toLong()) }

else -> null
}

Double::class -> when (toClass) {
Int::class -> convert<Double> { it.roundToInt() }
Float::class -> convert<Double> { it.toFloat() }
Long::class -> convert<Double> { it.roundToLong() }
Short::class -> convert<Double> { it.roundToInt().toShort() }
Byte::class -> convert<Double> { it.roundToInt().toByte() }
BigDecimal::class -> convert<Double> { it.toBigDecimal() }
BigInteger::class -> convert<Double> { it.toBigInteger() }
Boolean::class -> convert<Double> { it != 0.0 }
else -> null
}
Expand All @@ -405,12 +498,16 @@ internal fun createConverter(from: KType, to: KType, options: ParserOptions? = n

BigDecimal::class -> convert<Long> { it.toBigDecimal() }

BigInteger::class -> convert<Long> { it.toBigInteger() }

Boolean::class -> convert<Long> { it != 0L }

LocalDateTime::class -> convert<Long> { it.toLocalDateTime(defaultTimeZone) }

LocalDate::class -> convert<Long> { it.toLocalDate(defaultTimeZone) }

LocalTime::class -> convert<Long> { it.toLocalTime(defaultTimeZone) }

Instant::class -> convert<Long> { Instant.fromEpochMilliseconds(it) }

JavaLocalDateTime::class -> convert<Long> {
Expand Down Expand Up @@ -480,21 +577,38 @@ internal fun createConverter(from: KType, to: KType, options: ParserOptions? = n
Double::class -> convert<Float> { it.toDouble() }
Long::class -> convert<Float> { it.roundToLong() }
Int::class -> convert<Float> { it.roundToInt() }
Byte::class -> convert<Float> { it.roundToInt().toByte() }
Short::class -> convert<Float> { it.roundToInt().toShort() }
BigDecimal::class -> convert<Float> { it.toBigDecimal() }
BigInteger::class -> convert<Float> { it.toBigInteger() }
Boolean::class -> convert<Float> { it != 0.0F }
else -> null
}

BigDecimal::class -> when (toClass) {
Double::class -> convert<BigDecimal> { it.toDouble() }
Int::class -> convert<BigDecimal> { it.toInt() }
Byte::class -> convert<BigDecimal> { it.toByte() }
Short::class -> convert<BigDecimal> { it.toShort() }
Float::class -> convert<BigDecimal> { it.toFloat() }
Long::class -> convert<BigDecimal> { it.toLong() }
BigInteger::class -> convert<BigDecimal> { it.toBigInteger() }
Boolean::class -> convert<BigDecimal> { it != BigDecimal.ZERO }
else -> null
}

BigInteger::class -> when (toClass) {
Double::class -> convert<BigInteger> { it.toDouble() }
Int::class -> convert<BigInteger> { it.toInt() }
Byte::class -> convert<BigInteger> { it.toByte() }
Short::class -> convert<BigInteger> { it.toShort() }
Float::class -> convert<BigInteger> { it.toFloat() }
Long::class -> convert<BigInteger> { it.toLong() }
BigDecimal::class -> convert<BigInteger> { it.toBigDecimal() }
Boolean::class -> convert<BigInteger> { it != BigInteger.ZERO }
else -> null
}

LocalDateTime::class -> when (toClass) {
LocalDate::class -> convert<LocalDateTime> { it.date }
LocalTime::class -> convert<LocalDateTime> { it.time }
Expand Down Expand Up @@ -565,6 +679,16 @@ internal fun createConverter(from: KType, to: KType, options: ParserOptions? = n
else -> null
}

LocalTime::class -> when (toClass) {
JavaLocalTime::class -> convert<LocalTime> { it.toJavaLocalTime() }
else -> null
}

JavaLocalTime::class -> when (toClass) {
LocalTime::class -> convert<JavaLocalTime> { it.toKotlinLocalTime() }
else -> null
}

URL::class -> when (toClass) {
IMG::class -> convert<URL> { IMG(it.toString()) }
IFRAME::class -> convert<URL> { IFRAME(it.toString()) }
Expand All @@ -588,3 +712,21 @@ internal fun Instant.toLocalDate(zone: TimeZone = defaultTimeZone) = toLocalDate
internal fun Instant.toLocalTime(zone: TimeZone = defaultTimeZone) = toLocalDateTime(zone).time

internal val defaultTimeZone = TimeZone.currentSystemDefault()

internal fun Number.toBigDecimal(): BigDecimal =
when (this) {
is BigDecimal -> this
is BigInteger -> BigDecimal(this)
is Double -> BigDecimal.valueOf(this)
is Int -> BigDecimal(this)
is Long -> BigDecimal.valueOf(this)
else -> BigDecimal.valueOf(this.toDouble())
}

internal fun Number.toBigInteger(): BigInteger =
when (this) {
is BigInteger -> this
is BigDecimal -> this.toBigInteger()
is Long -> BigInteger.valueOf(this)
else -> BigInteger.valueOf(this.toLong())
}
Loading

0 comments on commit 510680c

Please sign in to comment.