Skip to content

Commit 1b41225

Browse files
committed
Add a workaround for #51
1 parent fb0fcf0 commit 1b41225

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

core/commonTest/src/InstantTest.kt

+1-3
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,7 @@ class InstantTest {
129129
expectBetween(instant1, instant4, 61, DateTimeUnit.WEEK)
130130
expectBetween(instant1, instant4, 366 + 31 + 30, DateTimeUnit.DAY)
131131
expectBetween(instant1, instant4, (366 + 31 + 30) * 24 + 1, DateTimeUnit.HOUR)
132-
/* TODO: make this pass on Darwin-based and Windows systems.
133-
See https://github.com/Kotlin/kotlinx-datetime/issues/51 */
134-
// assertEquals(instant1.plus(DateTimeUnit.HOUR), instant4.minus(14, DateTimeUnit.MONTH, zone))
132+
assertEquals(instant1.plus(DateTimeUnit.HOUR), instant4.minus(14, DateTimeUnit.MONTH, zone))
135133

136134
val period = DateTimePeriod(days = 1, hours = 1)
137135
val instant5 = instant1.plus(period, zone)

core/nativeMain/src/ZonedDateTime.kt

+10-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,16 @@ internal class ZonedDateTime(val dateTime: LocalDateTime, private val zone: Time
1616
internal fun plus(value: Int, unit: DateTimeUnit.DateBased): ZonedDateTime = dateTime.plus(value, unit).resolve()
1717

1818
// Never throws in practice
19-
private fun LocalDateTime.resolve(): ZonedDateTime = with(zone) { atZone(offset) }
19+
private fun LocalDateTime.resolve(): ZonedDateTime =
20+
// workaround for https://github.com/Kotlin/kotlinx-datetime/issues/51
21+
if (toInstant(offset).toLocalDateTime(zone) == this@resolve) {
22+
// this LocalDateTime is valid in these timezone and offset.
23+
ZonedDateTime(this, zone, offset)
24+
} else {
25+
// this LDT does need proper resolving, as the instant that it would map to given the preferred offset
26+
// is is mapped to another LDT.
27+
with(zone) { atZone(offset) }
28+
}
2029

2130
override fun equals(other: Any?): Boolean =
2231
this === other || other is ZonedDateTime &&

0 commit comments

Comments
 (0)