Skip to content

Commit c0b7ad7

Browse files
committed
CODEREVIEW AMEND
1 parent 8f41fbe commit c0b7ad7

File tree

10 files changed

+143
-190
lines changed

10 files changed

+143
-190
lines changed

core/js/src/JSJodaExceptions.kt

+2-5
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,5 @@
55

66
package kotlinx.datetime
77

8-
internal actual fun Throwable.isJodaArithmeticException(): Boolean = this.asDynamic().name == "ArithmeticException"
9-
internal actual fun Throwable.isJodaDateTimeException(): Boolean = this.asDynamic().name == "DateTimeException"
10-
internal actual fun Throwable.isJodaDateTimeParseException(): Boolean = this.asDynamic().name == "DateTimeParseException"
11-
12-
internal actual inline fun <reified T : Any> jsTry(crossinline body: () -> T): T = body()
8+
internal actual fun Throwable.hasJsExceptionName(name: String): Boolean =
9+
this.asDynamic().name == name

core/jsAndWasmShared/src/Instant.kt

+50-66
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,14 @@ public actual class Instant internal constructor(internal val value: jtInstant)
6767
Instant(jtClock.systemUTC().instant())
6868

6969
public actual fun fromEpochMilliseconds(epochMilliseconds: Long): Instant = try {
70-
jsTry {
71-
fromEpochSeconds(epochMilliseconds / MILLIS_PER_ONE, epochMilliseconds % MILLIS_PER_ONE * NANOS_PER_MILLI)
72-
}
70+
fromEpochSeconds(epochMilliseconds / MILLIS_PER_ONE, epochMilliseconds % MILLIS_PER_ONE * NANOS_PER_MILLI)
7371
} catch (e: Throwable) {
7472
if (!e.isJodaDateTimeException()) throw e
7573
if (epochMilliseconds > 0) MAX else MIN
7674
}
7775

7876
public actual fun parse(isoString: String): Instant = try {
79-
Instant(jsTry { jtOffsetDateTime.parse(fixOffsetRepresentation(isoString)) }.toInstant())
77+
Instant(jtOffsetDateTime.parse(fixOffsetRepresentation(isoString)).toInstant())
8078
} catch (e: Throwable) {
8179
if (e.isJodaDateTimeParseException()) throw DateTimeFormatException(e)
8280
throw e
@@ -94,21 +92,19 @@ public actual class Instant internal constructor(internal val value: jtInstant)
9492
}
9593

9694
public actual fun fromEpochSeconds(epochSeconds: Long, nanosecondAdjustment: Long): Instant = try {
97-
jsTry {
9895
/* Performing normalization here because otherwise this fails:
9996
assertEquals((Long.MAX_VALUE % 1_000_000_000).toInt(),
10097
Instant.fromEpochSeconds(0, Long.MAX_VALUE).nanosecondsOfSecond) */
10198
val secs = safeAdd(epochSeconds, nanosecondAdjustment.floorDiv(NANOS_PER_ONE.toLong()))
10299
val nos = nanosecondAdjustment.mod(NANOS_PER_ONE.toLong()).toInt()
103100
Instant(jtInstant.ofEpochSecond(secs.toDouble(), nos))
104-
}
105101
} catch (e: Throwable) {
106102
if (!e.isJodaDateTimeException() && e !is ArithmeticException) throw e
107103
if (epochSeconds > 0) MAX else MIN
108104
}
109105

