@@ -195,13 +195,33 @@ public expect fun Instant.periodUntil(other: Instant, timeZone: TimeZone): DateT
195
195
* - positive or zero if this instant is earlier than the other,
196
196
* - negative or zero if this instant is later than the other,
197
197
* - zero if this instant is equal to the other.
198
-
198
+ *
199
199
* If the result does not fit in [Long], returns [Long.MAX_VALUE] for a positive result or [Long.MIN_VALUE] for a negative result.
200
200
*
201
201
* @throws DateTimeArithmeticException if `this` or [other] instant is too large to fit in [LocalDateTime].
202
202
*/
203
203
public expect fun Instant.until (other : Instant , unit : DateTimeUnit , timeZone : TimeZone ): Long
204
204
205
+ /* *
206
+ * Returns the whole number of the specified time [units][unit] between `this` and [other] instants.
207
+ *
208
+ * The value returned is:
209
+ * - positive or zero if this instant is earlier than the other,
210
+ * - negative or zero if this instant is later than the other,
211
+ * - zero if this instant is equal to the other.
212
+ *
213
+ * If the result does not fit in [Long], returns [Long.MAX_VALUE] for a positive result or [Long.MIN_VALUE] for a negative result.
214
+ */
215
+ public fun Instant.until (other : Instant , unit : DateTimeUnit .TimeBased ): Long =
216
+ try {
217
+ multiplyAddAndDivide(other.epochSeconds - epochSeconds,
218
+ NANOS_PER_ONE .toLong(),
219
+ (other.nanosecondsOfSecond - nanosecondsOfSecond).toLong(),
220
+ unit.nanoseconds)
221
+ } catch (e: ArithmeticException ) {
222
+ if (this < other) Long .MAX_VALUE else Long .MIN_VALUE
223
+ }
224
+
205
225
/* *
206
226
* Returns the number of whole days between two instants in the specified [timeZone].
207
227
*
@@ -262,6 +282,16 @@ public fun Instant.minus(other: Instant, timeZone: TimeZone): DateTimePeriod =
262
282
*/
263
283
public expect fun Instant.plus (unit : DateTimeUnit , timeZone : TimeZone ): Instant
264
284
285
+ /* *
286
+ * Returns an instant that is the result of adding one [unit] to this instant.
287
+ *
288
+ * The returned instant is later than this instant.
289
+ *
290
+ * The return value is clamped to the platform-specific boundaries for [Instant] if the result exceeds them.
291
+ */
292
+ public fun Instant.plus (unit : DateTimeUnit .TimeBased ): Instant =
293
+ plus(1L , unit)
294
+
265
295
/* *
266
296
* Returns an instant that is the result of adding the [value] number of the specified [unit] to this instant
267
297
* in the specified [timeZone].
@@ -273,6 +303,17 @@ public expect fun Instant.plus(unit: DateTimeUnit, timeZone: TimeZone): Instant
273
303
*/
274
304
public expect fun Instant.plus (value : Int , unit : DateTimeUnit , timeZone : TimeZone ): Instant
275
305
306
+ /* *
307
+ * Returns an instant that is the result of adding the [value] number of the specified [unit] to this instant.
308
+ *
309
+ * If the [value] is positive, the returned instant is later than this instant.
310
+ * If the [value] is negative, the returned instant is earlier than this instant.
311
+ *
312
+ * The return value is clamped to the platform-specific boundaries for [Instant] if the result exceeds them.
313
+ */
314
+ public fun Instant.plus (value : Int , unit : DateTimeUnit .TimeBased ): Instant =
315
+ plus(value.toLong(), unit)
316
+
276
317
/* *
277
318
* Returns an instant that is the result of adding the [value] number of the specified [unit] to this instant
278
319
* in the specified [timeZone].
@@ -284,6 +325,16 @@ public expect fun Instant.plus(value: Int, unit: DateTimeUnit, timeZone: TimeZon
284
325
*/
285
326
public expect fun Instant.plus (value : Long , unit : DateTimeUnit , timeZone : TimeZone ): Instant
286
327
328
+ /* *
329
+ * Returns an instant that is the result of adding the [value] number of the specified [unit] to this instant.
330
+ *
331
+ * If the [value] is positive, the returned instant is later than this instant.
332
+ * If the [value] is negative, the returned instant is earlier than this instant.
333
+ *
334
+ * The return value is clamped to the platform-specific boundaries for [Instant] if the result exceeds them.
335
+ */
336
+ public expect fun Instant.plus (value : Long , unit : DateTimeUnit .TimeBased ): Instant
337
+
287
338
/* *
288
339
* Returns the whole number of the specified date or time [units][unit] between [other] and `this` instants
289
340
* in the specified [timeZone].
@@ -299,5 +350,18 @@ public expect fun Instant.plus(value: Long, unit: DateTimeUnit, timeZone: TimeZo
299
350
public fun Instant.minus (other : Instant , unit : DateTimeUnit , timeZone : TimeZone ): Long =
300
351
other.until(this , unit, timeZone)
301
352
353
+ /* *
354
+ * Returns the whole number of the specified time [units][unit] between [other] and `this` instants.
355
+ *
356
+ * The value returned is negative or zero if this instant is earlier than the other,
357
+ * and positive or zero if this instant is later than the other.
358
+ *
359
+ * If the result does not fit in [Long], returns [Long.MAX_VALUE] for a positive result or [Long.MIN_VALUE] for a negative result.
360
+ *
361
+ * @see Instant.until
362
+ */
363
+ public fun Instant.minus (other : Instant , unit : DateTimeUnit .TimeBased ): Long =
364
+ other.until(this , unit)
365
+
302
366
internal const val DISTANT_PAST_SECONDS = - 3217862419201
303
367
internal const val DISTANT_FUTURE_SECONDS = 3093527980800
0 commit comments