@@ -180,9 +180,23 @@ public expect fun Instant.plus(period: DateTimePeriod, timeZone: TimeZone): Inst
180
180
* @throws DateTimeArithmeticException if this value or the results of intermediate computations are too large to fit in
181
181
* [LocalDateTime].
182
182
*/
183
- public fun Instant.minus (period : DateTimePeriod , timeZone : TimeZone ): Instant = plus(- period, timeZone)
183
+ public fun Instant.minus (period : DateTimePeriod , timeZone : TimeZone ): Instant =
184
+ /* An overflow can happen for any component, but we are only worried about nanoseconds, as having an overflow in
185
+ any other component means that `plus` will throw due to the minimum value of the numeric type overflowing the
186
+ platform-specific limits. */
187
+ if (period.nanoseconds != Long .MIN_VALUE ) {
188
+ val negatedPeriod = with (period) {
189
+ DateTimePeriod (- years, - months, - days, - hours, - minutes, - seconds, - nanoseconds)
190
+ }
191
+ plus(negatedPeriod, timeZone)
192
+ } else {
193
+ val negatedPeriod = with (period) {
194
+ DateTimePeriod (- years, - months, - days, - hours, - minutes, - seconds, - (nanoseconds+ 1 ))
195
+ }
196
+ plus(negatedPeriod, timeZone).plus(DateTimeUnit .NANOSECOND )
197
+ }
184
198
185
- /* *
199
+ /* *
186
200
* Returns a [DateTimePeriod] representing the difference between `this` and [other] instants.
187
201
*
188
202
* The components of [DateTimePeriod] are calculated so that adding it to `this` instant results in the [other] instant.
@@ -386,7 +400,12 @@ public expect fun Instant.plus(value: Long, unit: DateTimeUnit, timeZone: TimeZo
386
400
*
387
401
* @throws DateTimeArithmeticException if this value or the result is too large to fit in [LocalDateTime].
388
402
*/
389
- public fun Instant.minus (value : Long , unit : DateTimeUnit , timeZone : TimeZone ) = plus(- value, unit, timeZone)
403
+ public fun Instant.minus (value : Long , unit : DateTimeUnit , timeZone : TimeZone ) =
404
+ if (value != Long .MIN_VALUE ) {
405
+ plus(- value, unit, timeZone)
406
+ } else {
407
+ plus(- (value+ 1 ), unit, timeZone).plus(unit, timeZone)
408
+ }
390
409
391
410
/* *
392
411
* Returns an instant that is the result of adding the [value] number of the specified [unit] to this instant.
0 commit comments