Skip to content

Commit 338b2ea

Browse files
committed
Refactor: move LocalDateTime<->Instant conversion functions closer to TimeZone
1 parent 2bd63a6 commit 338b2ea

File tree

12 files changed

+67
-58
lines changed

12 files changed

+67
-58
lines changed

core/commonMain/src/LocalDate.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ public expect class LocalDate : Comparable<LocalDate> {
3737
*/
3838
public fun String.toLocalDate(): LocalDate = LocalDate.parse(this)
3939

40+
/**
41+
* @see LocalDate.atStartOfDayIn
42+
*/
4043
public fun LocalDate.atTime(hour: Int, minute: Int, second: Int = 0, nanosecond: Int = 0): LocalDateTime =
4144
LocalDateTime(year, monthNumber, dayOfMonth, hour, minute, second, nanosecond)
4245

43-
public expect fun LocalDate.atStartOfDayIn(timeZone: TimeZone): Instant
4446

4547
/**
4648
* @throws DateTimeArithmeticException if arithmetic overflow occurs or the boundaries of [LocalDate] are exceeded at

core/commonMain/src/LocalDateTime.kt

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,3 @@ public expect class LocalDateTime : Comparable<LocalDateTime> {
4646
*/
4747
public fun String.toLocalDateTime(): LocalDateTime = LocalDateTime.parse(this)
4848

49-
/**
50-
* @throws DateTimeArithmeticException if this value is too large to fit in [LocalDateTime].
51-
*/
52-
public expect fun Instant.toLocalDateTime(timeZone: TimeZone): LocalDateTime
53-
54-
/** */
55-
public expect fun Instant.offsetAt(timeZone: TimeZone): ZoneOffset
56-
57-
/** */
58-
public expect fun LocalDateTime.toInstant(timeZone: TimeZone): Instant
59-

core/commonMain/src/TimeZone.kt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,18 @@ public expect open class TimeZone {
3838

3939
public expect class ZoneOffset : TimeZone {
4040
val totalSeconds: Int
41-
}
41+
}
42+
43+
44+
/**
45+
* @throws DateTimeArithmeticException if this value is too large to fit in [LocalDateTime].
46+
*/
47+
public expect fun Instant.toLocalDateTime(timeZone: TimeZone): LocalDateTime
48+
49+
/** */
50+
public expect fun Instant.offsetAt(timeZone: TimeZone): ZoneOffset
51+
52+
/** */
53+
public expect fun LocalDateTime.toInstant(timeZone: TimeZone): Instant
54+
55+
public expect fun LocalDate.atStartOfDayIn(timeZone: TimeZone): Instant

core/jsMain/src/LocalDate.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@ public actual class LocalDate internal constructor(internal val value: jtLocalDa
4646
actual override fun compareTo(other: LocalDate): Int = this.value.compareTo(other.value).toInt()
4747
}
4848

49-
public actual fun LocalDate.atStartOfDayIn(timeZone: TimeZone): Instant =
50-
this.value.atStartOfDay(timeZone.zoneId).toInstant().let(::Instant)
51-
5249
public actual fun LocalDate.plus(unit: DateTimeUnit.DateBased): LocalDate = plusNumber(1, unit)
5350
public actual fun LocalDate.plus(value: Int, unit: DateTimeUnit.DateBased): LocalDate = plusNumber(value, unit)
5451
public actual fun LocalDate.plus(value: Long, unit: DateTimeUnit.DateBased): LocalDate = plusNumber(value, unit)

core/jsMain/src/LocalDateTime.kt

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,3 @@ public actual class LocalDateTime internal constructor(internal val value: jtLoc
5454

5555
}
5656

57-
58-
public actual fun Instant.toLocalDateTime(timeZone: TimeZone): LocalDateTime = try {
59-
jtLocalDateTime.ofInstant(this.value, timeZone.zoneId).let(::LocalDateTime)
60-
} catch (e: Throwable) {
61-
if (e.isJodaDateTimeException()) throw DateTimeArithmeticException(e)
62-
throw e
63-
}
64-
65-
public actual fun Instant.offsetAt(timeZone: TimeZone): ZoneOffset =
66-
timeZone.zoneId.rules().offsetOfInstant(this.value).let(::ZoneOffset)
67-
68-
public actual fun LocalDateTime.toInstant(timeZone: TimeZone): Instant =
69-
this.value.atZone(timeZone.zoneId).toInstant().let(::Instant)

core/jsMain/src/TimeZone.kt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,21 @@ public actual class ZoneOffset internal constructor(zoneOffset: jtZoneOffset): T
4242
internal val zoneOffset get() = zoneId as jtZoneOffset
4343

4444
actual val totalSeconds: Int get() = zoneOffset.totalSeconds().toInt()
45-
}
45+
}
46+
47+
48+
public actual fun Instant.toLocalDateTime(timeZone: TimeZone): LocalDateTime = try {
49+
kotlinx.datetime.internal.JSJoda.LocalDateTime.ofInstant(this.value, timeZone.zoneId).let(::LocalDateTime)
50+
} catch (e: Throwable) {
51+
if (e.isJodaDateTimeException()) throw DateTimeArithmeticException(e)
52+
throw e
53+
}
54+
55+
public actual fun Instant.offsetAt(timeZone: TimeZone): ZoneOffset =
56+
timeZone.zoneId.rules().offsetOfInstant(this.value).let(::ZoneOffset)
57+
58+
public actual fun LocalDateTime.toInstant(timeZone: TimeZone): Instant =
59+
this.value.atZone(timeZone.zoneId).toInstant().let(::Instant)
60+
61+
public actual fun LocalDate.atStartOfDayIn(timeZone: TimeZone): Instant =
62+
this.value.atStartOfDay(timeZone.zoneId).toInstant().let(::Instant)

