Skip to content

Commit 6b76328

Browse files
committed
Remove ZonedDateTime from the Native implementation
1 parent 38d579e commit 6b76328

File tree

5 files changed

+12
-34
lines changed

5 files changed

+12
-34
lines changed

core/native/src/Instant.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ public actual fun Instant.plus(period: DateTimePeriod, timeZone: TimeZone): Inst
271271
val newLdt = toLocalDateTimeFailing(initialOffset)
272272
.run { if (totalMonths != 0) { plus(totalMonths, DateTimeUnit.MONTH) } else { this } }
273273
.run { if (days != 0) { plus(days, DateTimeUnit.DAY) } else { this } }
274-
timeZone.atZone(newLdt, preferred = initialOffset).toInstant()
274+
timeZone.localDateTimeToInstant(newLdt, preferred = initialOffset)
275275
.run { if (totalNanoseconds != 0L) plus(0, totalNanoseconds).check(timeZone) else this }
276276
}.check(timeZone)
277277
} catch (e: ArithmeticException) {
@@ -294,7 +294,7 @@ public actual fun Instant.plus(value: Long, unit: DateTimeUnit, timeZone: TimeZo
294294
throw ArithmeticException("Can't add a Long date-based value, as it would cause an overflow")
295295
val initialOffset = offsetIn(timeZone)
296296
val initialLdt = toLocalDateTimeFailing(initialOffset)
297-
timeZone.atZone(initialLdt.plus(value.toInt(), unit), preferred = initialOffset).toInstant()
297+
timeZone.localDateTimeToInstant(initialLdt.plus(value.toInt(), unit), preferred = initialOffset)
298298
}
299299
is DateTimeUnit.TimeBased ->
300300
check(timeZone).plus(value, unit).check(timeZone)
@@ -324,7 +324,7 @@ public actual fun Instant.periodUntil(other: Instant, timeZone: TimeZone): DateT
324324
val months = initialLdt.until(otherLdt, DateTimeUnit.MONTH).toLong().toInt() // `until` on dates never fails
325325
val ldtWithMonths = initialLdt.plus(months, DateTimeUnit.MONTH) // won't throw: thisLdt + months <= otherLdt, which is known to be valid
326326
val days = ldtWithMonths.until(otherLdt, DateTimeUnit.DAY).toLong().toInt() // `until` on dates never fails
327-
val newInstant = timeZone.atZone(ldtWithMonths.plus(days, DateTimeUnit.DAY), preferred = initialOffset).toInstant() // won't throw: thisLdt + days <= otherLdt
327+
val newInstant = timeZone.localDateTimeToInstant(ldtWithMonths.plus(days, DateTimeUnit.DAY), preferred = initialOffset) // won't throw: thisLdt + days <= otherLdt
328328
val nanoseconds = newInstant.until(other, DateTimeUnit.NANOSECOND) // |otherLdt - thisLdt| < 24h
329329

330330
return buildDateTimePeriod(months, days, nanoseconds)

core/native/src/LocalDateTimeWithOffset.kt

Lines changed: 0 additions & 14 deletions
This file was deleted.

core/native/src/TimeZone.kt

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,7 @@ public actual open class TimeZone internal constructor() {
8585
throw DateTimeArithmeticException("Instant $instant is not representable as LocalDateTime.", e)
8686
}
8787

88-
internal open fun localDateTimeToInstant(dateTime: LocalDateTime): Instant =
89-
atZone(dateTime).toInstant()
90-
91-
internal open fun atZone(dateTime: LocalDateTime, preferred: UtcOffset? = null): LocalDateTimeWithOffset =
88+
internal open fun localDateTimeToInstant(dateTime: LocalDateTime, preferred: UtcOffset? = null): Instant =
9289
error("Should be overridden")
9390

9491
override fun equals(other: Any?): Boolean =
@@ -112,11 +109,10 @@ public actual class FixedOffsetTimeZone internal constructor(public actual val o
112109

113110
override fun offsetAtImpl(instant: Instant): UtcOffset = offset
114111

115-
override fun atZone(dateTime: LocalDateTime, preferred: UtcOffset?): LocalDateTimeWithOffset =
116-
LocalDateTimeWithOffset(dateTime, offset)
112+
override fun localDateTimeToInstant(dateTime: LocalDateTime, preferred: UtcOffset?): Instant =
113+
dateTime.toInstant(offset)
117114

118115
override fun instantToLocalDateTime(instant: Instant): LocalDateTime = instant.toLocalDateTime(offset)
119-
override fun localDateTimeToInstant(dateTime: LocalDateTime): Instant = dateTime.toInstant(offset)
120116
}
121117

122118

core/native/src/internal/RegionTimeZone.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ internal class RegionTimeZone(private val tzid: TimeZoneRules, override val id:
2727
}
2828
}
2929

30-
override fun atZone(dateTime: LocalDateTime, preferred: UtcOffset?): LocalDateTimeWithOffset =
30+
override fun localDateTimeToInstant(dateTime: LocalDateTime, preferred: UtcOffset?): Instant =
3131
when (val info = tzid.infoAtDatetime(dateTime)) {
32-
is OffsetInfo.Regular -> LocalDateTimeWithOffset(dateTime, info.offset)
32+
is OffsetInfo.Regular -> dateTime.toInstant(info.offset)
3333
is OffsetInfo.Gap -> {
3434
try {
35-
LocalDateTimeWithOffset(dateTime.plusSeconds(info.transitionDurationSeconds), info.offsetAfter)
35+
dateTime.plusSeconds(info.transitionDurationSeconds).toInstant(info.offsetAfter)
3636
} catch (e: IllegalArgumentException) {
3737
throw DateTimeArithmeticException(
3838
"Overflow whet correcting the date-time to not be in the transition gap",
@@ -41,8 +41,7 @@ internal class RegionTimeZone(private val tzid: TimeZoneRules, override val id:
4141
}
4242
}
4343

44-
is OffsetInfo.Overlap -> LocalDateTimeWithOffset(
45-
dateTime,
44+
is OffsetInfo.Overlap -> dateTime.toInstant(
4645
if (info.offsetAfter == preferred) info.offsetAfter else info.offsetBefore
4746
)
4847
}

core/native/test/ThreeTenBpTimeZoneTest.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,14 @@ class ThreeTenBpTimeZoneTest {
3434
val t1 = LocalDateTime(2020, 3, 29, 2, 14, 17, 201)
3535
val t2 = LocalDateTime(2020, 3, 29, 3, 14, 17, 201)
3636
val tz = TimeZone.of("Europe/Berlin")
37-
assertEquals(tz.atZone(t1), tz.atZone(t2))
37+
assertEquals(tz.localDateTimeToInstant(t1), tz.localDateTimeToInstant(t2))
3838
}
3939

4040
@Test
4141
fun overlappingLocalTime() {
4242
val t = LocalDateTime(2007, 10, 28, 2, 30, 0, 0)
4343
val zone = TimeZone.of("Europe/Paris")
44-
assertEquals(LocalDateTimeWithOffset(
45-
LocalDateTime(2007, 10, 28, 2, 30, 0, 0),
46-
UtcOffset(seconds = 2 * 3600)
47-
), zone.atZone(t))
44+
assertEquals(t.toInstant(UtcOffset(hours = 2)), zone.localDateTimeToInstant(t))
4845
}
4946

5047
}

0 commit comments

Comments
 (0)