Skip to content

Commit

Permalink
REMOVE REFACTORING
Browse files Browse the repository at this point in the history
  • Loading branch information
igoriakovlev committed Nov 30, 2023
1 parent 6d83166 commit d6afe30
Show file tree
Hide file tree
Showing 14 changed files with 82 additions and 617 deletions.
2 changes: 1 addition & 1 deletion core/commonJs/src/DayOfWeek.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

package kotlinx.datetime

import kotlinx.datetime.internal.JodaTimeDayOfWeek as jsDayOfWeek
import kotlinx.datetime.internal.JSJoda.DayOfWeek as jsDayOfWeek

public actual enum class DayOfWeek {
MONDAY,
Expand Down
59 changes: 30 additions & 29 deletions core/commonJs/src/Instant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@

package kotlinx.datetime

import kotlinx.datetime.internal.JodaTimeInstant as jtInstant
import kotlinx.datetime.internal.JodaTimeOffsetDateTime as jtOffsetDateTime
import kotlinx.datetime.internal.JodaTimeDuration as jtDuration
import kotlinx.datetime.internal.JodaTimeClock as jtClock
import kotlinx.datetime.internal.JodaTimeChronoUnit as jtChronoUnit
import kotlinx.datetime.internal.JSJoda.Instant as jtInstant
import kotlinx.datetime.internal.JSJoda.OffsetDateTime as jtOffsetDateTime
import kotlinx.datetime.internal.JSJoda.Duration as jtDuration
import kotlinx.datetime.internal.JSJoda.Clock as jtClock
import kotlinx.datetime.internal.JSJoda.ChronoUnit as jtChronoUnit
import kotlinx.datetime.internal.JSJoda.ZonedDateTime as jtZonedDateTime
import kotlinx.datetime.internal.safeAdd
import kotlinx.datetime.internal.*
import kotlinx.datetime.serializers.InstantIso8601Serializer
Expand Down Expand Up @@ -41,7 +42,7 @@ public actual class Instant internal constructor(internal val value: jtInstant)
internal fun plusFix(seconds: Double, nanos: Int): jtInstant {
val newSeconds = value.epochSecond() + seconds
val newNanos = value.nano() + nanos
return jtInstant.ofEpochSecond(newSeconds, newNanos.toInt())
return jsTry { jtInstant.ofEpochSecond(newSeconds, newNanos.toInt()) }
}

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

override fun equals(other: Any?): Boolean =
(this === other) || (other is Instant && this.value == other.value)
(this === other) || (other is Instant && (this.value === other.value || this.value.equals(other.value)))

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

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

public actual fun parse(isoString: String): Instant = try {
Instant(jtOffsetDateTime.parse(fixOffsetRepresentation(isoString)).toInstant())
Instant(jsTry { jtOffsetDateTime.parse(fixOffsetRepresentation(isoString)) }.toInstant())
} catch (e: Throwable) {
if (e.isJodaDateTimeParseException()) throw DateTimeFormatException(e)
throw e
Expand All @@ -96,21 +97,21 @@ public actual class Instant internal constructor(internal val value: jtInstant)
Instant.fromEpochSeconds(0, Long.MAX_VALUE).nanosecondsOfSecond) */
val secs = safeAdd(epochSeconds, nanosecondAdjustment.floorDiv(NANOS_PER_ONE.toLong()))
val nos = nanosecondAdjustment.mod(NANOS_PER_ONE.toLong()).toInt()
Instant(jtInstant.ofEpochSecond(secs.toDouble(), nos))
Instant(jsTry { jtInstant.ofEpochSecond(secs.toDouble(), nos) })
} catch (e: Throwable) {
if (!e.isJodaDateTimeException() && e !is ArithmeticException) throw e
if (epochSeconds > 0) MAX else MIN
}

public actual fun fromEpochSeconds(epochSeconds: Long, nanosecondAdjustment: Int): Instant = try {
Instant(jtInstant.ofEpochSecond(epochSeconds.toDouble(), nanosecondAdjustment))
Instant(jsTry { jtInstant.ofEpochSecond(epochSeconds.toDouble(), nanosecondAdjustment) })
} catch (e: Throwable) {
if (!e.isJodaDateTimeException()) throw e
if (epochSeconds > 0) MAX else MIN
}

public actual val DISTANT_PAST: Instant = Instant(jtInstant.ofEpochSecond(DISTANT_PAST_SECONDS.toDouble(), 999_999_999))
public actual val DISTANT_FUTURE: Instant = Instant(jtInstant.ofEpochSecond(DISTANT_FUTURE_SECONDS.toDouble(), 0))
public actual val DISTANT_PAST: Instant = Instant(jsTry { jtInstant.ofEpochSecond(DISTANT_PAST_SECONDS.toDouble(), 999_999_999) })
public actual val DISTANT_FUTURE: Instant = Instant(jsTry { jtInstant.ofEpochSecond(DISTANT_FUTURE_SECONDS.toDouble(), 0) })

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


