Skip to content

Commit bc07761

Browse files
committed
Minor refactor atZone/checkZone helpers in JS
1 parent dbfb009 commit bc07761

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

core/jsMain/src/Instant.kt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,18 +140,19 @@ private fun ZonedDateTime.plusNanosFix(nanoseconds: Long): ZonedDateTime {
140140
}
141141
}
142142

143-
private fun jtInstant.atZone(zone: TimeZone): ZonedDateTime = atZone(zone.zoneId)
143+
private fun Instant.atZone(zone: TimeZone): ZonedDateTime = value.atZone(zone.zoneId)
144+
private fun jtInstant.checkZone(zone: TimeZone): jtInstant = apply { atZone(zone.zoneId) }
144145

145146
public actual fun Instant.plus(unit: DateTimeUnit, zone: TimeZone): Instant =
146147
plus(1, unit, zone)
147148

148149
public actual fun Instant.plus(value: Long, unit: DateTimeUnit, zone: TimeZone): Instant =
149150
try {
150-
val thisZdt = this.value.atZone(zone)
151+
val thisZdt = this.atZone(zone)
151152
when (unit) {
152153
is DateTimeUnit.TimeBased -> {
153154
multiplyAndDivide(value, unit.nanoseconds, NANOS_PER_ONE.toLong()).let {
154-
(d, r) -> this.plusFix(d.toDouble(), r.toInt()).also { it.atZone(zone.zoneId) }
155+
(d, r) -> this.plusFix(d.toDouble(), r.toInt()).checkZone(zone)
155156
}
156157
}
157158
is DateTimeUnit.DateBased.DayBased ->
@@ -166,11 +167,11 @@ public actual fun Instant.plus(value: Long, unit: DateTimeUnit, zone: TimeZone):
166167

167168
public actual fun Instant.plus(value: Int, unit: DateTimeUnit, zone: TimeZone): Instant =
168169
try {
169-
val thisZdt = this.value.atZone(zone)
170+
val thisZdt = this.atZone(zone)
170171
when (unit) {
171172
is DateTimeUnit.TimeBased -> {
172173
multiplyAndDivide(value.toLong(), unit.nanoseconds, NANOS_PER_ONE.toLong()).let {
173-
(d, r) -> this.plusFix(d.toDouble(), r.toInt()).also { it.atZone(zone.zoneId) }
174+
(d, r) -> this.plusFix(d.toDouble(), r.toInt()).checkZone(zone)
174175
}
175176
}
176177
is DateTimeUnit.DateBased.DayBased ->
@@ -200,8 +201,8 @@ public actual fun Instant.periodUntil(other: Instant, zone: TimeZone): DateTimeP
200201
}
201202

202203
public actual fun Instant.until(other: Instant, unit: DateTimeUnit, zone: TimeZone): Long = try {
203-
val thisZdt = this.value.atZone(zone)
204-
val otherZdt = other.value.atZone(zone)
204+
val thisZdt = this.atZone(zone)
205+
val otherZdt = other.atZone(zone)
205206
when(unit) {
206207
is DateTimeUnit.TimeBased -> {
207208
multiplyAddAndDivide(other.epochSeconds - epochSeconds,

core/jvmMain/src/Instant.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
package kotlinx.datetime
88

99
import java.time.DateTimeException
10-
import java.time.ZoneId
1110
import java.time.format.DateTimeParseException
1211
import java.time.temporal.ChronoUnit
1312
import kotlin.time.*

0 commit comments

Comments
 (0)