5
5
6
6
package kotlinx.datetime
7
7
8
- import kotlinx.datetime.internal.JSJoda.ZonedDateTime
9
8
import kotlinx.datetime.internal.JSJoda.Instant as jtInstant
10
9
import kotlinx.datetime.internal.JSJoda.OffsetDateTime as jtOffsetDateTime
11
10
import kotlinx.datetime.internal.JSJoda.Duration as jtDuration
12
11
import 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
14
14
import kotlinx.datetime.internal.safeAdd
15
15
import kotlinx.datetime.internal.*
16
16
import kotlinx.datetime.serializers.InstantIso8601Serializer
@@ -40,24 +40,24 @@ public actual class Instant internal constructor(internal val value: jtInstant)
40
40
}
41
41
42
42
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()) }
46
46
}
47
47
48
48
public actual operator fun minus (duration : Duration ): Instant = plus(- duration)
49
49
50
50
public actual operator fun minus (other : Instant ): Duration {
51
51
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
53
53
}
54
54
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)
56
56
57
57
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)) )
59
59
60
- override fun hashCode (): Int = value.hashCode().toInt()
60
+ override fun hashCode (): Int = value.hashCode()
61
61
62
62
actual override fun toString (): String = value.toString()
63
63
@@ -74,7 +74,7 @@ public actual class Instant internal constructor(internal val value: jtInstant)
74
74
}
75
75
76
76
public actual fun parse (isoString : String ): Instant = try {
77
- Instant (jtOffsetDateTime.parse(fixOffsetRepresentation(isoString)).toInstant())
77
+ Instant (jsTry { jtOffsetDateTime.parse(fixOffsetRepresentation(isoString)) } .toInstant())
78
78
} catch (e: Throwable ) {
79
79
if (e.isJodaDateTimeParseException()) throw DateTimeFormatException (e)
80
80
throw e
@@ -97,21 +97,21 @@ public actual class Instant internal constructor(internal val value: jtInstant)
97
97
Instant.fromEpochSeconds(0, Long.MAX_VALUE).nanosecondsOfSecond) */
98
98
val secs = safeAdd(epochSeconds, nanosecondAdjustment.floorDiv(NANOS_PER_ONE .toLong()))
99
99
val nos = nanosecondAdjustment.mod(NANOS_PER_ONE .toLong()).toInt()
100
- Instant (jtInstant.ofEpochSecond(secs, nos))
100
+ Instant (jsTry { jtInstant.ofEpochSecond(secs.toDouble() , nos) } )
101
101
} catch (e: Throwable ) {
102
102
if (! e.isJodaDateTimeException() && e !is ArithmeticException ) throw e
103
103
if (epochSeconds > 0 ) MAX else MIN
104
104
}
105
105
106
106
public actual fun fromEpochSeconds (epochSeconds : Long , nanosecondAdjustment : Int ): Instant = try {
107
- Instant (jtInstant.ofEpochSecond(epochSeconds, nanosecondAdjustment))
107
+ Instant (jsTry { jtInstant.ofEpochSecond(epochSeconds.toDouble() , nanosecondAdjustment) } )
108
108
} catch (e: Throwable ) {
109
109
if (! e.isJodaDateTimeException()) throw e
110
110
if (epochSeconds > 0 ) MAX else MIN
111
111
}
112
112
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 ) } )
115
115
116
116
internal actual val MIN : Instant = Instant (jtInstant.MIN )
117
117
internal actual val MAX : Instant = Instant (jtInstant.MAX )
@@ -120,23 +120,23 @@ public actual class Instant internal constructor(internal val value: jtInstant)
120
120
121
121
122
122
public 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) }
124
124
with (period) {
125
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 }
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 }
132
132
}.toInstant().let (::Instant )
133
133
} catch (e: Throwable ) {
134
134
if (e.isJodaDateTimeException()) throw DateTimeArithmeticException (e)
135
135
throw e
136
136
}
137
137
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) } }
140
140
141
141
@Deprecated(" Use the plus overload with an explicit number of units" , ReplaceWith (" this.plus(1, unit, timeZone)" ))
142
142
public actual fun Instant.plus (unit : DateTimeUnit , timeZone : TimeZone ): Instant =
@@ -150,9 +150,9 @@ public actual fun Instant.plus(value: Long, unit: DateTimeUnit, timeZone: TimeZo
150
150
plus(value, unit).value.checkZone(timeZone)
151
151
}
152
152
is DateTimeUnit .DayBased ->
153
- thisZdt.plusDays(value.toDouble() * unit.days).toInstant()
153
+ jsTry { thisZdt.plusDays(value.toDouble() * unit.days) } .toInstant()
154
154
is DateTimeUnit .MonthBased ->
155
- thisZdt.plusMonths(value.toDouble() * unit.months).toInstant()
155
+ jsTry { thisZdt.plusMonths(value.toDouble() * unit.months) } .toInstant()
156
156
}.let (::Instant )
157
157
} catch (e: Throwable ) {
158
158
if (e.isJodaDateTimeException()) throw DateTimeArithmeticException (e)
@@ -166,9 +166,9 @@ public actual fun Instant.plus(value: Int, unit: DateTimeUnit, timeZone: TimeZon
166
166
is DateTimeUnit .TimeBased ->
167
167
plus(value.toLong(), unit).value.checkZone(timeZone)
168
168
is DateTimeUnit .DayBased ->
169
- thisZdt.plusDays(value.toDouble() * unit.days).toInstant()
169
+ jsTry { thisZdt.plusDays(value.toDouble() * unit.days) } .toInstant()
170
170
is DateTimeUnit .MonthBased ->
171
- thisZdt.plusMonths(value.toDouble() * unit.months).toInstant()
171
+ jsTry { thisZdt.plusMonths(value.toDouble() * unit.months) } .toInstant()
172
172
}.let (::Instant )
173
173
} catch (e: Throwable ) {
174
174
if (e.isJodaDateTimeException()) throw DateTimeArithmeticException (e)
@@ -194,12 +194,12 @@ public actual fun Instant.plus(value: Long, unit: DateTimeUnit.TimeBased): Insta
194
194
}
195
195
196
196
public 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) }
199
199
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 )
203
203
204
204
buildDateTimePeriod(months.toInt(), days.toInt(), nanoseconds.toLong())
205
205
} catch (e: Throwable ) {
@@ -211,8 +211,8 @@ public actual fun Instant.until(other: Instant, unit: DateTimeUnit, timeZone: Ti
211
211
val otherZdt = other.atZone(timeZone)
212
212
when (unit) {
213
213
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()
216
216
}
217
217
} catch (e: ArithmeticException ) {
218
218
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
221
221
}
222
222
223
223
internal 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