public actual fun Instant.plus(period: DateTimePeriod, timeZone: TimeZone): Instant = try {
val thisZdt = this.value.atZone(timeZone.zoneId)
val thisZdt = jsTry { this.value.atZone(timeZone.zoneId) }
with(period) {
thisZdt
.run { if (totalMonths != 0) plusMonths(totalMonths) else this }
.run { if (days != 0) plusDays(days) else this }
.run { if (hours != 0) plusHours(hours) else this }
.run { if (minutes != 0) plusMinutes(minutes) else this }
.run { if (seconds != 0) plusSeconds(seconds) else this }
.run { if (nanoseconds != 0) plusNanos(nanoseconds.toDouble()) else this }
.run { if (totalMonths != 0) jsTry { plusMonths(totalMonths) } else this }
.run { if (days != 0) jsTry { plusDays(days) } else this }
.run { if (hours != 0) jsTry { plusHours(hours) } else this }
.run { if (minutes != 0) jsTry { plusMinutes(minutes) } else this }
.run { if (seconds != 0) jsTry { plusSeconds(seconds) } else this }
.run { if (nanoseconds != 0) jsTry { plusNanos(nanoseconds.toDouble()) } else this }
}.toInstant().let(::Instant)
} catch (e: Throwable) {
if (e.isJodaDateTimeException()) throw DateTimeArithmeticException(e)
throw e
}

private fun Instant.atZone(zone: TimeZone): JodaTimeZonedDateTime = value.atZone(zone.zoneId)
private fun jtInstant.checkZone(zone: TimeZone): jtInstant = apply { atZone(zone.zoneId) }
private fun Instant.atZone(zone: TimeZone): jtZonedDateTime = jsTry { value.atZone(zone.zoneId) }
private fun jtInstant.checkZone(zone: TimeZone): jtInstant = apply { jsTry { atZone(zone.zoneId) } }

@Deprecated("Use the plus overload with an explicit number of units", ReplaceWith("this.plus(1, unit, timeZone)"))
public actual fun Instant.plus(unit: DateTimeUnit, timeZone: TimeZone): Instant =
Expand All @@ -149,9 +150,9 @@ public actual fun Instant.plus(value: Long, unit: DateTimeUnit, timeZone: TimeZo
plus(value, unit).value.checkZone(timeZone)
}
is DateTimeUnit.DayBased ->
thisZdt.plusDays(value.toDouble() * unit.days).toInstant()
jsTry {thisZdt.plusDays(value.toDouble() * unit.days) }.toInstant()
is DateTimeUnit.MonthBased ->
thisZdt.plusMonths(value.toDouble() * unit.months).toInstant()
jsTry { thisZdt.plusMonths(value.toDouble() * unit.months) }.toInstant()
}.let(::Instant)
} catch (e: Throwable) {
if (e.isJodaDateTimeException()) throw DateTimeArithmeticException(e)
Expand All @@ -165,9 +166,9 @@ public actual fun Instant.plus(value: Int, unit: DateTimeUnit, timeZone: TimeZon
is DateTimeUnit.TimeBased ->
plus(value.toLong(), unit).value.checkZone(timeZone)
is DateTimeUnit.DayBased ->
thisZdt.plusDays(value.toDouble() * unit.days).toInstant()
jsTry { thisZdt.plusDays(value.toDouble() * unit.days) }.toInstant()
is DateTimeUnit.MonthBased ->
thisZdt.plusMonths(value.toDouble() * unit.months).toInstant()
jsTry { thisZdt.plusMonths(value.toDouble() * unit.months) }.toInstant()
}.let(::Instant)
} catch (e: Throwable) {
if (e.isJodaDateTimeException()) throw DateTimeArithmeticException(e)
Expand All @@ -193,11 +194,11 @@ public actual fun Instant.plus(value: Long, unit: DateTimeUnit.TimeBased): Insta
}

public actual fun Instant.periodUntil(other: Instant, timeZone: TimeZone): DateTimePeriod = try {
var thisZdt = this.value.atZone(timeZone.zoneId)
val otherZdt = other.value.atZone(timeZone.zoneId)
var thisZdt = jsTry { this.value.atZone(timeZone.zoneId) }
val otherZdt = jsTry { other.value.atZone(timeZone.zoneId) }

val months = thisZdt.until(otherZdt, jtChronoUnit.MONTHS); thisZdt = thisZdt.plusMonths(months)
val days = thisZdt.until(otherZdt, jtChronoUnit.DAYS); thisZdt = thisZdt.plusDays(days)
val months = thisZdt.until(otherZdt, jtChronoUnit.MONTHS); thisZdt = jsTry { thisZdt.plusMonths(months) }
val days = thisZdt.until(otherZdt, jtChronoUnit.DAYS); thisZdt = jsTry { thisZdt.plusDays(days) }
val nanoseconds = thisZdt.until(otherZdt, jtChronoUnit.NANOS)

buildDateTimePeriod(months.toInt(), days.toInt(), nanoseconds.toLong())
Expand Down
4 changes: 3 additions & 1 deletion core/commonJs/src/JSJodaExceptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ internal fun Throwable.isJodaArithmeticException(): Boolean = hasJsExceptionName
internal fun Throwable.isJodaDateTimeException(): Boolean = hasJsExceptionName("DateTimeException")
internal fun Throwable.isJodaDateTimeParseException(): Boolean = hasJsExceptionName("DateTimeParseException")

internal expect fun Throwable.hasJsExceptionName(name: String): Boolean
internal expect fun Throwable.hasJsExceptionName(name: String): Boolean

internal expect inline fun <reified T : Any> jsTry(crossinline body: () -> T): T
229 changes: 0 additions & 229 deletions core/commonJs/src/JsJodaPlatform.kt

This file was deleted.

0 comments on commit d6afe30

Please sign in to comment.