55
66package kotlinx.datetime
77
8- import kotlinx.datetime.internal.JSJoda.ZonedDateTime
98import kotlinx.datetime.internal.JSJoda.Instant as jtInstant
109import kotlinx.datetime.internal.JSJoda.OffsetDateTime as jtOffsetDateTime
1110import kotlinx.datetime.internal.JSJoda.Duration as jtDuration
1211import kotlinx.datetime.internal.JSJoda.Clock as jtClock
13- import kotlinx.datetime.internal.JSJoda.ChronoUnit
12+ import kotlinx.datetime.internal.JSJoda.ChronoUnit as jtChronoUnit
13+ import kotlinx.datetime.internal.JSJoda.ZonedDateTime as jtZonedDateTime
1414import kotlinx.datetime.internal.safeAdd
1515import kotlinx.datetime.internal.*
1616import kotlinx.datetime.serializers.InstantIso8601Serializer
@@ -40,24 +40,24 @@ public actual class Instant internal constructor(internal val value: jtInstant)
4040 }
4141
4242 internal fun plusFix (seconds : Double , nanos : Int ): jtInstant {
43- val newSeconds = value.epochSecond().toDouble() + seconds
44- val newNanos = value.nano().toDouble() + nanos
45- return jtInstant.ofEpochSecond(newSeconds, newNanos)
43+ val newSeconds = value.epochSecond() + seconds
44+ val newNanos = value.nano() + nanos
45+ return jsTry { jtInstant.ofEpochSecond(newSeconds, newNanos.toInt()) }
4646 }
4747
4848 public actual operator fun minus (duration : Duration ): Instant = plus(- duration)
4949
5050 public actual operator fun minus (other : Instant ): Duration {
5151 val diff = jtDuration.between(other.value, this .value)
52- return diff.seconds().toDouble(). seconds + diff.nano().toDouble ().nanoseconds
52+ return diff.seconds().seconds + diff.nano().nanoseconds
5353 }
5454
55- public actual override operator fun compareTo (other : Instant ): Int = this .value.compareTo(other.value).toInt()
55+ public actual override operator fun compareTo (other : Instant ): Int = this .value.compareTo(other.value)
5656
5757 override fun equals (other : Any? ): Boolean =
58- (this == = other) || (other is Instant && this .value == other.value)
58+ (this == = other) || (other is Instant && ( this .value == = other.value || this .value.equals(other.value)) )
5959
60- override fun hashCode (): Int = value.hashCode().toInt()
60+ override fun hashCode (): Int = value.hashCode()
6161
6262 actual override fun toString (): String = value.toString()
6363
@@ -74,7 +74,7 @@ public actual class Instant internal constructor(internal val value: jtInstant)
7474 }
7575
7676 public actual fun parse (isoString : String ): Instant = try {
77- Instant (jtOffsetDateTime.parse(fixOffsetRepresentation(isoString)).toInstant())
77+ Instant (jsTry { jtOffsetDateTime.parse(fixOffsetRepresentation(isoString)) } .toInstant())
7878 } catch (e: Throwable ) {
7979 if (e.isJodaDateTimeParseException()) throw DateTimeFormatException (e)
8080 throw e
@@ -97,21 +97,21 @@ public actual class Instant internal constructor(internal val value: jtInstant)
9797 Instant.fromEpochSeconds(0, Long.MAX_VALUE).nanosecondsOfSecond) */
9898 val secs = safeAdd(epochSeconds, nanosecondAdjustment.floorDiv(NANOS_PER_ONE .toLong()))
9999 val nos = nanosecondAdjustment.mod(NANOS_PER_ONE .toLong()).toInt()
100- Instant (jtInstant.ofEpochSecond(secs, nos))
100+ Instant (jsTry { jtInstant.ofEpochSecond(secs.toDouble() , nos) } )
101101 } catch (e: Throwable ) {
102102 if (! e.isJodaDateTimeException() && e !is ArithmeticException ) throw e
103103 if (epochSeconds > 0 ) MAX else MIN
104104 }
105105
106106 public actual fun fromEpochSeconds (epochSeconds : Long , nanosecondAdjustment : Int ): Instant = try {
107- Instant (jtInstant.ofEpochSecond(epochSeconds, nanosecondAdjustment))
107+ Instant (jsTry { jtInstant.ofEpochSecond(epochSeconds.toDouble() , nanosecondAdjustment) } )
108108 } catch (e: Throwable ) {
109109 if (! e.isJodaDateTimeException()) throw e
110110 if (epochSeconds > 0 ) MAX else MIN
111111 }
112112
113- public actual val DISTANT_PAST : Instant = Instant (jtInstant.ofEpochSecond(DISTANT_PAST_SECONDS , 999_999_999 ))
114- public actual val DISTANT_FUTURE : Instant = Instant (jtInstant.ofEpochSecond(DISTANT_FUTURE_SECONDS , 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 ) } )
115115
116116 internal actual val MIN : Instant = Instant (jtInstant.MIN )
117117 internal actual val MAX : Instant = Instant (jtInstant.MAX )
@@ -120,23 +120,23 @@ public actual class Instant internal constructor(internal val value: jtInstant)
120120
121121
122122public actual fun Instant.plus (period : DateTimePeriod , timeZone : TimeZone ): Instant = try {
123- val thisZdt = this .value.atZone(timeZone.zoneId)
123+ val thisZdt = jsTry { this .value.atZone(timeZone.zoneId) }
124124 with (period) {
125125 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 }
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 }
132132 }.toInstant().let (::Instant )
133133} catch (e: Throwable ) {
134134 if (e.isJodaDateTimeException()) throw DateTimeArithmeticException (e)
135135 throw e
136136}
137137
138- private fun Instant.atZone (zone : TimeZone ): ZonedDateTime = value.atZone(zone.zoneId)
139- 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) } }
140140
141141@Deprecated(" Use the plus overload with an explicit number of units" , ReplaceWith (" this.plus(1, unit, timeZone)" ))
142142public actual fun Instant.plus (unit : DateTimeUnit , timeZone : TimeZone ): Instant =
@@ -150,9 +150,9 @@ public actual fun Instant.plus(value: Long, unit: DateTimeUnit, timeZone: TimeZo
150150 plus(value, unit).value.checkZone(timeZone)
151151 }
152152 is DateTimeUnit .DayBased ->
153- thisZdt.plusDays(value.toDouble() * unit.days).toInstant()
153+ jsTry { thisZdt.plusDays(value.toDouble() * unit.days) } .toInstant()
154154 is DateTimeUnit .MonthBased ->
155- thisZdt.plusMonths(value.toDouble() * unit.months).toInstant()
155+ jsTry { thisZdt.plusMonths(value.toDouble() * unit.months) } .toInstant()
156156 }.let (::Instant )
157157 } catch (e: Throwable ) {
158158 if (e.isJodaDateTimeException()) throw DateTimeArithmeticException (e)
@@ -166,9 +166,9 @@ public actual fun Instant.plus(value: Int, unit: DateTimeUnit, timeZone: TimeZon
166166 is DateTimeUnit .TimeBased ->
167167 plus(value.toLong(), unit).value.checkZone(timeZone)
168168 is DateTimeUnit .DayBased ->
169- thisZdt.plusDays(value.toDouble() * unit.days).toInstant()
169+ jsTry { thisZdt.plusDays(value.toDouble() * unit.days) } .toInstant()
170170 is DateTimeUnit .MonthBased ->
171- thisZdt.plusMonths(value.toDouble() * unit.months).toInstant()
171+ jsTry { thisZdt.plusMonths(value.toDouble() * unit.months) } .toInstant()
172172 }.let (::Instant )
173173 } catch (e: Throwable ) {
174174 if (e.isJodaDateTimeException()) throw DateTimeArithmeticException (e)
@@ -194,12 +194,12 @@ public actual fun Instant.plus(value: Long, unit: DateTimeUnit.TimeBased): Insta
194194 }
195195
196196public actual fun Instant.periodUntil (other : Instant , timeZone : TimeZone ): DateTimePeriod = try {
197- var thisZdt = this .value.atZone(timeZone.zoneId)
198- 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) }
199199
200- val months = thisZdt.until(otherZdt, ChronoUnit .MONTHS ).toDouble() ; thisZdt = thisZdt.plusMonths(months)
201- val days = thisZdt.until(otherZdt, ChronoUnit .DAYS ).toDouble() ; thisZdt = thisZdt.plusDays(days)
202- val nanoseconds = thisZdt.until(otherZdt, ChronoUnit .NANOS ).toDouble( )
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) }
202+ val nanoseconds = thisZdt.until(otherZdt, jtChronoUnit .NANOS )
203203
204204 buildDateTimePeriod(months.toInt(), days.toInt(), nanoseconds.toLong())
205205} catch (e: Throwable ) {
@@ -211,8 +211,8 @@ public actual fun Instant.until(other: Instant, unit: DateTimeUnit, timeZone: Ti
211211 val otherZdt = other.atZone(timeZone)
212212 when (unit) {
213213 is DateTimeUnit .TimeBased -> until(other, unit)
214- is DateTimeUnit .DayBased -> (thisZdt.until(otherZdt, ChronoUnit .DAYS ).toDouble( ) / unit.days).toLong()
215- is DateTimeUnit .MonthBased -> (thisZdt.until(otherZdt, ChronoUnit .MONTHS ).toDouble( ) / unit.months).toLong()
214+ is DateTimeUnit .DayBased -> (thisZdt.until(otherZdt, jtChronoUnit .DAYS ) / unit.days).toLong()
215+ is DateTimeUnit .MonthBased -> (thisZdt.until(otherZdt, jtChronoUnit .MONTHS ) / unit.months).toLong()
216216 }
217217} catch (e: ArithmeticException ) {
218218 if (this < other) Long .MAX_VALUE else Long .MIN_VALUE
@@ -221,4 +221,4 @@ public actual fun Instant.until(other: Instant, unit: DateTimeUnit, timeZone: Ti
221221}
222222
223223internal actual fun Instant.toStringWithOffset (offset : UtcOffset ): String =
224- jtOffsetDateTime.ofInstant(this .value, offset.zoneOffset).toString()
224+ jtOffsetDateTime.ofInstant(this .value, offset.zoneOffset).toString()
0 commit comments