core/jvmMain/src/LocalDate.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,6 @@ public actual class LocalDate internal constructor(internal val value: jtLocalDa
4747
actual override fun compareTo(other: LocalDate): Int = this.value.compareTo(other.value)
4848
}
4949

50-
public actual fun LocalDate.atStartOfDayIn(timeZone: TimeZone): Instant =
51-
this.value.atStartOfDay(timeZone.zoneId).toInstant().let(::Instant)
52-
5350
public actual fun LocalDate.plus(unit: DateTimeUnit.DateBased): LocalDate =
5451
plus(1L, unit)
5552

core/jvmMain/src/LocalDateTime.kt

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,3 @@ public actual class LocalDateTime internal constructor(internal val value: jtLoc
5858

5959
}
6060

61-
62-
public actual fun Instant.toLocalDateTime(timeZone: TimeZone): LocalDateTime = try {
63-
jtLocalDateTime.ofInstant(this.value, timeZone.zoneId).let(::LocalDateTime)
64-
} catch (e: DateTimeException) {
65-
throw DateTimeArithmeticException(e)
66-
}
67-
68-
public actual fun Instant.offsetAt(timeZone: TimeZone): ZoneOffset =
69-
timeZone.zoneId.rules.getOffset(this.value).let(::ZoneOffset)
70-
71-
public actual fun LocalDateTime.toInstant(timeZone: TimeZone): Instant =
72-
this.value.atZone(timeZone.zoneId).toInstant().let(::Instant)

core/jvmMain/src/TimeZone.kt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,20 @@ public actual class ZoneOffset internal constructor(zoneOffset: jtZoneOffset): T
4545
internal val zoneOffset get() = zoneId as jtZoneOffset
4646

4747
actual val totalSeconds: Int get() = zoneOffset.totalSeconds
48-
}
48+
}
49+
50+
51+
public actual fun Instant.toLocalDateTime(timeZone: TimeZone): LocalDateTime = try {
52+
java.time.LocalDateTime.ofInstant(this.value, timeZone.zoneId).let(::LocalDateTime)
53+
} catch (e: DateTimeException) {
54+
throw DateTimeArithmeticException(e)
55+
}
56+
57+
public actual fun Instant.offsetAt(timeZone: TimeZone): ZoneOffset =
58+
timeZone.zoneId.rules.getOffset(this.value).let(::ZoneOffset)
59+
60+
public actual fun LocalDateTime.toInstant(timeZone: TimeZone): Instant =
61+
this.value.atZone(timeZone.zoneId).toInstant().let(::Instant)
62+
63+
public actual fun LocalDate.atStartOfDayIn(timeZone: TimeZone): Instant =
64+
this.value.atStartOfDay(timeZone.zoneId).toInstant().let(::Instant)

core/nativeMain/src/LocalDate.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,6 @@ public actual class LocalDate actual constructor(actual val year: Int, actual va
242242
}
243243
}
244244

245-
public actual fun LocalDate.atStartOfDayIn(timeZone: TimeZone): Instant =
246-
timeZone.atStartOfDay(this)
247-
248245
public actual fun LocalDate.plus(unit: DateTimeUnit.DateBased): LocalDate =
249246
plus(1, unit)
250247

0 commit comments

Comments
 (0)