Skip to content

Commit d6afe30

Browse files
committed
REMOVE REFACTORING
1 parent 6d83166 commit d6afe30

14 files changed

+82
-617
lines changed

core/commonJs/src/DayOfWeek.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
package kotlinx.datetime
77

8-
import kotlinx.datetime.internal.JodaTimeDayOfWeek as jsDayOfWeek
8+
import kotlinx.datetime.internal.JSJoda.DayOfWeek as jsDayOfWeek
99

1010
public actual enum class DayOfWeek {
1111
MONDAY,

core/commonJs/src/Instant.kt

+30-29
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55

66
package kotlinx.datetime
77

8-
import kotlinx.datetime.internal.JodaTimeInstant as jtInstant
9-
import kotlinx.datetime.internal.JodaTimeOffsetDateTime as jtOffsetDateTime
10-
import kotlinx.datetime.internal.JodaTimeDuration as jtDuration
11-
import kotlinx.datetime.internal.JodaTimeClock as jtClock
12-
import kotlinx.datetime.internal.JodaTimeChronoUnit as jtChronoUnit
8+
import kotlinx.datetime.internal.JSJoda.Instant as jtInstant
9+
import kotlinx.datetime.internal.JSJoda.OffsetDateTime as jtOffsetDateTime
10+
import kotlinx.datetime.internal.JSJoda.Duration as jtDuration
11+
import kotlinx.datetime.internal.JSJoda.Clock as jtClock
12+
import kotlinx.datetime.internal.JSJoda.ChronoUnit as jtChronoUnit
13+
import kotlinx.datetime.internal.JSJoda.ZonedDateTime as jtZonedDateTime
1314
import kotlinx.datetime.internal.safeAdd
1415
import kotlinx.datetime.internal.*
1516
import kotlinx.datetime.serializers.InstantIso8601Serializer
@@ -41,7 +42,7 @@ public actual class Instant internal constructor(internal val value: jtInstant)
4142
internal fun plusFix(seconds: Double, nanos: Int): jtInstant {
4243
val newSeconds = value.epochSecond() + seconds
4344
val newNanos = value.nano() + nanos
44-
return jtInstant.ofEpochSecond(newSeconds, newNanos.toInt())
45+
return jsTry { jtInstant.ofEpochSecond(newSeconds, newNanos.toInt()) }
4546
}
4647

4748
public actual operator fun minus(duration: Duration): Instant = plus(-duration)
@@ -54,7 +55,7 @@ public actual class Instant internal constructor(internal val value: jtInstant)
5455
public actual override operator fun compareTo(other: Instant): Int = this.value.compareTo(other.value)
5556

5657
override fun equals(other: Any?): Boolean =
57-
(this === other) || (other is Instant && this.value == other.value)
58+
(this === other) || (other is Instant && (this.value === other.value || this.value.equals(other.value)))
5859

5960
override fun hashCode(): Int = value.hashCode()
6061

@@ -73,7 +74,7 @@ public actual class Instant internal constructor(internal val value: jtInstant)
7374
}
7475

7576
public actual fun parse(isoString: String): Instant = try {
76-
Instant(jtOffsetDateTime.parse(fixOffsetRepresentation(isoString)).toInstant())
77+
Instant(jsTry { jtOffsetDateTime.parse(fixOffsetRepresentation(isoString)) }.toInstant())
7778
} catch (e: Throwable) {
7879
if (e.isJodaDateTimeParseException()) throw DateTimeFormatException(e)
7980
throw e
@@ -96,21 +97,21 @@ public actual class Instant internal constructor(internal val value: jtInstant)
9697
Instant.fromEpochSeconds(0, Long.MAX_VALUE).nanosecondsOfSecond) */
9798
val secs = safeAdd(epochSeconds, nanosecondAdjustment.floorDiv(NANOS_PER_ONE.toLong()))
9899
val nos = nanosecondAdjustment.mod(NANOS_PER_ONE.toLong()).toInt()
99-
Instant(jtInstant.ofEpochSecond(secs.toDouble(), nos))
100+
Instant(jsTry { jtInstant.ofEpochSecond(secs.toDouble(), nos) })
100101
} catch (e: Throwable) {
101102
if (!e.isJodaDateTimeException() && e !is ArithmeticException) throw e
102103
if (epochSeconds > 0) MAX else MIN
103104
}
104105

