Skip to content

Commit 3b8ee5d

Browse files
committed
Remove ZonedDateTime from the Native implementation
1 parent d6a5fa3 commit 3b8ee5d

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
@@ -169,7 +169,7 @@ public actual fun Instant.plus(period: DateTimePeriod, timeZone: TimeZone): Inst
169169
val newLdt = toLocalDateTimeFailing(initialOffset)
170170
.run { if (totalMonths != 0) { plus(totalMonths, DateTimeUnit.MONTH) } else { this } }
171171
.run { if (days != 0) { plus(days, DateTimeUnit.DAY) } else { this } }
172-
timeZone.atZone(newLdt, preferred = initialOffset).toInstant()
172+
timeZone.localDateTimeToInstant(newLdt, preferred = initialOffset)
173173
.run { if (totalNanoseconds != 0L) plus(0, totalNanoseconds).check(timeZone) else this }
174174
}.check(timeZone)
175175
} catch (e: ArithmeticException) {
@@ -192,7 +192,7 @@ public actual fun Instant.plus(value: Long, unit: DateTimeUnit, timeZone: TimeZo
192192
throw ArithmeticException("Can't add a Long date-based value, as it would cause an overflow")
193193
val initialOffset = offsetIn(timeZone)
194194
val initialLdt = toLocalDateTimeFailing(initialOffset)
195-
timeZone.atZone(initialLdt.plus(value.toInt(), unit), preferred = initialOffset).toInstant()
195+
timeZone.localDateTimeToInstant(initialLdt.plus(value.toInt(), unit), preferred = initialOffset)
196196
}
197197
is DateTimeUnit.TimeBased ->
198198
check(timeZone).plus(value, unit).check(timeZone)
@@ -222,7 +222,7 @@ public actual fun Instant.periodUntil(other: Instant, timeZone: TimeZone): DateT
222222
val months = initialLdt.until(otherLdt, DateTimeUnit.MONTH).toLong().toInt() // `until` on dates never fails
223223
val ldtWithMonths = initialLdt.plus(months, DateTimeUnit.MONTH) // won't throw: thisLdt + months <= otherLdt, which is known to be valid
224224
val days = ldtWithMonths.until(otherLdt, DateTimeUnit.DAY).toLong().toInt() // `until` on dates never fails
225-
val newInstant = timeZone.atZone(ldtWithMonths.plus(days, DateTimeUnit.DAY), preferred = initialOffset).toInstant() // won't throw: thisLdt + days <= otherLdt
225+
val newInstant = timeZone.localDateTimeToInstant(ldtWithMonths.plus(days, DateTimeUnit.DAY), preferred = initialOffset) // won't throw: thisLdt + days <= otherLdt
226226
val nanoseconds = newInstant.until(other, DateTimeUnit.NANOSECOND) // |otherLdt - thisLdt| < 24h
227227

228228
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
@@ -92,10 +92,7 @@ public actual open class TimeZone internal constructor() {
9292
throw DateTimeArithmeticException("Instant $instant is not representable as LocalDateTime.", e)
9393
}
9494

95-
internal open fun localDateTimeToInstant(dateTime: LocalDateTime): Instant =
96-
atZone(dateTime).toInstant()
97-
98-
internal open fun atZone(dateTime: LocalDateTime, preferred: UtcOffset? = null): LocalDateTimeWithOffset =
95+
internal open fun localDateTimeToInstant(dateTime: LocalDateTime, preferred: UtcOffset? = null): Instant =
9996
error("Should be overridden")
10097

10198
override fun equals(other: Any?): Boolean =
@@ -119,11 +116,10 @@ public actual class FixedOffsetTimeZone internal constructor(public actual val o
119116

120117
override fun offsetAtImpl(instant: Instant): UtcOffset = offset
121118

122-
override fun atZone(dateTime: LocalDateTime, preferred: UtcOffset?): LocalDateTimeWithOffset =
123-
LocalDateTimeWithOffset(dateTime, offset)
119+
override fun localDateTimeToInstant(dateTime: LocalDateTime, preferred: UtcOffset?): Instant =
120+
dateTime.toInstant(offset)
124121

125122
override fun instantToLocalDateTime(instant: Instant): LocalDateTime = instant.toLocalDateTime(offset)
126-
override fun localDateTimeToInstant(dateTime: LocalDateTime): Instant = dateTime.toInstant(offset)
127123
}
128124

129125

core/native/src/internal/RegionTimeZone.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ internal class RegionTimeZone(private val tzid: TimeZoneRules, override val id:
1818
}
1919
}
2020

21-
override fun atZone(dateTime: LocalDateTime, preferred: UtcOffset?): LocalDateTimeWithOffset =
21+
override fun localDateTimeToInstant(dateTime: LocalDateTime, preferred: UtcOffset?): Instant =
2222
when (val info = tzid.infoAtDatetime(dateTime)) {
23-
is OffsetInfo.Regular -> LocalDateTimeWithOffset(dateTime, info.offset)
23+
is OffsetInfo.Regular -> dateTime.toInstant(info.offset)
2424
is OffsetInfo.Gap -> {
2525
try {
26-
LocalDateTimeWithOffset(dateTime.plusSeconds(info.transitionDurationSeconds), info.offsetAfter)
26+
dateTime.plusSeconds(info.transitionDurationSeconds).toInstant(info.offsetAfter)
2727
} catch (e: IllegalArgumentException) {
2828
throw DateTimeArithmeticException(
2929
"Overflow whet correcting the date-time to not be in the transition gap",
@@ -32,8 +32,7 @@ internal class RegionTimeZone(private val tzid: TimeZoneRules, override val id:
3232
}
3333
}
3434

35-
is OffsetInfo.Overlap -> LocalDateTimeWithOffset(
36-
dateTime,
35+
is OffsetInfo.Overlap -> dateTime.toInstant(
3736
if (info.offsetAfter == preferred) info.offsetAfter else info.offsetBefore
3837
)
3938
}

core/native/test/ThreeTenBpTimeZoneTest.kt

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

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

4946
}

0 commit comments

Comments
 (0)