@@ -9,6 +9,7 @@ import kotlinx.datetime.internal.JodaTimeInstant as jtInstant
9
9
import kotlinx.datetime.internal.JodaTimeOffsetDateTime as jtOffsetDateTime
10
10
import kotlinx.datetime.internal.JodaTimeDuration as jtDuration
11
11
import kotlinx.datetime.internal.JodaTimeClock as jtClock
12
+ import kotlinx.datetime.internal.JodaTimeChronoUnit as jtChronoUnit
12
13
import kotlinx.datetime.internal.safeAdd
13
14
import kotlinx.datetime.internal.*
14
15
import kotlinx.datetime.serializers.InstantIso8601Serializer
@@ -30,9 +31,7 @@ public actual class Instant internal constructor(internal val value: jtInstant)
30
31
31
32
public actual operator fun plus (duration : Duration ): Instant = duration.toComponents { seconds, nanoseconds ->
32
33
return try {
33
- // jsTry {
34
- Instant (plusFix(seconds.toDouble(), nanoseconds))
35
- // }
34
+ Instant (plusFix(seconds.toDouble(), nanoseconds))
36
35
} catch (e: Throwable ) {
37
36
if (! e.isJodaDateTimeException()) throw e
38
37
if (duration.isPositive()) MAX else MIN
@@ -41,8 +40,8 @@ public actual class Instant internal constructor(internal val value: jtInstant)
41
40
42
41
internal fun plusFix (seconds : Double , nanos : Int ): jtInstant {
43
42
val newSeconds = value.epochSecond() + seconds
44
- val newNanos = ( value.nano() + nanos).toInt()
45
- return jtInstant.ofEpochSecond(newSeconds, newNanos)
43
+ val newNanos = value.nano() + nanos
44
+ return jtInstant.ofEpochSecond(newSeconds, newNanos.toInt() )
46
45
}
47
46
48
47
public actual operator fun minus (duration : Duration ): Instant = plus(- duration)
@@ -55,7 +54,7 @@ public actual class Instant internal constructor(internal val value: jtInstant)
55
54
public actual override operator fun compareTo (other : Instant ): Int = this .value.compareTo(other.value)
56
55
57
56
override fun equals (other : Any? ): Boolean =
58
- (this == = other) || (other is Instant && ( this .value == = other.value || this .value.equals(other.value)) )
57
+ (this == = other) || (other is Instant && this .value == other.value)
59
58
60
59
override fun hashCode (): Int = value.hashCode()
61
60
@@ -92,12 +91,12 @@ public actual class Instant internal constructor(internal val value: jtInstant)
92
91
}
93
92
94
93
public actual fun fromEpochSeconds (epochSeconds : Long , nanosecondAdjustment : Long ): Instant = try {
95
- /* Performing normalization here because otherwise this fails:
94
+ /* Performing normalization here because otherwise this fails:
96
95
assertEquals((Long.MAX_VALUE % 1_000_000_000).toInt(),
97
96
Instant.fromEpochSeconds(0, Long.MAX_VALUE).nanosecondsOfSecond) */
98
- val secs = safeAdd(epochSeconds, nanosecondAdjustment.floorDiv(NANOS_PER_ONE .toLong()))
99
- val nos = nanosecondAdjustment.mod(NANOS_PER_ONE .toLong()).toInt()
100
- Instant (jtInstant.ofEpochSecond(secs.toDouble(), nos))
97
+ val secs = safeAdd(epochSeconds, nanosecondAdjustment.floorDiv(NANOS_PER_ONE .toLong()))
98
+ val nos = nanosecondAdjustment.mod(NANOS_PER_ONE .toLong()).toInt()
99
+ Instant (jtInstant.ofEpochSecond(secs.toDouble(), nos))
101
100
} catch (e: Throwable ) {
102
101
if (! e.isJodaDateTimeException() && e !is ArithmeticException ) throw e
103
102
if (epochSeconds > 0 ) MAX else MIN
@@ -123,12 +122,12 @@ public actual fun Instant.plus(period: DateTimePeriod, timeZone: TimeZone): Inst
123
122
val thisZdt = this .value.atZone(timeZone.zoneId)
124
123
with (period) {
125
124
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 }
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 }
132
131
}.toInstant().let (::Instant )
133
132
} catch (e: Throwable ) {
134
133
if (e.isJodaDateTimeException()) throw DateTimeArithmeticException (e)
@@ -149,10 +148,8 @@ public actual fun Instant.plus(value: Long, unit: DateTimeUnit, timeZone: TimeZo
149
148
is DateTimeUnit .TimeBased -> {
150
149
plus(value, unit).value.checkZone(timeZone)
151
150
}
152
-
153
151
is DateTimeUnit .DayBased ->
154
152
thisZdt.plusDays(value.toDouble() * unit.days).toInstant()
155
-
156
153
is DateTimeUnit .MonthBased ->
157
154
thisZdt.plusMonths(value.toDouble() * unit.months).toInstant()
158
155
}.let (::Instant )
@@ -167,10 +164,8 @@ public actual fun Instant.plus(value: Int, unit: DateTimeUnit, timeZone: TimeZon
167
164
when (unit) {
168
165
is DateTimeUnit .TimeBased ->
169
166
plus(value.toLong(), unit).value.checkZone(timeZone)
170
-
171
167
is DateTimeUnit .DayBased ->
172
168
thisZdt.plusDays(value.toDouble() * unit.days).toInstant()
173
-
174
169
is DateTimeUnit .MonthBased ->
175
170
thisZdt.plusMonths(value.toDouble() * unit.months).toInstant()
176
171
}.let (::Instant )
@@ -201,9 +196,9 @@ public actual fun Instant.periodUntil(other: Instant, timeZone: TimeZone): DateT
201
196
var thisZdt = this .value.atZone(timeZone.zoneId)
202
197
val otherZdt = other.value.atZone(timeZone.zoneId)
203
198
204
- val months = thisZdt.until(otherZdt, JodaTimeChronoUnit .MONTHS ); thisZdt = thisZdt.plusMonths(months)
205
- val days = thisZdt.until(otherZdt, JodaTimeChronoUnit .DAYS ); thisZdt = thisZdt.plusDays(days)
206
- val nanoseconds = thisZdt.until(otherZdt, JodaTimeChronoUnit .NANOS )
199
+ val months = thisZdt.until(otherZdt, jtChronoUnit .MONTHS ); thisZdt = thisZdt.plusMonths(months)
200
+ val days = thisZdt.until(otherZdt, jtChronoUnit .DAYS ); thisZdt = thisZdt.plusDays(days)
201
+ val nanoseconds = thisZdt.until(otherZdt, jtChronoUnit .NANOS )
207
202
208
203
buildDateTimePeriod(months.toInt(), days.toInt(), nanoseconds.toLong())
209
204
} catch (e: Throwable ) {
@@ -213,10 +208,10 @@ public actual fun Instant.periodUntil(other: Instant, timeZone: TimeZone): DateT
213
208
public actual fun Instant.until (other : Instant , unit : DateTimeUnit , timeZone : TimeZone ): Long = try {
214
209
val thisZdt = this .atZone(timeZone)
215
210
val otherZdt = other.atZone(timeZone)
216
- when (unit) {
211
+ when (unit) {
217
212
is DateTimeUnit .TimeBased -> until(other, unit)
218
- is DateTimeUnit .DayBased -> (thisZdt.until(otherZdt, JodaTimeChronoUnit .DAYS ) / unit.days).toLong()
219
- is DateTimeUnit .MonthBased -> (thisZdt.until(otherZdt, JodaTimeChronoUnit .MONTHS ) / unit.months).toLong()
213
+ is DateTimeUnit .DayBased -> (thisZdt.until(otherZdt, jtChronoUnit .DAYS ) / unit.days).toLong()
214
+ is DateTimeUnit .MonthBased -> (thisZdt.until(otherZdt, jtChronoUnit .MONTHS ) / unit.months).toLong()
220
215
}
221
216
} catch (e: ArithmeticException ) {
222
217
if (this < other) Long .MAX_VALUE else Long .MIN_VALUE
@@ -225,4 +220,4 @@ public actual fun Instant.until(other: Instant, unit: DateTimeUnit, timeZone: Ti
225
220
}
226
221
227
222
internal actual fun Instant.toStringWithOffset (offset : UtcOffset ): String =
228
- jtOffsetDateTime.ofInstant(this .value, offset.zoneOffset).toString()
223
+ jtOffsetDateTime.ofInstant(this .value, offset.zoneOffset).toString()
0 commit comments