Skip to content

Commit

Permalink
Rework the Windows timezone implementation (#390)
Browse files Browse the repository at this point in the history
Now, cinterop is not used in timezone implementations,
improving compatibility between versions of the library.

As part of the rewrite, the allocation strategy on startup
was improved, which should lower memory usage during
initialization.

Additionally, the implementation is now thoroughly tested.
  • Loading branch information
dkhalanskyjb committed Aug 8, 2024
1 parent da6f678 commit 19e3160
Show file tree
Hide file tree
Showing 9 changed files with 356 additions and 118 deletions.
26 changes: 6 additions & 20 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import java.io.ByteArrayOutputStream
import java.io.PrintWriter
import org.jetbrains.dokka.gradle.AbstractDokkaLeafTask
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
import org.jetbrains.kotlin.konan.target.Family

plugins {
kotlin("multiplatform")
Expand All @@ -29,9 +30,6 @@ val serializationVersion: String by project

java {
toolchain { languageVersion.set(JavaLanguageVersion.of(mainJavaToolchainVersion)) }
with(javaToolchains.launcherFor(toolchain).get().metadata) {
logger.info("Using JDK $languageVersion toolchain installed in $installationPath")
}
}

kotlin {
Expand Down Expand Up @@ -150,25 +148,13 @@ kotlin {
compilations["test"].kotlinOptions {
freeCompilerArgs += listOf("-trw")
}
when {
konanTarget.family == org.jetbrains.kotlin.konan.target.Family.MINGW -> {
compilations["main"].cinterops {
create("declarations") {
defFile("$projectDir/windows/cinterop/definitions.def")
headers("$projectDir/windows/cinterop/definitions.h")
}
if (konanTarget.family == Family.MINGW) {
compilations["test"].cinterops {
create("modern_api") {
defFile("$projectDir/windows/test_cinterop/modern_api.def")
headers("$projectDir/windows/test_cinterop/modern_api.h")
}
}

konanTarget.family == org.jetbrains.kotlin.konan.target.Family.LINUX ||
konanTarget.family == org.jetbrains.kotlin.konan.target.Family.ANDROID ||
konanTarget.family.isAppleFamily ->
{
// do nothing special
}
else -> {
throw IllegalArgumentException("Unknown native target ${this@withType}")
}
}
}
sourceSets {
Expand Down
16 changes: 9 additions & 7 deletions core/native/src/internal/MonthDayTime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,17 @@ internal class MonthDayTime(
* Converts this [MonthDayTime] to an [Instant] in the given [year],
* using the knowledge of the offset that's in effect at the resulting datetime.
*/
fun toInstant(year: Int, effectiveOffset: UtcOffset): Instant {
val localDateTime = time.resolve(date.toLocalDate(year))
return when (this.offset) {
is OffsetResolver.WallClockOffset -> localDateTime.toInstant(effectiveOffset)
is OffsetResolver.FixedOffset -> localDateTime.toInstant(this.offset.offset)
}
fun toInstant(year: Int, effectiveOffset: UtcOffset): Instant = when (this.offset) {
is OffsetResolver.WallClockOffset -> toLocalDateTime(year).toInstant(effectiveOffset)
is OffsetResolver.FixedOffset -> toLocalDateTime(year).toInstant(this.offset.offset)
}

/**
* Converts this [MonthDayTime] to a [LocalDateTime] in the given [year],
* ignoring the UTC offset.
*/
fun toLocalDateTime(year: Int): LocalDateTime = time.resolve(date.toLocalDate(year))

/**
* Describes how the offset in which the local datetime is expressed is defined.
*/
Expand All @@ -160,7 +163,6 @@ internal class MonthDayTime(
* The local time of day at which the transition occurs.
*/
class TransitionLocaltime(val seconds: Int) {
constructor(time: LocalTime) : this(time.toSecondOfDay())

constructor(hour: Int, minute: Int, second: Int) : this(hour * 3600 + minute * 60 + second)

Expand Down
4 changes: 2 additions & 2 deletions core/native/src/internal/TimeZoneRules.kt
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@ internal class RecurringZoneRules(
val offsetBefore: UtcOffset,
val offsetAfter: UtcOffset,
) {
override fun toString(): String = "transitioning to $offsetAfter on $transitionDateTime"
override fun toString(): String = "transitioning from $offsetBefore to $offsetAfter on $transitionDateTime"
}

// see `tzparse` in https://data.iana.org/time-zones/tzdb/localtime.c: looks like there's no guarantees about
// a way to pre-sort the transitions, so we have to do it for each query separately.
private fun rulesForYear(year: Int): List<Rule<Instant>> {
fun rulesForYear(year: Int): List<Rule<Instant>> {
return rules.map { rule ->
val transitionInstant = rule.transitionDateTime.toInstant(year, rule.offsetBefore)
Rule(transitionInstant, rule.offsetBefore, rule.offsetAfter)
Expand Down
11 changes: 0 additions & 11 deletions core/windows/cinterop/definitions.h

This file was deleted.

Loading

0 comments on commit 19e3160

Please sign in to comment.