105106
public actual fun fromEpochSeconds(epochSeconds: Long, nanosecondAdjustment: Int): Instant = try {
106-
Instant(jtInstant.ofEpochSecond(epochSeconds.toDouble(), nanosecondAdjustment))
107+
Instant(jsTry { jtInstant.ofEpochSecond(epochSeconds.toDouble(), nanosecondAdjustment) })
107108
} catch (e: Throwable) {
108109
if (!e.isJodaDateTimeException()) throw e
109110
if (epochSeconds > 0) MAX else MIN
110111
}
111112

112-
public actual val DISTANT_PAST: Instant = Instant(jtInstant.ofEpochSecond(DISTANT_PAST_SECONDS.toDouble(), 999_999_999))
113-
public actual val DISTANT_FUTURE: Instant = Instant(jtInstant.ofEpochSecond(DISTANT_FUTURE_SECONDS.toDouble(), 0))
113+
public actual val DISTANT_PAST: Instant = Instant(jsTry { jtInstant.ofEpochSecond(DISTANT_PAST_SECONDS.toDouble(), 999_999_999) })
114+
public actual val DISTANT_FUTURE: Instant = Instant(jsTry { jtInstant.ofEpochSecond(DISTANT_FUTURE_SECONDS.toDouble(), 0) })
114115

115116
internal actual val MIN: Instant = Instant(jtInstant.MIN)
116117
internal actual val MAX: Instant = Instant(jtInstant.MAX)
@@ -119,23 +120,23 @@ public actual class Instant internal constructor(internal val value: jtInstant)
119120

120121

121122
public actual fun Instant.plus(period: DateTimePeriod, timeZone: TimeZone): Instant = try {
122-
val thisZdt = this.value.atZone(timeZone.zoneId)
123+
val thisZdt = jsTry { this.value.atZone(timeZone.zoneId) }
123124
with(period) {
124125
thisZdt
125-
.run { if (totalMonths != 0) plusMonths(totalMonths) else this }
126-
.run { if (days != 0) plusDays(days) else this }
127-
.run { if (hours != 0) plusHours(hours) else this }
128-
.run { if (minutes != 0) plusMinutes(minutes) else this }
129-
.run { if (seconds != 0) plusSeconds(seconds) else this }
130-
.run { if (nanoseconds != 0) plusNanos(nanoseconds.toDouble()) else this }
126+
.run { if (totalMonths != 0) jsTry { plusMonths(totalMonths) } else this }
127+
.run { if (days != 0) jsTry { plusDays(days) } else this }
128+
.run { if (hours != 0) jsTry { plusHours(hours) } else this }
129+
.run { if (minutes != 0) jsTry { plusMinutes(minutes) } else this }
130+
.run { if (seconds != 0) jsTry { plusSeconds(seconds) } else this }
131+
.run { if (nanoseconds != 0) jsTry { plusNanos(nanoseconds.toDouble()) } else this }
131132
}.toInstant().let(::Instant)
132133
} catch (e: Throwable) {
133134
if (e.isJodaDateTimeException()) throw DateTimeArithmeticException(e)
134135
throw e
135136
}
136137

137-
private fun Instant.atZone(zone: TimeZone): JodaTimeZonedDateTime = value.atZone(zone.zoneId)
138-
private fun jtInstant.checkZone(zone: TimeZone): jtInstant = apply { atZone(zone.zoneId) }
138+
private fun Instant.atZone(zone: TimeZone): jtZonedDateTime = jsTry { value.atZone(zone.zoneId) }
139+
private fun jtInstant.checkZone(zone: TimeZone): jtInstant = apply { jsTry { atZone(zone.zoneId) } }
139140