110106
public actual fun fromEpochSeconds(epochSeconds: Long, nanosecondAdjustment: Int): Instant = try {
111-
jsTry { Instant(jtInstant.ofEpochSecond(epochSeconds.toDouble(), nanosecondAdjustment)) }
107+
Instant(jtInstant.ofEpochSecond(epochSeconds.toDouble(), nanosecondAdjustment))
112108
} catch (e: Throwable) {
113109
if (!e.isJodaDateTimeException()) throw e
114110
if (epochSeconds > 0) MAX else MIN
@@ -124,18 +120,16 @@ public actual class Instant internal constructor(internal val value: jtInstant)
124120

125121

126122
public actual fun Instant.plus(period: DateTimePeriod, timeZone: TimeZone): Instant = try {
127-
jsTry {
128-
val thisZdt = this.value.atZone(timeZone.zoneId)
129-
with(period) {
130-
thisZdt
131-
.run { if (totalMonths != 0) plusMonths(totalMonths) else this }
132-
.run { if (days != 0) plusDays(days) else this }
133-
.run { if (hours != 0) plusHours(hours) else this }
134-
.run { if (minutes != 0) plusMinutes(minutes) else this }
135-
.run { if (seconds != 0) plusSeconds(seconds) else this }
136-
.run { if (nanoseconds != 0) plusNanos(nanoseconds.toDouble()) else this }
137-
}.toInstant().let(::Instant)
138-
}
123+
val thisZdt = this.value.atZone(timeZone.zoneId)
124+
with(period) {
125+
thisZdt
126+
.run { if (totalMonths != 0) plusMonths(totalMonths) else this }
127+
.run { if (days != 0) plusDays(days) else this }
128+
.run { if (hours != 0) plusHours(hours) else this }
129+
.run { if (minutes != 0) plusMinutes(minutes) else this }
130+
.run { if (seconds != 0) plusSeconds(seconds) else this }
131+
.run { if (nanoseconds != 0) plusNanos(nanoseconds.toDouble()) else this }
132+
}.toInstant().let(::Instant)
139133
} catch (e: Throwable) {
140134
if (e.isJodaDateTimeException()) throw DateTimeArithmeticException(e)
141135
throw e
@@ -150,40 +144,36 @@ public actual fun Instant.plus(unit: DateTimeUnit, timeZone: TimeZone): Instant
150144

151145
public actual fun Instant.plus(value: Long, unit: DateTimeUnit, timeZone: TimeZone): Instant =
152146
try {
153-
jsTry {
154-
val thisZdt = this.atZone(timeZone)
155-
when (unit) {
156-
is DateTimeUnit.TimeBased -> {
157-
plus(value, unit).value.checkZone(timeZone)
158-
}
159-
160-
is DateTimeUnit.DayBased ->
161-
thisZdt.plusDays(value.toDouble() * unit.days).toInstant()
162-
163-
is DateTimeUnit.MonthBased ->
164-
thisZdt.plusMonths(value.toDouble() * unit.months).toInstant()
165-
}.let(::Instant)
166-
}
147+
val thisZdt = this.atZone(timeZone)
148+
when (unit) {
149+
is DateTimeUnit.TimeBased -> {
150+
plus(value, unit).value.checkZone(timeZone)
151+
}
152+
153+
is DateTimeUnit.DayBased ->
154+
thisZdt.plusDays(value.toDouble() * unit.days).toInstant()
155+
156+
is DateTimeUnit.MonthBased ->
157+
thisZdt.plusMonths(value.toDouble() * unit.months).toInstant()
158+
}.let(::Instant)
167159
} catch (e: Throwable) {
168160
if (e.isJodaDateTimeException()) throw DateTimeArithmeticException(e)
169161
throw e
170162
}
171163

172164
public actual fun Instant.plus(value: Int, unit: DateTimeUnit, timeZone: TimeZone): Instant =
173165
try {
174-
jsTry {
175-
val thisZdt = this.atZone(timeZone)
176-
when (unit) {
177-
is DateTimeUnit.TimeBased ->
178-
plus(value.toLong(), unit).value.checkZone(timeZone)
179-
180-
is DateTimeUnit.DayBased ->
181-
thisZdt.plusDays(value.toDouble() * unit.days).toInstant()
182-
183-
is DateTimeUnit.MonthBased ->
184-
thisZdt.plusMonths(value.toDouble() * unit.months).toInstant()
185-
}.let(::Instant)
186-
}
166+
val thisZdt = this.atZone(timeZone)
167+
when (unit) {
168+
is DateTimeUnit.TimeBased ->
169+
plus(value.toLong(), unit).value.checkZone(timeZone)
170+
171+
is DateTimeUnit.DayBased ->
172+
thisZdt.plusDays(value.toDouble() * unit.days).toInstant()
173+
174+
is DateTimeUnit.MonthBased ->
175+
thisZdt.plusMonths(value.toDouble() * unit.months).toInstant()
176+
}.let(::Instant)
187177
} catch (e: Throwable) {
188178
if (e.isJodaDateTimeException()) throw DateTimeArithmeticException(e)
189179
throw e
@@ -197,10 +187,8 @@ public actual fun Instant.minus(value: Int, unit: DateTimeUnit, timeZone: TimeZo
197187

198188
public actual fun Instant.plus(value: Long, unit: DateTimeUnit.TimeBased): Instant =
199189
try {
200-
jsTry {
201-
multiplyAndDivide(value, unit.nanoseconds, NANOS_PER_ONE.toLong()).let { (d, r) ->
202-
Instant(plusFix(d.toDouble(), r.toInt()))
203-
}
190+
multiplyAndDivide(value, unit.nanoseconds, NANOS_PER_ONE.toLong()).let { (d, r) ->
191+
Instant(plusFix(d.toDouble(), r.toInt()))
204192
}
205193
} catch (e: Throwable) {
206194
if (!e.isJodaDateTimeException()) {
@@ -210,29 +198,25 @@ public actual fun Instant.plus(value: Long, unit: DateTimeUnit.TimeBased): Insta
210198
}
211199

212200
public actual fun Instant.periodUntil(other: Instant, timeZone: TimeZone): DateTimePeriod = try {
213-
jsTry {
214-
var thisZdt = this.value.atZone(timeZone.zoneId)
215-
val otherZdt = other.value.atZone(timeZone.zoneId)
201+
var thisZdt = this.value.atZone(timeZone.zoneId)
202+
val otherZdt = other.value.atZone(timeZone.zoneId)
216203

217-
val months = thisZdt.until(otherZdt, JodaTimeChronoUnit.MONTHS); thisZdt = thisZdt.plusMonths(months)
218-
val days = thisZdt.until(otherZdt, JodaTimeChronoUnit.DAYS); thisZdt = thisZdt.plusDays(days)
219-
val nanoseconds = thisZdt.until(otherZdt, JodaTimeChronoUnit.NANOS)
204+
val months = thisZdt.until(otherZdt, JodaTimeChronoUnit.MONTHS); thisZdt = thisZdt.plusMonths(months)
205+
val days = thisZdt.until(otherZdt, JodaTimeChronoUnit.DAYS); thisZdt = thisZdt.plusDays(days)
206+
val nanoseconds = thisZdt.until(otherZdt, JodaTimeChronoUnit.NANOS)
220207

221-
buildDateTimePeriod(months.toInt(), days.toInt(), nanoseconds.toLong())
222-
}
208+
buildDateTimePeriod(months.toInt(), days.toInt(), nanoseconds.toLong())
223209
} catch (e: Throwable) {
224210
if (e.isJodaDateTimeException()) throw DateTimeArithmeticException(e) else throw e
225211
}
226212

227213
public actual fun Instant.until(other: Instant, unit: DateTimeUnit, timeZone: TimeZone): Long = try {
228-
jsTry {
229-
val thisZdt = this.atZone(timeZone)
230-
val otherZdt = other.atZone(timeZone)
231-
when (unit) {
232-
is DateTimeUnit.TimeBased -> until(other, unit)
233-
is DateTimeUnit.DayBased -> (thisZdt.until(otherZdt, JodaTimeChronoUnit.DAYS) / unit.days).toLong()
234-
is DateTimeUnit.MonthBased -> (thisZdt.until(otherZdt, JodaTimeChronoUnit.MONTHS) / unit.months).toLong()
235-
}
214+
val thisZdt = this.atZone(timeZone)
215+
val otherZdt = other.atZone(timeZone)
216+
when (unit) {
217+
is DateTimeUnit.TimeBased -> until(other, unit)
218+
is DateTimeUnit.DayBased -> (thisZdt.until(otherZdt, JodaTimeChronoUnit.DAYS) / unit.days).toLong()
219+
is DateTimeUnit.MonthBased -> (thisZdt.until(otherZdt, JodaTimeChronoUnit.MONTHS) / unit.months).toLong()
236220
}
237221
} catch (e: ArithmeticException) {
238222
if (this < other) Long.MAX_VALUE else Long.MIN_VALUE

core/jsAndWasmShared/src/JSJodaExceptions.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
package kotlinx.datetime
77

8-
internal expect fun Throwable.isJodaArithmeticException(): Boolean
9-
internal expect fun Throwable.isJodaDateTimeException(): Boolean
10-
internal expect fun Throwable.isJodaDateTimeParseException(): Boolean
8+
internal fun Throwable.isJodaArithmeticException(): Boolean = hasJsExceptionName("ArithmeticException")
9+
internal fun Throwable.isJodaDateTimeException(): Boolean = hasJsExceptionName("DateTimeException")
10+
internal fun Throwable.isJodaDateTimeParseException(): Boolean = hasJsExceptionName("DateTimeParseException")
1111

12-
internal expect inline fun <reified T : Any> jsTry(crossinline body: () -> T): T
12+
internal expect fun Throwable.hasJsExceptionName(name: String): Boolean

core/jsAndWasmShared/src/LocalDate.kt

+12-16
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import kotlinx.datetime.internal.JodaTimeLocalDate as jtLocalDate
1414
public actual class LocalDate internal constructor(internal val value: jtLocalDate) : Comparable<LocalDate> {
1515
public actual companion object {
1616
public actual fun parse(isoString: String): LocalDate = try {
17-
jsTry { jtLocalDate.parse(isoString) }.let(::LocalDate)
17+
jtLocalDate.parse(isoString).let(::LocalDate)
1818
} catch (e: Throwable) {
1919
if (e.isJodaDateTimeParseException()) throw DateTimeFormatException(e)
2020
throw e
@@ -24,7 +24,7 @@ public actual class LocalDate internal constructor(internal val value: jtLocalDa
2424
internal actual val MAX: LocalDate = LocalDate(jtLocalDate.MAX)
2525

2626
public actual fun fromEpochDays(epochDays: Int): LocalDate = try {
27-
jsTry { LocalDate(jtLocalDate.ofEpochDay(epochDays)) }
27+
LocalDate(jtLocalDate.ofEpochDay(epochDays))
2828
} catch (e: Throwable) {
2929
if (e.isJodaDateTimeException()) throw IllegalArgumentException(e)
3030
throw e
@@ -33,7 +33,7 @@ public actual class LocalDate internal constructor(internal val value: jtLocalDa
3333

3434
public actual constructor(year: Int, monthNumber: Int, dayOfMonth: Int) :
3535
this(try {
36-
jsTry { jtLocalDate.of(year, monthNumber, dayOfMonth) }
36+
jtLocalDate.of(year, monthNumber, dayOfMonth)
3737
} catch (e: Throwable) {
3838
if (e.isJodaDateTimeException()) throw IllegalArgumentException(e)
3939
throw e
@@ -68,27 +68,23 @@ public actual fun LocalDate.plus(value: Long, unit: DateTimeUnit.DateBased): Loc
6868

6969
private fun LocalDate.plusNumber(value: Number, unit: DateTimeUnit.DateBased): LocalDate =
7070
try {
71-
jsTry {
72-
when (unit) {
73-
is DateTimeUnit.DayBased -> this.value.plusDays((value.toDouble() * unit.days).toInt())
74-
is DateTimeUnit.MonthBased -> this.value.plusMonths((value.toDouble() * unit.months).toInt())
75-
}.let(::LocalDate)
76-
}
71+
when (unit) {
72+
is DateTimeUnit.DayBased -> this.value.plusDays((value.toDouble() * unit.days).toInt())
73+
is DateTimeUnit.MonthBased -> this.value.plusMonths((value.toDouble() * unit.months).toInt())
74+
}.let(::LocalDate)
7775
} catch (e: Throwable) {
7876
if (!e.isJodaDateTimeException() && !e.isJodaArithmeticException()) throw e
7977
throw DateTimeArithmeticException("The result of adding $value of $unit to $this is out of LocalDate range.", e)
8078
}
8179

8280

8381
public actual operator fun LocalDate.plus(period: DatePeriod): LocalDate = try {
84-
jsTry {
85-
with(period) {
86-
return@with value
87-
.run { if (totalMonths != 0) plusMonths(totalMonths) else this }
88-
.run { if (days != 0) plusDays(days) else this }
82+
with(period) {
83+
return@with value
84+
.run { if (totalMonths != 0) plusMonths(totalMonths) else this }
85+
.run { if (days != 0) plusDays(days) else this }
8986

90-
}.let(::LocalDate)
91-
}
87+
}.let(::LocalDate)
9288
} catch (e: Throwable) {
9389
if (e.isJodaDateTimeException() || e.isJodaArithmeticException()) throw DateTimeArithmeticException(e)
9490
throw e

core/jsAndWasmShared/src/LocalDateTime.kt

+2-6
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ public actual class LocalDateTime internal constructor(internal val value: jtLoc
1313

1414
public actual constructor(year: Int, monthNumber: Int, dayOfMonth: Int, hour: Int, minute: Int, second: Int, nanosecond: Int) :
1515
this(try {
16-
jsTry {
17-
jtLocalDateTime.of(year, monthNumber, dayOfMonth, hour, minute, second, nanosecond)
18-
}
16+
jtLocalDateTime.of(year, monthNumber, dayOfMonth, hour, minute, second, nanosecond)
1917
} catch (e: Throwable) {
2018
if (e.isJodaDateTimeException()) throw IllegalArgumentException(e)
2119
throw e
@@ -54,9 +52,7 @@ public actual class LocalDateTime internal constructor(internal val value: jtLoc
5452

5553
public actual companion object {
5654
public actual fun parse(isoString: String): LocalDateTime = try {
57-
jsTry {
58-
jtLocalDateTime.parse(isoString).let(::LocalDateTime)
59-
}
55+
jtLocalDateTime.parse(isoString).let(::LocalDateTime)
6056
} catch (e: Throwable) {
6157
if (e.isJodaDateTimeParseException()) throw DateTimeFormatException(e)
6258
throw e

core/jsAndWasmShared/src/LocalTime.kt

+8-18
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ public actual class LocalTime internal constructor(internal val value: jtLocalTi
1616
public actual constructor(hour: Int, minute: Int, second: Int, nanosecond: Int) :
1717
this(
1818
try {
19-
jsTry {
20-
jtLocalTime.of(hour, minute, second, nanosecond)
21-
}
19+
jtLocalTime.of(hour, minute, second, nanosecond)
2220
} catch (e: Throwable) {
2321
if (e.isJodaDateTimeException()) throw IllegalArgumentException(e)
2422
throw e
@@ -34,45 +32,37 @@ public actual class LocalTime internal constructor(internal val value: jtLocalTi
3432
public actual fun toNanosecondOfDay(): Long = value.toNanoOfDay().toLong()
3533

3634
override fun equals(other: Any?): Boolean =
37-
(this === other) || (other is LocalTime && (this.value === other.value || this.value.equals(other.value)))
35+
(this === other) || (other is LocalTime && this.value == other.value)
3836

39-
override fun hashCode(): Int = value.hashCode().toInt()
37+
override fun hashCode(): Int = value.hashCode()
4038

4139
actual override fun toString(): String = value.toString()
4240

43-
actual override fun compareTo(other: LocalTime): Int = this.value.compareTo(other.value).toInt()
41+
actual override fun compareTo(other: LocalTime): Int = this.value.compareTo(other.value)
4442

4543
public actual companion object {
4644
public actual fun parse(isoString: String): LocalTime = try {
47-
jsTry {
48-
jtLocalTime.parse(isoString).let(::LocalTime)
49-
}
45+
jtLocalTime.parse(isoString).let(::LocalTime)
5046
} catch (e: Throwable) {
5147
if (e.isJodaDateTimeParseException()) throw DateTimeFormatException(e)
5248
throw e
5349
}
5450

5551
public actual fun fromSecondOfDay(secondOfDay: Int): LocalTime = try {
56-
jsTry {
57-
jtLocalTime.ofSecondOfDay(secondOfDay, 0).let(::LocalTime)
58-
}
52+
jtLocalTime.ofSecondOfDay(secondOfDay, 0).let(::LocalTime)
5953
} catch (e: Throwable) {
6054
throw IllegalArgumentException(e)
6155
}
6256

6357
public actual fun fromMillisecondOfDay(millisecondOfDay: Int): LocalTime = try {
64-
jsTry {
65-
jtLocalTime.ofNanoOfDay(millisecondOfDay * 1_000_000.0).let(::LocalTime)
66-
}
58+
jtLocalTime.ofNanoOfDay(millisecondOfDay * 1_000_000.0).let(::LocalTime)
6759
} catch (e: Throwable) {
6860
throw IllegalArgumentException(e)
6961
}
7062

7163
public actual fun fromNanosecondOfDay(nanosecondOfDay: Long): LocalTime = try {
7264
// number of nanoseconds in a day is much less than `Number.MAX_SAFE_INTEGER`.
73-
jsTry {
74-
jtLocalTime.ofNanoOfDay(nanosecondOfDay.toDouble()).let(::LocalTime)
75-
}
65+
jtLocalTime.ofNanoOfDay(nanosecondOfDay.toDouble()).let(::LocalTime)
7666
} catch (e: Throwable) {
7767
throw IllegalArgumentException(e)
7868
}

core/jsAndWasmShared/src/TimeZone.kt

+3-9
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ public actual open class TimeZone internal constructor(internal val zoneId: Joda
3333
public actual val UTC: FixedOffsetTimeZone = UtcOffset(jtZoneOffset.UTC).asTimeZone()
3434

3535
public actual fun of(zoneId: String): TimeZone = try {
36-
jsTry {
37-
ofZone(JodaTimeZoneId.of(zoneId))
38-
}
36+
ofZone(JodaTimeZoneId.of(zoneId))
3937
} catch (e: Throwable) {
4038
if (e.isJodaDateTimeException()) throw IllegalTimeZoneException(e)
4139
throw e
@@ -66,18 +64,14 @@ internal constructor(public actual val offset: UtcOffset, zoneId: JodaTimeZoneId
6664
}
6765

6866
public actual fun Instant.toLocalDateTime(timeZone: TimeZone): LocalDateTime = try {
69-
jsTry {
70-
JodaTimeLocalDateTime.ofInstant(this.value, timeZone.zoneId).let(::LocalDateTime)
71-
}
67+
JodaTimeLocalDateTime.ofInstant(this.value, timeZone.zoneId).let(::LocalDateTime)
7268
} catch (e: Throwable) {
7369
if (e.isJodaDateTimeException()) throw DateTimeArithmeticException(e)
7470
throw e
7571
}
7672

7773
internal actual fun Instant.toLocalDateTime(offset: UtcOffset): LocalDateTime = try {
78-
jsTry {
79-
JodaTimeLocalDateTime.ofInstant(this.value, offset.zoneOffset).let(::LocalDateTime)
80-
}
74+
JodaTimeLocalDateTime.ofInstant(this.value, offset.zoneOffset).let(::LocalDateTime)
8175
} catch (e: Throwable) {
8276
if (e.isJodaDateTimeException()) throw DateTimeArithmeticException(e)
8377
throw e

0 commit comments

Comments
 (0)