@@ -182,6 +182,29 @@ public fun String.toInstant(): Instant = Instant.parse(this)
182
182
public expect fun Instant.plus (period : DateTimePeriod , timeZone : TimeZone ): Instant
183
183
184
184
/* *
185
+ * Returns an instant that is the result of subtracting components of [DateTimePeriod] from this instant. The components
186
+ * are subtracted in the order from the largest units to the smallest, i.e. from years to nanoseconds.
187
+ *
188
+ * @throws DateTimeArithmeticException if this value or the results of intermediate computations are too large to fit in
189
+ * [LocalDateTime].
190
+ */
191
+ public fun Instant.minus (period : DateTimePeriod , timeZone : TimeZone ): Instant =
192
+ /* An overflow can happen for any component, but we are only worried about nanoseconds, as having an overflow in
193
+ any other component means that `plus` will throw due to the minimum value of the numeric type overflowing the
194
+ platform-specific limits. */
195
+ if (period.nanoseconds != Long .MIN_VALUE ) {
196
+ val negatedPeriod = with (period) {
197
+ DateTimePeriod (- years, - months, - days, - hours, - minutes, - seconds, - nanoseconds)
198
+ }
199
+ plus(negatedPeriod, timeZone)
200
+ } else {
201
+ val negatedPeriod = with (period) {
202
+ DateTimePeriod (- years, - months, - days, - hours, - minutes, - seconds, - (nanoseconds+ 1 ))
203
+ }
204
+ plus(negatedPeriod, timeZone).plus(DateTimeUnit .NANOSECOND )
205
+ }
206
+
207
+ /* *
185
208
* Returns a [DateTimePeriod] representing the difference between `this` and [other] instants.
186
209
*
187
210
* The components of [DateTimePeriod] are calculated so that adding it to `this` instant results in the [other] instant.
@@ -290,6 +313,17 @@ public fun Instant.minus(other: Instant, timeZone: TimeZone): DateTimePeriod =
290
313
*/
291
314
public expect fun Instant.plus (unit : DateTimeUnit , timeZone : TimeZone ): Instant
292
315
316
+ /* *
317
+ * Returns an instant that is the result of subtracting one [unit] from this instant
318
+ * in the specified [timeZone].
319
+ *
320
+ * The returned instant is earlier than this instant.
321
+ *
322
+ * @throws DateTimeArithmeticException if this value or the result is too large to fit in [LocalDateTime].
323
+ */
324
+ public fun Instant.minus (unit : DateTimeUnit , timeZone : TimeZone ): Instant =
325
+ plus(- 1 , unit, timeZone)
326
+
293
327
/* *
294
328
* Returns an instant that is the result of adding one [unit] to this instant.
295
329
*
@@ -300,6 +334,16 @@ public expect fun Instant.plus(unit: DateTimeUnit, timeZone: TimeZone): Instant
300
334
public fun Instant.plus (unit : DateTimeUnit .TimeBased ): Instant =
301
335
plus(1L , unit)
302
336
337
+ /* *
338
+ * Returns an instant that is the result of subtracting one [unit] from this instant.
339
+ *
340
+ * The returned instant is earlier than this instant.
341
+ *
342
+ * The return value is clamped to the platform-specific boundaries for [Instant] if the result exceeds them.
343
+ */
344
+ public fun Instant.minus (unit : DateTimeUnit .TimeBased ): Instant =
345
+ plus(- 1L , unit)
346
+
303
347
/* *
304
348
* Returns an instant that is the result of adding the [value] number of the specified [unit] to this instant
305
349
* in the specified [timeZone].
@@ -311,6 +355,17 @@ public fun Instant.plus(unit: DateTimeUnit.TimeBased): Instant =
311
355
*/
312
356
public expect fun Instant.plus (value : Int , unit : DateTimeUnit , timeZone : TimeZone ): Instant
313
357
358
+ /* *
359
+ * Returns an instant that is the result of subtracting the [value] number of the specified [unit] from this instant
360
+ * in the specified [timeZone].
361
+ *
362
+ * If the [value] is positive, the returned instant is earlier than this instant.
363
+ * If the [value] is negative, the returned instant is later than this instant.
364
+ *
365
+ * @throws DateTimeArithmeticException if this value or the result is too large to fit in [LocalDateTime].
366
+ */
367
+ public expect fun Instant.minus (value : Int , unit : DateTimeUnit , timeZone : TimeZone ): Instant
368
+
314
369
/* *
315
370
* Returns an instant that is the result of adding the [value] number of the specified [unit] to this instant.
316
371
*
@@ -322,6 +377,17 @@ public expect fun Instant.plus(value: Int, unit: DateTimeUnit, timeZone: TimeZon
322
377
public fun Instant.plus (value : Int , unit : DateTimeUnit .TimeBased ): Instant =
323
378
plus(value.toLong(), unit)
324
379
380
+ /* *
381
+ * Returns an instant that is the result of subtracting the [value] number of the specified [unit] from this instant.
382
+ *
383
+ * If the [value] is positive, the returned instant is earlier than this instant.
384
+ * If the [value] is negative, the returned instant is later than this instant.
385
+ *
386
+ * The return value is clamped to the platform-specific boundaries for [Instant] if the result exceeds them.
387
+ */
388
+ public fun Instant.minus (value : Int , unit : DateTimeUnit .TimeBased ): Instant =
389
+ minus(value.toLong(), unit)
390
+
325
391
/* *
326
392
* Returns an instant that is the result of adding the [value] number of the specified [unit] to this instant
327
393
* in the specified [timeZone].
@@ -333,6 +399,22 @@ public fun Instant.plus(value: Int, unit: DateTimeUnit.TimeBased): Instant =
333
399
*/
334
400
public expect fun Instant.plus (value : Long , unit : DateTimeUnit , timeZone : TimeZone ): Instant
335
401
402
+ /* *
403
+ * Returns an instant that is the result of subtracting the [value] number of the specified [unit] from this instant
404
+ * in the specified [timeZone].
405
+ *
406
+ * If the [value] is positive, the returned instant is earlier than this instant.
407
+ * If the [value] is negative, the returned instant is later than this instant.
408
+ *
409
+ * @throws DateTimeArithmeticException if this value or the result is too large to fit in [LocalDateTime].
410
+ */
411
+ public fun Instant.minus (value : Long , unit : DateTimeUnit , timeZone : TimeZone ) =
412
+ if (value != Long .MIN_VALUE ) {
413
+ plus(- value, unit, timeZone)
414
+ } else {
415
+ plus(- (value + 1 ), unit, timeZone).plus(unit, timeZone)
416
+ }
417
+
336
418
/* *
337
419
* Returns an instant that is the result of adding the [value] number of the specified [unit] to this instant.
338
420
*
@@ -343,6 +425,21 @@ public expect fun Instant.plus(value: Long, unit: DateTimeUnit, timeZone: TimeZo
343
425
*/
344
426
public expect fun Instant.plus (value : Long , unit : DateTimeUnit .TimeBased ): Instant
345
427
428
+ /* *
429
+ * Returns an instant that is the result of subtracting the [value] number of the specified [unit] from this instant.
430
+ *
431
+ * If the [value] is positive, the returned instant is earlier than this instant.
432
+ * If the [value] is negative, the returned instant is later than this instant.
433
+ *
434
+ * The return value is clamped to the platform-specific boundaries for [Instant] if the result exceeds them.
435
+ */
436
+ public fun Instant.minus (value : Long , unit : DateTimeUnit .TimeBased ): Instant =
437
+ if (value != Long .MIN_VALUE ) {
438
+ plus(- value, unit)
439
+ } else {
440
+ plus(- (value + 1 ), unit).plus(unit)
441
+ }
442
+
346
443
/* *
347
444
* Returns the whole number of the specified date or time [units][unit] between [other] and `this` instants
348
445
* in the specified [timeZone].
0 commit comments