5
5
6
6
package kotlinx.datetime
7
7
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
13
14
import kotlinx.datetime.internal.safeAdd
14
15
import kotlinx.datetime.internal.*
15
16
import kotlinx.datetime.serializers.InstantIso8601Serializer
@@ -41,7 +42,7 @@ public actual class Instant internal constructor(internal val value: jtInstant)
41
42
internal fun plusFix (seconds : Double , nanos : Int ): jtInstant {
42
43
val newSeconds = value.epochSecond() + seconds
43
44
val newNanos = value.nano() + nanos
44
- return jtInstant.ofEpochSecond(newSeconds, newNanos.toInt())
45
+ return jsTry { jtInstant.ofEpochSecond(newSeconds, newNanos.toInt()) }
45
46
}
46
47
47
48
public actual operator fun minus (duration : Duration ): Instant = plus(- duration)
@@ -54,7 +55,7 @@ public actual class Instant internal constructor(internal val value: jtInstant)
54
55
public actual override operator fun compareTo (other : Instant ): Int = this .value.compareTo(other.value)
55
56
56
57
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)) )
58
59
59
60
override fun hashCode (): Int = value.hashCode()
60
61
@@ -73,7 +74,7 @@ public actual class Instant internal constructor(internal val value: jtInstant)
73
74
}
74
75
75
76
public actual fun parse (isoString : String ): Instant = try {
76
- Instant (jtOffsetDateTime.parse(fixOffsetRepresentation(isoString)).toInstant())
77
+ Instant (jsTry { jtOffsetDateTime.parse(fixOffsetRepresentation(isoString)) } .toInstant())
77
78
} catch (e: Throwable ) {
78
79
if (e.isJodaDateTimeParseException()) throw DateTimeFormatException (e)
79
80
throw e
@@ -96,21 +97,21 @@ public actual class Instant internal constructor(internal val value: jtInstant)
96
97
Instant.fromEpochSeconds(0, Long.MAX_VALUE).nanosecondsOfSecond) */
97
98
val secs = safeAdd(epochSeconds, nanosecondAdjustment.floorDiv(NANOS_PER_ONE .toLong()))
98
99
val nos = nanosecondAdjustment.mod(NANOS_PER_ONE .toLong()).toInt()
99
- Instant (jtInstant.ofEpochSecond(secs.toDouble(), nos))
100
+ Instant (jsTry { jtInstant.ofEpochSecond(secs.toDouble(), nos) } )
100
101
} catch (e: Throwable ) {
101
102
if (! e.isJodaDateTimeException() && e !is ArithmeticException ) throw e
102
103
if (epochSeconds > 0 ) MAX else MIN
103
104
}
104
105
105
106
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) } )
107
108
} catch (e: Throwable ) {
108
109
if (! e.isJodaDateTimeException()) throw e
109
110
if (epochSeconds > 0 ) MAX else MIN
110
111
}
111
112
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 ) } )
114
115
115
116
internal actual val MIN : Instant = Instant (jtInstant.MIN )
116
117
internal actual val MAX : Instant = Instant (jtInstant.MAX )
@@ -119,23 +120,23 @@ public actual class Instant internal constructor(internal val value: jtInstant)
119
120
120
121
121
122
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) }
123
124
with (period) {
124
125
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 }
131
132
}.toInstant().let (::Instant )
132
133
} catch (e: Throwable ) {
133
134
if (e.isJodaDateTimeException()) throw DateTimeArithmeticException (e)
134
135
throw e
135
136
}
136
137
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) } }
139
140
140
141
@Deprecated(" Use the plus overload with an explicit number of units" , ReplaceWith (" this.plus(1, unit, timeZone)" ))
141
142
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
149
150
plus(value, unit).value.checkZone(timeZone)
150
151
}
151
152
is DateTimeUnit .DayBased ->
152
- thisZdt.plusDays(value.toDouble() * unit.days).toInstant()
153
+ jsTry { thisZdt.plusDays(value.toDouble() * unit.days) } .toInstant()
153
154
is DateTimeUnit .MonthBased ->
154
- thisZdt.plusMonths(value.toDouble() * unit.months).toInstant()
155
+ jsTry { thisZdt.plusMonths(value.toDouble() * unit.months) } .toInstant()
155
156
}.let (::Instant )
156
157
} catch (e: Throwable ) {
157
158
if (e.isJodaDateTimeException()) throw DateTimeArithmeticException (e)
@@ -165,9 +166,9 @@ public actual fun Instant.plus(value: Int, unit: DateTimeUnit, timeZone: TimeZon
165
166
is DateTimeUnit .TimeBased ->
166
167
plus(value.toLong(), unit).value.checkZone(timeZone)
167
168
is DateTimeUnit .DayBased ->
168
- thisZdt.plusDays(value.toDouble() * unit.days).toInstant()
169
+ jsTry { thisZdt.plusDays(value.toDouble() * unit.days) } .toInstant()
169
170
is DateTimeUnit .MonthBased ->
170
- thisZdt.plusMonths(value.toDouble() * unit.months).toInstant()
171
+ jsTry { thisZdt.plusMonths(value.toDouble() * unit.months) } .toInstant()
171
172
}.let (::Instant )
172
173
} catch (e: Throwable ) {
173
174
if (e.isJodaDateTimeException()) throw DateTimeArithmeticException (e)
@@ -193,11 +194,11 @@ public actual fun Instant.plus(value: Long, unit: DateTimeUnit.TimeBased): Insta
193
194
}
194
195
195
196
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) }
198
199
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) }
201
202
val nanoseconds = thisZdt.until(otherZdt, jtChronoUnit.NANOS )
202
203
203
204
buildDateTimePeriod(months.toInt(), days.toInt(), nanoseconds.toLong())
0 commit comments