@@ -199,6 +199,7 @@ public expect class Instant : Comparable<Instant> {
199
199
* Note that this number doesn't include leap seconds added or removed since the epoch.
200
200
*
201
201
* @see fromEpochSeconds
202
+ * @sample kotlinx.datetime.test.samples.InstantSamples.epochSeconds
202
203
*/
203
204
public val epochSeconds: Long
204
205
@@ -208,6 +209,7 @@ public expect class Instant : Comparable<Instant> {
208
209
* The value is always positive and lies in the range `0..999_999_999`.
209
210
*
210
211
* @see fromEpochSeconds
212
+ * @sample kotlinx.datetime.test.samples.InstantSamples.nanosecondsOfSecond
211
213
*/
212
214
public val nanosecondsOfSecond: Int
213
215
@@ -219,6 +221,7 @@ public expect class Instant : Comparable<Instant> {
219
221
* If the result does not fit in [Long], returns [Long.MAX_VALUE] for a positive result or [Long.MIN_VALUE] for a negative result.
220
222
*
221
223
* @see fromEpochMilliseconds
224
+ * @sample kotlinx.datetime.test.samples.InstantSamples.toEpochMilliseconds
222
225
*/
223
226
public fun toEpochMilliseconds (): Long
224
227
@@ -234,6 +237,8 @@ public expect class Instant : Comparable<Instant> {
234
237
* in `kotlinx-datetime`, adding a day is a calendar-based operation, whereas [Duration] always considers
235
238
* a day to be 24 hours.
236
239
* For an explanation of why this is error-prone, see [DateTimeUnit.DayBased].
240
+ *
241
+ * @sample kotlinx.datetime.test.samples.InstantSamples.plusDuration
237
242
*/
238
243
public operator fun plus (duration : Duration ): Instant
239
244
@@ -249,6 +254,8 @@ public expect class Instant : Comparable<Instant> {
249
254
* in `kotlinx-datetime`, adding a day is a calendar-based operation, whereas [Duration] always considers
250
255
* a day to be 24 hours.
251
256
* For an explanation of why this is error-prone, see [DateTimeUnit.DayBased].
257
+ *
258
+ * @sample kotlinx.datetime.test.samples.InstantSamples.minusDuration
252
259
*/
253
260
public operator fun minus (duration : Duration ): Instant
254
261
@@ -266,6 +273,8 @@ public expect class Instant : Comparable<Instant> {
266
273
* or even monotonic, so the result of this operation may be negative even if the other instant was observed later
267
274
* than this one, or vice versa.
268
275
* For measuring time intervals, consider using [TimeSource.Monotonic].
276
+ *
277
+ * @sample kotlinx.datetime.test.samples.InstantSamples.minusInstant
269
278
*/
270
279
public operator fun minus (other : Instant ): Duration
271
280
@@ -274,6 +283,8 @@ public expect class Instant : Comparable<Instant> {
274
283
* Returns zero if this instant represents the same moment as the other (i.e., equal to other),
275
284
* a negative number if this instant is earlier than the other,
276
285
* and a positive number if this instant is later than the other.
286
+ *
287
+ * @sample kotlinx.datetime.test.samples.InstantSamples.compareToSample
277
288
*/
278
289
public override operator fun compareTo (other : Instant ): Int
279
290
@@ -289,6 +300,7 @@ public expect class Instant : Comparable<Instant> {
289
300
* @see DateTimeComponents.Formats.ISO_DATE_TIME_OFFSET for a very similar format. The difference is that
290
301
* [DateTimeComponents.Formats.ISO_DATE_TIME_OFFSET] will not add trailing zeros for readability to the
291
302
* fractional part of the second.
303
+ * @sample kotlinx.datetime.test.samples.InstantSamples.toStringSample
292
304
*/
293
305
public override fun toString (): String
294
306
@@ -306,6 +318,7 @@ public expect class Instant : Comparable<Instant> {
306
318
* Note that [Instant] also supports nanosecond precision via [fromEpochSeconds].
307
319
*
308
320
* @see Instant.toEpochMilliseconds
321
+ * @sample kotlinx.datetime.test.samples.InstantSamples.fromEpochMilliseconds
309
322
*/
310
323
public fun fromEpochMilliseconds (epochMilliseconds : Long ): Instant
311
324
@@ -320,6 +333,7 @@ public expect class Instant : Comparable<Instant> {
320
333
*
321
334
* @see Instant.epochSeconds
322
335
* @see Instant.nanosecondsOfSecond
336
+ * @sample kotlinx.datetime.test.samples.InstantSamples.fromEpochSeconds
323
337
*/
324
338
public fun fromEpochSeconds (epochSeconds : Long , nanosecondAdjustment : Long = 0): Instant
325
339
@@ -334,6 +348,7 @@ public expect class Instant : Comparable<Instant> {
334
348
*
335
349
* @see Instant.epochSeconds
336
350
* @see Instant.nanosecondsOfSecond
351
+ * @sample kotlinx.datetime.test.samples.InstantSamples.fromEpochSecondsIntNanos
337
352
*/
338
353
public fun fromEpochSeconds (epochSeconds : Long , nanosecondAdjustment : Int ): Instant
339
354
@@ -356,6 +371,7 @@ public expect class Instant : Comparable<Instant> {
356
371
*
357
372
* @see Instant.toString for formatting using the default format.
358
373
* @see Instant.format for formatting using a custom format.
374
+ * @sample kotlinx.datetime.test.samples.InstantSamples.parsing
359
375
*/
360
376
public fun parse (
361
377
input : CharSequence ,
@@ -387,11 +403,19 @@ public expect class Instant : Comparable<Instant> {
387
403
}
388
404
}
389
405
390
- /* * Returns true if the instant is [Instant.DISTANT_PAST] or earlier. */
406
+ /* *
407
+ * Returns true if the instant is [Instant.DISTANT_PAST] or earlier.
408
+ *
409
+ * @sample kotlinx.datetime.test.samples.InstantSamples.isDistantPast
410
+ */
391
411
public val Instant .isDistantPast: Boolean
392
412
get() = this <= Instant .DISTANT_PAST
393
413
394
- /* * Returns true if the instant is [Instant.DISTANT_FUTURE] or later. */
414
+ /* *
415
+ * Returns true if the instant is [Instant.DISTANT_FUTURE] or later.
416
+ *
417
+ * @sample kotlinx.datetime.test.samples.InstantSamples.isDistantFuture
418
+ */
395
419
public val Instant .isDistantFuture: Boolean
396
420
get() = this >= Instant .DISTANT_FUTURE
397
421
@@ -412,12 +436,9 @@ public fun String.toInstant(): Instant = Instant.parse(this)
412
436
* please consider using a multiple of [DateTimeUnit.DAY] or [DateTimeUnit.MONTH], like in
413
437
* `Clock.System.now().plus(5, DateTimeUnit.DAY, TimeZone.currentSystemDefault())`.
414
438
*
415
- * ```
416
- * Clock.System.now().plus(DateTimePeriod(months = 1, days = -1), TimeZone.UTC) // one day short from a month later
417
- * ```
418
- *
419
439
* @throws DateTimeArithmeticException if this value or the results of intermediate computations are too large to fit in
420
440
* [LocalDateTime].
441
+ * @sample kotlinx.datetime.test.samples.InstantSamples.plusPeriod
421
442
*/
422
443
public expect fun Instant.plus (period : DateTimePeriod , timeZone : TimeZone ): Instant
423
444
@@ -432,12 +453,9 @@ public expect fun Instant.plus(period: DateTimePeriod, timeZone: TimeZone): Inst
432
453
* please consider using a multiple of [DateTimeUnit.DAY] or [DateTimeUnit.MONTH], as in
433
454
* `Clock.System.now().minus(5, DateTimeUnit.DAY, TimeZone.currentSystemDefault())`.
434
455
*
435
- * ```
436
- * Clock.System.now().minus(DateTimePeriod(months = 1, days = -1), TimeZone.UTC) // one day short from a month earlier
437
- * ```
438
- *
439
456
* @throws DateTimeArithmeticException if this value or the results of intermediate computations are too large to fit in
440
457
* [LocalDateTime].
458
+ * @sample kotlinx.datetime.test.samples.InstantSamples.minusPeriod
441
459
*/
442
460
public fun Instant.minus (period : DateTimePeriod , timeZone : TimeZone ): Instant =
443
461
/* An overflow can happen for any component, but we are only worried about nanoseconds, as having an overflow in
@@ -463,6 +481,7 @@ public fun Instant.minus(period: DateTimePeriod, timeZone: TimeZone): Instant =
463
481
*
464
482
* @throws DateTimeArithmeticException if `this` or [other] instant is too large to fit in [LocalDateTime].
465
483
* Or (only on the JVM) if the number of months between the two dates exceeds an Int.
484
+ * @sample kotlinx.datetime.test.samples.InstantSamples.periodUntil
466
485
*/
467
486
public expect fun Instant.periodUntil (other : Instant , timeZone : TimeZone ): DateTimePeriod
468
487
@@ -477,13 +496,8 @@ public expect fun Instant.periodUntil(other: Instant, timeZone: TimeZone): DateT
477
496
*
478
497
* If the result does not fit in [Long], returns [Long.MAX_VALUE] for a positive result or [Long.MIN_VALUE] for a negative result.
479
498
*
480
- * ```
481
- * val momentOfBirth = LocalDateTime.parse("1990-02-20T12:03:53Z").toInstant(TimeZone.of("Europe/Berlin"))
482
- * val currentMoment = Clock.System.now()
483
- * val daysLived = momentOfBirth.until(currentMoment, DateTimeUnit.DAY, TimeZone.currentSystemDefault())
484
- * ```
485
- *
486
499
* @throws DateTimeArithmeticException if `this` or [other] instant is too large to fit in [LocalDateTime].
500
+ * @sample kotlinx.datetime.test.samples.InstantSamples.untilAsDateTimeUnit
487
501
*/
488
502
public expect fun Instant.until (other : Instant , unit : DateTimeUnit , timeZone : TimeZone ): Long
489
503
@@ -497,11 +511,7 @@ public expect fun Instant.until(other: Instant, unit: DateTimeUnit, timeZone: Ti
497
511
*
498
512
* If the result does not fit in [Long], returns [Long.MAX_VALUE] for a positive result or [Long.MIN_VALUE] for a negative result.
499
513
*
500
- * ```
501
- * val momentOfBirth = LocalDateTime.parse("1990-02-20T12:03:53Z").toInstant(TimeZone.of("Europe/Berlin"))
502
- * val currentMoment = Clock.System.now()
503
- * val minutesLived = momentOfBirth.until(currentMoment, DateTimeUnit.MINUTE)
504
- * ```
514
+ * @sample kotlinx.datetime.test.samples.InstantSamples.untilAsTimeBasedUnit
505
515
*/
506
516
public fun Instant.until (other : Instant , unit : DateTimeUnit .TimeBased ): Long =
507
517
try {
@@ -520,6 +530,7 @@ public fun Instant.until(other: Instant, unit: DateTimeUnit.TimeBased): Long =
520
530
*
521
531
* @see Instant.until
522
532
* @throws DateTimeArithmeticException if `this` or [other] instant is too large to fit in [LocalDateTime].
533
+ * @sample kotlinx.datetime.test.samples.InstantSamples.daysUntil
523
534
*/
524
535
public fun Instant.daysUntil (other : Instant , timeZone : TimeZone ): Int =
525
536
until(other, DateTimeUnit .DAY , timeZone).clampToInt()
@@ -531,6 +542,7 @@ public fun Instant.daysUntil(other: Instant, timeZone: TimeZone): Int =
531
542
*
532
543
* @see Instant.until
533
544
* @throws DateTimeArithmeticException if `this` or [other] instant is too large to fit in [LocalDateTime].
545
+ * @sample kotlinx.datetime.test.samples.InstantSamples.monthsUntil
534
546
*/
535
547
public fun Instant.monthsUntil (other : Instant , timeZone : TimeZone ): Int =
536
548
until(other, DateTimeUnit .MONTH , timeZone).clampToInt()
@@ -542,6 +554,7 @@ public fun Instant.monthsUntil(other: Instant, timeZone: TimeZone): Int =
542
554
*
543
555
* @see Instant.until
544
556
* @throws DateTimeArithmeticException if `this` or [other] instant is too large to fit in [LocalDateTime].
557
+ * @sample kotlinx.datetime.test.samples.InstantSamples.yearsUntil
545
558
*/
546
559
public fun Instant.yearsUntil (other : Instant , timeZone : TimeZone ): Int =
547
560
until(other, DateTimeUnit .YEAR , timeZone).clampToInt()
@@ -559,6 +572,7 @@ public fun Instant.yearsUntil(other: Instant, timeZone: TimeZone): Int =
559
572
* @throws DateTimeArithmeticException if `this` or [other] instant is too large to fit in [LocalDateTime].
560
573
* Or (only on the JVM) if the number of months between the two dates exceeds an Int.
561
574
* @see Instant.periodUntil
575
+ * @sample kotlinx.datetime.test.samples.InstantSamples.minusInstantInZone
562
576
*/
563
577
public fun Instant.minus (other : Instant , timeZone : TimeZone ): DateTimePeriod =
564
578
other.periodUntil(this , timeZone)
@@ -619,11 +633,8 @@ public fun Instant.minus(unit: DateTimeUnit.TimeBased): Instant =
619
633
* Note that the time zone does not need to be passed when the [unit] is a time-based unit.
620
634
* It is also not needed when adding date-based units to a [LocalDate].
621
635
*
622
- * ```
623
- * Clock.System.now().plus(5, DateTimeUnit.DAY, TimeZone.of("Europe/Berlin")) // 5 days from now in Berlin
624
- * ```
625
- *
626
636
* @throws DateTimeArithmeticException if this value or the result is too large to fit in [LocalDateTime].
637
+ * @sample kotlinx.datetime.test.samples.InstantSamples.plusDateTimeUnit
627
638
*/
628
639
public expect fun Instant.plus (value : Int , unit : DateTimeUnit , timeZone : TimeZone ): Instant
629
640
@@ -637,14 +648,11 @@ public expect fun Instant.plus(value: Int, unit: DateTimeUnit, timeZone: TimeZon
637
648
* Note that the time zone does not need to be passed when the [unit] is a time-based unit.
638
649
* It is also not needed when subtracting date-based units from a [LocalDate].
639
650
*
640
- * ```
641
- * Clock.System.now().minus(5, DateTimeUnit.DAY, TimeZone.of("Europe/Berlin")) // 5 days earlier than now in Berlin
642
- * ```
643
- *
644
651
* If the [value] is positive, the returned instant is earlier than this instant.
645
652
* If the [value] is negative, the returned instant is later than this instant.
646
653
*
647
654
* @throws DateTimeArithmeticException if this value or the result is too large to fit in [LocalDateTime].
655
+ * @sample kotlinx.datetime.test.samples.InstantSamples.minusDateTimeUnit
648
656
*/
649
657
public expect fun Instant.minus (value : Int , unit : DateTimeUnit , timeZone : TimeZone ): Instant
650
658
@@ -654,11 +662,9 @@ public expect fun Instant.minus(value: Int, unit: DateTimeUnit, timeZone: TimeZo
654
662
* If the [value] is positive, the returned instant is later than this instant.
655
663
* If the [value] is negative, the returned instant is earlier than this instant.
656
664
*
657
- * ```
658
- * Clock.System.now().plus(5, DateTimeUnit.HOUR) // 5 hours from now
659
- * ```
660
- *
661
665
* The return value is clamped to the platform-specific boundaries for [Instant] if the result exceeds them.
666
+ *
667
+ * @sample kotlinx.datetime.test.samples.InstantSamples.plusTimeBasedUnit
662
668
*/
663
669
public fun Instant.plus (value : Int , unit : DateTimeUnit .TimeBased ): Instant =
664
670
plus(value.toLong(), unit)
@@ -669,11 +675,9 @@ public fun Instant.plus(value: Int, unit: DateTimeUnit.TimeBased): Instant =
669
675
* If the [value] is positive, the returned instant is earlier than this instant.
670
676
* If the [value] is negative, the returned instant is later than this instant.
671
677
*
672
- * ```
673
- * Clock.System.now().minus(5, DateTimeUnit.HOUR) // 5 hours earlier than now
674
- * ```
675
- *
676
678
* The return value is clamped to the platform-specific boundaries for [Instant] if the result exceeds them.
679
+ *
680
+ * @sample kotlinx.datetime.test.samples.InstantSamples.minusTimeBasedUnit
677
681
*/
678
682
public fun Instant.minus (value : Int , unit : DateTimeUnit .TimeBased ): Instant =
679
683
minus(value.toLong(), unit)
@@ -688,11 +692,8 @@ public fun Instant.minus(value: Int, unit: DateTimeUnit.TimeBased): Instant =
688
692
* Note that the time zone does not need to be passed when the [unit] is a time-based unit.
689
693
* It is also not needed when adding date-based units to a [LocalDate].
690
694
*
691
- * ```
692
- * Clock.System.now().plus(5L, DateTimeUnit.DAY, TimeZone.of("Europe/Berlin")) // 5 days from now in Berlin
693
- * ```
694
- *
695
695
* @throws DateTimeArithmeticException if this value or the result is too large to fit in [LocalDateTime].
696
+ * @sample kotlinx.datetime.test.samples.InstantSamples.plusDateTimeUnitLong
696
697
*/
697
698
public expect fun Instant.plus (value : Long , unit : DateTimeUnit , timeZone : TimeZone ): Instant
698
699
@@ -706,11 +707,8 @@ public expect fun Instant.plus(value: Long, unit: DateTimeUnit, timeZone: TimeZo
706
707
* Note that the time zone does not need to be passed when the [unit] is a time-based unit.
707
708
* It is also not needed when subtracting date-based units from a [LocalDate].
708
709
*
709
- * ```
710
- * Clock.System.now().minus(5L, DateTimeUnit.DAY, TimeZone.of("Europe/Berlin")) // 5 days earlier than now in Berlin
711
- * ```
712
- *
713
710
* @throws DateTimeArithmeticException if this value or the result is too large to fit in [LocalDateTime].
711
+ * @sample kotlinx.datetime.test.samples.InstantSamples.minusDateTimeUnitLong
714
712
*/
715
713
public fun Instant.minus (value : Long , unit : DateTimeUnit , timeZone : TimeZone ): Instant =
716
714
if (value != Long .MIN_VALUE ) {
@@ -725,11 +723,9 @@ public fun Instant.minus(value: Long, unit: DateTimeUnit, timeZone: TimeZone): I
725
723
* If the [value] is positive, the returned instant is later than this instant.
726
724
* If the [value] is negative, the returned instant is earlier than this instant.
727
725
*
728
- * ```
729
- * Clock.System.now().plus(5L, DateTimeUnit.HOUR) // 5 hours from now
730
- * ```
731
- *
732
726
* The return value is clamped to the platform-specific boundaries for [Instant] if the result exceeds them.
727
+ *
728
+ * @sample kotlinx.datetime.test.samples.InstantSamples.plusTimeBasedUnitLong
733
729
*/
734
730
public expect fun Instant.plus (value : Long , unit : DateTimeUnit .TimeBased ): Instant
735
731
@@ -739,11 +735,9 @@ public expect fun Instant.plus(value: Long, unit: DateTimeUnit.TimeBased): Insta
739
735
* If the [value] is positive, the returned instant is earlier than this instant.
740
736
* If the [value] is negative, the returned instant is later than this instant.
741
737
*
742
- * ```
743
- * Clock.System.now().minus(5L, DateTimeUnit.HOUR) // 5 hours earlier than now
744
- * ```
745
- *
746
738
* The return value is clamped to the platform-specific boundaries for [Instant] if the result exceeds them.
739
+ *
740
+ * @sample kotlinx.datetime.test.samples.InstantSamples.minusTimeBasedUnitLong
747
741
*/
748
742
public fun Instant.minus (value : Long , unit : DateTimeUnit .TimeBased ): Instant =
749
743
if (value != Long .MIN_VALUE ) {
@@ -759,16 +753,11 @@ public fun Instant.minus(value: Long, unit: DateTimeUnit.TimeBased): Instant =
759
753
* The value returned is negative or zero if this instant is earlier than the other,
760
754
* and positive or zero if this instant is later than the other.
761
755
*
762
- * ```
763
- * val momentOfBirth = LocalDateTime.parse("1990-02-20T12:03:53Z").toInstant(TimeZone.of("Europe/Berlin"))
764
- * val currentMoment = Clock.System.now()
765
- * val daysLived = currentMoment.minus(momentOfBirth, DateTimeUnit.DAY, TimeZone.currentSystemDefault())
766
- * ```
767
- *
768
756
* If the result does not fit in [Long], returns [Long.MAX_VALUE] for a positive result or [Long.MIN_VALUE] for a negative result.
769
757
*
770
758
* @throws DateTimeArithmeticException if `this` or [other] instant is too large to fit in [LocalDateTime].
771
759
* @see Instant.until for the same operation but with swapped arguments.
760
+ * @sample kotlinx.datetime.test.samples.InstantSamples.minusAsDateTimeUnit
772
761
*/
773
762
public fun Instant.minus (other : Instant , unit : DateTimeUnit , timeZone : TimeZone ): Long =
774
763
other.until(this , unit, timeZone)
@@ -779,15 +768,10 @@ public fun Instant.minus(other: Instant, unit: DateTimeUnit, timeZone: TimeZone)
779
768
* The value returned is negative or zero if this instant is earlier than the other,
780
769
* and positive or zero if this instant is later than the other.
781
770
*
782
- * ```
783
- * val momentOfBirth = LocalDateTime.parse("1990-02-20T12:03:53Z").toInstant(TimeZone.of("Europe/Berlin"))
784
- * val currentMoment = Clock.System.now()
785
- * val minutesLived = currentMoment.minus(momentOfBirth, DateTimeUnit.MINUTE)
786
- * ```
787
- *
788
771
* If the result does not fit in [Long], returns [Long.MAX_VALUE] for a positive result or [Long.MIN_VALUE] for a negative result.
789
772
*
790
773
* @see Instant.until for the same operation but with swapped arguments.
774
+ * @sample kotlinx.datetime.test.samples.InstantSamples.minusAsTimeBasedUnit
791
775
*/
792
776
public fun Instant.minus (other : Instant , unit : DateTimeUnit .TimeBased ): Long =
793
777
other.until(this , unit)
@@ -801,6 +785,8 @@ public fun Instant.minus(other: Instant, unit: DateTimeUnit.TimeBased): Long =
801
785
* [DateTimeComponents.Formats.ISO_DATE_TIME_OFFSET] is a format very similar to the one used by [toString].
802
786
* The only difference is that [Instant.toString] adds trailing zeros to the fraction-of-second component so that the
803
787
* number of digits after a dot is a multiple of three.
788
+ *
789
+ * @sample kotlinx.datetime.test.samples.InstantSamples.formatting
804
790
*/
805
791
public fun Instant.format (format : DateTimeFormat <DateTimeComponents >, offset : UtcOffset = UtcOffset .ZERO ): String {
806
792
val instant = this
0 commit comments