140141
@Deprecated("Use the plus overload with an explicit number of units", ReplaceWith("this.plus(1, unit, timeZone)"))
141142
public actual fun Instant.plus(unit: DateTimeUnit, timeZone: TimeZone): Instant =
@@ -149,9 +150,9 @@ public actual fun Instant.plus(value: Long, unit: DateTimeUnit, timeZone: TimeZo
149150
plus(value, unit).value.checkZone(timeZone)
150151
}
151152
is DateTimeUnit.DayBased ->
152-
thisZdt.plusDays(value.toDouble() * unit.days).toInstant()
153+
jsTry {thisZdt.plusDays(value.toDouble() * unit.days) }.toInstant()
153154
is DateTimeUnit.MonthBased ->
154-
thisZdt.plusMonths(value.toDouble() * unit.months).toInstant()
155+
jsTry { thisZdt.plusMonths(value.toDouble() * unit.months) }.toInstant()
155156
}.let(::Instant)
156157
} catch (e: Throwable) {
157158
if (e.isJodaDateTimeException()) throw DateTimeArithmeticException(e)
@@ -165,9 +166,9 @@ public actual fun Instant.plus(value: Int, unit: DateTimeUnit, timeZone: TimeZon
165166
is DateTimeUnit.TimeBased ->
166167
plus(value.toLong(), unit).value.checkZone(timeZone)
167168
is DateTimeUnit.DayBased ->
168-
thisZdt.plusDays(value.toDouble() * unit.days).toInstant()
169+
jsTry { thisZdt.plusDays(value.toDouble() * unit.days) }.toInstant()
169170
is DateTimeUnit.MonthBased ->
170-
thisZdt.plusMonths(value.toDouble() * unit.months).toInstant()
171+
jsTry { thisZdt.plusMonths(value.toDouble() * unit.months) }.toInstant()
171172
}.let(::Instant)
172173
} catch (e: Throwable) {
173174
if (e.isJodaDateTimeException()) throw DateTimeArithmeticException(e)
@@ -193,11 +194,11 @@ public actual fun Instant.plus(value: Long, unit: DateTimeUnit.TimeBased): Insta
193194
}
194195

195196
public actual fun Instant.periodUntil(other: Instant, timeZone: TimeZone): DateTimePeriod = try {
196-
var thisZdt = this.value.atZone(timeZone.zoneId)
197-
val otherZdt = other.value.atZone(timeZone.zoneId)
197+
var thisZdt = jsTry { this.value.atZone(timeZone.zoneId) }
198+
val otherZdt = jsTry { other.value.atZone(timeZone.zoneId) }
198199

199-
val months = thisZdt.until(otherZdt, jtChronoUnit.MONTHS); thisZdt = thisZdt.plusMonths(months)
200-
val days = thisZdt.until(otherZdt, jtChronoUnit.DAYS); thisZdt = thisZdt.plusDays(days)
200+
val months = thisZdt.until(otherZdt, jtChronoUnit.MONTHS); thisZdt = jsTry { thisZdt.plusMonths(months) }
201+
val days = thisZdt.until(otherZdt, jtChronoUnit.DAYS); thisZdt = jsTry { thisZdt.plusDays(days) }
201202
val nanoseconds = thisZdt.until(otherZdt, jtChronoUnit.NANOS)
202203

203204
buildDateTimePeriod(months.toInt(), days.toInt(), nanoseconds.toLong())

core/commonJs/src/JSJodaExceptions.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ internal fun Throwable.isJodaArithmeticException(): Boolean = hasJsExceptionName
99
internal fun Throwable.isJodaDateTimeException(): Boolean = hasJsExceptionName("DateTimeException")
1010
internal fun Throwable.isJodaDateTimeParseException(): Boolean = hasJsExceptionName("DateTimeParseException")
1111

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

core/commonJs/src/JsJodaPlatform.kt

-229
This file was deleted.

0 commit comments

Comments
 (0)