Skip to content

Commit 4801426

Browse files
committed
Finish adding samples
1 parent c24595b commit 4801426

File tree

3 files changed

+376
-67
lines changed

3 files changed

+376
-67
lines changed

core/common/src/Instant.kt

Lines changed: 51 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ public expect class Instant : Comparable<Instant> {
199199
* Note that this number doesn't include leap seconds added or removed since the epoch.
200200
*
201201
* @see fromEpochSeconds
202+
* @sample kotlinx.datetime.test.samples.InstantSamples.epochSeconds
202203
*/
203204
public val epochSeconds: Long
204205

@@ -208,6 +209,7 @@ public expect class Instant : Comparable<Instant> {
208209
* The value is always positive and lies in the range `0..999_999_999`.
209210
*
210211
* @see fromEpochSeconds
212+
* @sample kotlinx.datetime.test.samples.InstantSamples.nanosecondsOfSecond
211213
*/
212214
public val nanosecondsOfSecond: Int
213215

@@ -219,6 +221,7 @@ public expect class Instant : Comparable<Instant> {
219221
* If the result does not fit in [Long], returns [Long.MAX_VALUE] for a positive result or [Long.MIN_VALUE] for a negative result.
220222
*
221223
* @see fromEpochMilliseconds
224+
* @sample kotlinx.datetime.test.samples.InstantSamples.toEpochMilliseconds
222225
*/
223226
public fun toEpochMilliseconds(): Long
224227

@@ -234,6 +237,8 @@ public expect class Instant : Comparable<Instant> {
234237
* in `kotlinx-datetime`, adding a day is a calendar-based operation, whereas [Duration] always considers
235238
* a day to be 24 hours.
236239
* For an explanation of why this is error-prone, see [DateTimeUnit.DayBased].
240+
*
241+
* @sample kotlinx.datetime.test.samples.InstantSamples.plusDuration
237242
*/
238243
public operator fun plus(duration: Duration): Instant
239244

@@ -249,6 +254,8 @@ public expect class Instant : Comparable<Instant> {
249254
* in `kotlinx-datetime`, adding a day is a calendar-based operation, whereas [Duration] always considers
250255
* a day to be 24 hours.
251256
* For an explanation of why this is error-prone, see [DateTimeUnit.DayBased].
257+
*
258+
* @sample kotlinx.datetime.test.samples.InstantSamples.minusDuration
252259
*/
253260
public operator fun minus(duration: Duration): Instant
254261

@@ -266,6 +273,8 @@ public expect class Instant : Comparable<Instant> {
266273
* or even monotonic, so the result of this operation may be negative even if the other instant was observed later
267274
* than this one, or vice versa.
268275
* For measuring time intervals, consider using [TimeSource.Monotonic].
276+
*
277+
* @sample kotlinx.datetime.test.samples.InstantSamples.minusInstant
269278
*/
270279
public operator fun minus(other: Instant): Duration
271280

@@ -274,6 +283,8 @@ public expect class Instant : Comparable<Instant> {
274283
* Returns zero if this instant represents the same moment as the other (i.e., equal to other),
275284
* a negative number if this instant is earlier than the other,
276285
* and a positive number if this instant is later than the other.
286+
*
287+
* @sample kotlinx.datetime.test.samples.InstantSamples.compareToSample
277288
*/
278289
public override operator fun compareTo(other: Instant): Int
279290

@@ -289,6 +300,7 @@ public expect class Instant : Comparable<Instant> {
289300
* @see DateTimeComponents.Formats.ISO_DATE_TIME_OFFSET for a very similar format. The difference is that
290301
* [DateTimeComponents.Formats.ISO_DATE_TIME_OFFSET] will not add trailing zeros for readability to the
291302
* fractional part of the second.
303+
* @sample kotlinx.datetime.test.samples.InstantSamples.toStringSample
292304
*/
293305
public override fun toString(): String
294306

@@ -306,6 +318,7 @@ public expect class Instant : Comparable<Instant> {
306318
* Note that [Instant] also supports nanosecond precision via [fromEpochSeconds].
307319
*
308320
* @see Instant.toEpochMilliseconds
321+
* @sample kotlinx.datetime.test.samples.InstantSamples.fromEpochMilliseconds
309322
*/
310323
public fun fromEpochMilliseconds(epochMilliseconds: Long): Instant
311324

@@ -320,6 +333,7 @@ public expect class Instant : Comparable<Instant> {
320333
*
321334
* @see Instant.epochSeconds
322335
* @see Instant.nanosecondsOfSecond
336+
* @sample kotlinx.datetime.test.samples.InstantSamples.fromEpochSeconds
323337
*/
324338
public fun fromEpochSeconds(epochSeconds: Long, nanosecondAdjustment: Long = 0): Instant
325339

@@ -334,6 +348,7 @@ public expect class Instant : Comparable<Instant> {
334348
*
335349
* @see Instant.epochSeconds
336350
* @see Instant.nanosecondsOfSecond
351+
* @sample kotlinx.datetime.test.samples.InstantSamples.fromEpochSecondsIntNanos
337352
*/
338353
public fun fromEpochSeconds(epochSeconds: Long, nanosecondAdjustment: Int): Instant
339354

@@ -356,6 +371,7 @@ public expect class Instant : Comparable<Instant> {
356371
*
357372
* @see Instant.toString for formatting using the default format.
358373
* @see Instant.format for formatting using a custom format.
374+
* @sample kotlinx.datetime.test.samples.InstantSamples.parsing
359375
*/
360376
public fun parse(
361377
input: CharSequence,
@@ -387,11 +403,19 @@ public expect class Instant : Comparable<Instant> {
387403
}
388404
}
389405

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+
*/
391411
public val Instant.isDistantPast: Boolean
392412
get() = this <= Instant.DISTANT_PAST
393413

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+
*/
395419
public val Instant.isDistantFuture: Boolean
396420
get() = this >= Instant.DISTANT_FUTURE
397421

@@ -412,12 +436,9 @@ public fun String.toInstant(): Instant = Instant.parse(this)
412436
* please consider using a multiple of [DateTimeUnit.DAY] or [DateTimeUnit.MONTH], like in
413437
* `Clock.System.now().plus(5, DateTimeUnit.DAY, TimeZone.currentSystemDefault())`.
414438
*
415-
* ```
416-
* Clock.System.now().plus(DateTimePeriod(months = 1, days = -1), TimeZone.UTC) // one day short from a month later
417-
* ```
418-
*
419439
* @throws DateTimeArithmeticException if this value or the results of intermediate computations are too large to fit in
420440
* [LocalDateTime].
441+
* @sample kotlinx.datetime.test.samples.InstantSamples.plusPeriod
421442
*/
422443
public expect fun Instant.plus(period: DateTimePeriod, timeZone: TimeZone): Instant
423444

@@ -432,12 +453,9 @@ public expect fun Instant.plus(period: DateTimePeriod, timeZone: TimeZone): Inst
432453
* please consider using a multiple of [DateTimeUnit.DAY] or [DateTimeUnit.MONTH], as in
433454
* `Clock.System.now().minus(5, DateTimeUnit.DAY, TimeZone.currentSystemDefault())`.
434455
*
435-
* ```
436-
* Clock.System.now().minus(DateTimePeriod(months = 1, days = -1), TimeZone.UTC) // one day short from a month earlier
437-
* ```
438-
*
439456
* @throws DateTimeArithmeticException if this value or the results of intermediate computations are too large to fit in
440457
* [LocalDateTime].
458+
* @sample kotlinx.datetime.test.samples.InstantSamples.minusPeriod
441459
*/
442460
public fun Instant.minus(period: DateTimePeriod, timeZone: TimeZone): Instant =
443461
/* 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 =
463481
*
464482
* @throws DateTimeArithmeticException if `this` or [other] instant is too large to fit in [LocalDateTime].
465483
* 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
466485
*/
467486
public expect fun Instant.periodUntil(other: Instant, timeZone: TimeZone): DateTimePeriod
468487

@@ -477,13 +496,8 @@ public expect fun Instant.periodUntil(other: Instant, timeZone: TimeZone): DateT
477496
*
478497
* If the result does not fit in [Long], returns [Long.MAX_VALUE] for a positive result or [Long.MIN_VALUE] for a negative result.
479498
*
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-
*
486499
* @throws DateTimeArithmeticException if `this` or [other] instant is too large to fit in [LocalDateTime].
500+
* @sample kotlinx.datetime.test.samples.InstantSamples.untilAsDateTimeUnit
487501
*/
488502
public expect fun Instant.until(other: Instant, unit: DateTimeUnit, timeZone: TimeZone): Long
489503

@@ -497,11 +511,7 @@ public expect fun Instant.until(other: Instant, unit: DateTimeUnit, timeZone: Ti
497511
*
498512
* If the result does not fit in [Long], returns [Long.MAX_VALUE] for a positive result or [Long.MIN_VALUE] for a negative result.
499513
*
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
505515
*/
506516
public fun Instant.until(other: Instant, unit: DateTimeUnit.TimeBased): Long =
507517
try {
@@ -520,6 +530,7 @@ public fun Instant.until(other: Instant, unit: DateTimeUnit.TimeBased): Long =
520530
*
521531
* @see Instant.until
522532
* @throws DateTimeArithmeticException if `this` or [other] instant is too large to fit in [LocalDateTime].
533+
* @sample kotlinx.datetime.test.samples.InstantSamples.daysUntil
523534
*/
524535
public fun Instant.daysUntil(other: Instant, timeZone: TimeZone): Int =
525536
until(other, DateTimeUnit.DAY, timeZone).clampToInt()
@@ -531,6 +542,7 @@ public fun Instant.daysUntil(other: Instant, timeZone: TimeZone): Int =
531542
*
532543
* @see Instant.until
533544
* @throws DateTimeArithmeticException if `this` or [other] instant is too large to fit in [LocalDateTime].
545+
* @sample kotlinx.datetime.test.samples.InstantSamples.monthsUntil
534546
*/
535547
public fun Instant.monthsUntil(other: Instant, timeZone: TimeZone): Int =
536548
until(other, DateTimeUnit.MONTH, timeZone).clampToInt()
@@ -542,6 +554,7 @@ public fun Instant.monthsUntil(other: Instant, timeZone: TimeZone): Int =
542554
*
543555
* @see Instant.until
544556
* @throws DateTimeArithmeticException if `this` or [other] instant is too large to fit in [LocalDateTime].
557+
* @sample kotlinx.datetime.test.samples.InstantSamples.yearsUntil
545558
*/
546559
public fun Instant.yearsUntil(other: Instant, timeZone: TimeZone): Int =
547560
until(other, DateTimeUnit.YEAR, timeZone).clampToInt()
@@ -559,6 +572,7 @@ public fun Instant.yearsUntil(other: Instant, timeZone: TimeZone): Int =
559572
* @throws DateTimeArithmeticException if `this` or [other] instant is too large to fit in [LocalDateTime].
560573
* Or (only on the JVM) if the number of months between the two dates exceeds an Int.
561574
* @see Instant.periodUntil
575+
* @sample kotlinx.datetime.test.samples.InstantSamples.minusInstantInZone
562576
*/
563577
public fun Instant.minus(other: Instant, timeZone: TimeZone): DateTimePeriod =
564578
other.periodUntil(this, timeZone)
@@ -619,11 +633,8 @@ public fun Instant.minus(unit: DateTimeUnit.TimeBased): Instant =
619633
* Note that the time zone does not need to be passed when the [unit] is a time-based unit.
620634
* It is also not needed when adding date-based units to a [LocalDate].
621635
*
622-
* ```
623-
* Clock.System.now().plus(5, DateTimeUnit.DAY, TimeZone.of("Europe/Berlin")) // 5 days from now in Berlin
624-
* ```
625-
*
626636
* @throws DateTimeArithmeticException if this value or the result is too large to fit in [LocalDateTime].
637+
* @sample kotlinx.datetime.test.samples.InstantSamples.plusDateTimeUnit
627638
*/
628639
public expect fun Instant.plus(value: Int, unit: DateTimeUnit, timeZone: TimeZone): Instant
629640

@@ -637,14 +648,11 @@ public expect fun Instant.plus(value: Int, unit: DateTimeUnit, timeZone: TimeZon
637648
* Note that the time zone does not need to be passed when the [unit] is a time-based unit.
638649
* It is also not needed when subtracting date-based units from a [LocalDate].
639650
*
640-
* ```
641-
* Clock.System.now().minus(5, DateTimeUnit.DAY, TimeZone.of("Europe/Berlin")) // 5 days earlier than now in Berlin
642-
* ```
643-
*
644651
* If the [value] is positive, the returned instant is earlier than this instant.
645652
* If the [value] is negative, the returned instant is later than this instant.
646653
*
647654
* @throws DateTimeArithmeticException if this value or the result is too large to fit in [LocalDateTime].
655+
* @sample kotlinx.datetime.test.samples.InstantSamples.minusDateTimeUnit
648656
*/
649657
public expect fun Instant.minus(value: Int, unit: DateTimeUnit, timeZone: TimeZone): Instant
650658

@@ -654,11 +662,9 @@ public expect fun Instant.minus(value: Int, unit: DateTimeUnit, timeZone: TimeZo
654662
* If the [value] is positive, the returned instant is later than this instant.
655663
* If the [value] is negative, the returned instant is earlier than this instant.
656664
*
657-
* ```
658-
* Clock.System.now().plus(5, DateTimeUnit.HOUR) // 5 hours from now
659-
* ```
660-
*
661665
* 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
662668
*/
663669
public fun Instant.plus(value: Int, unit: DateTimeUnit.TimeBased): Instant =
664670
plus(value.toLong(), unit)
@@ -669,11 +675,9 @@ public fun Instant.plus(value: Int, unit: DateTimeUnit.TimeBased): Instant =
669675
* If the [value] is positive, the returned instant is earlier than this instant.
670676
* If the [value] is negative, the returned instant is later than this instant.
671677
*
672-
* ```
673-
* Clock.System.now().minus(5, DateTimeUnit.HOUR) // 5 hours earlier than now
674-
* ```
675-
*
676678
* 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
677681
*/
678682
public fun Instant.minus(value: Int, unit: DateTimeUnit.TimeBased): Instant =
679683
minus(value.toLong(), unit)
@@ -688,11 +692,8 @@ public fun Instant.minus(value: Int, unit: DateTimeUnit.TimeBased): Instant =
688692
* Note that the time zone does not need to be passed when the [unit] is a time-based unit.
689693
* It is also not needed when adding date-based units to a [LocalDate].
690694
*
691-
* ```
692-
* Clock.System.now().plus(5L, DateTimeUnit.DAY, TimeZone.of("Europe/Berlin")) // 5 days from now in Berlin
693-
* ```
694-
*
695695
* @throws DateTimeArithmeticException if this value or the result is too large to fit in [LocalDateTime].
696+
* @sample kotlinx.datetime.test.samples.InstantSamples.plusDateTimeUnitLong
696697
*/
697698
public expect fun Instant.plus(value: Long, unit: DateTimeUnit, timeZone: TimeZone): Instant
698699

@@ -706,11 +707,8 @@ public expect fun Instant.plus(value: Long, unit: DateTimeUnit, timeZone: TimeZo
706707
* Note that the time zone does not need to be passed when the [unit] is a time-based unit.
707708
* It is also not needed when subtracting date-based units from a [LocalDate].
708709
*
709-
* ```
710-
* Clock.System.now().minus(5L, DateTimeUnit.DAY, TimeZone.of("Europe/Berlin")) // 5 days earlier than now in Berlin
711-
* ```
712-
*
713710
* @throws DateTimeArithmeticException if this value or the result is too large to fit in [LocalDateTime].
711+
* @sample kotlinx.datetime.test.samples.InstantSamples.minusDateTimeUnitLong
714712
*/
715713
public fun Instant.minus(value: Long, unit: DateTimeUnit, timeZone: TimeZone): Instant =
716714
if (value != Long.MIN_VALUE) {
@@ -725,11 +723,9 @@ public fun Instant.minus(value: Long, unit: DateTimeUnit, timeZone: TimeZone): I
725723
* If the [value] is positive, the returned instant is later than this instant.
726724
* If the [value] is negative, the returned instant is earlier than this instant.
727725
*
728-
* ```
729-
* Clock.System.now().plus(5L, DateTimeUnit.HOUR) // 5 hours from now
730-
* ```
731-
*
732726
* 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
733729
*/
734730
public expect fun Instant.plus(value: Long, unit: DateTimeUnit.TimeBased): Instant
735731

@@ -739,11 +735,9 @@ public expect fun Instant.plus(value: Long, unit: DateTimeUnit.TimeBased): Insta
739735
* If the [value] is positive, the returned instant is earlier than this instant.
740736
* If the [value] is negative, the returned instant is later than this instant.
741737
*
742-
* ```
743-
* Clock.System.now().minus(5L, DateTimeUnit.HOUR) // 5 hours earlier than now
744-
* ```
745-
*
746738
* 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
747741
*/
748742
public fun Instant.minus(value: Long, unit: DateTimeUnit.TimeBased): Instant =
749743
if (value != Long.MIN_VALUE) {
@@ -759,16 +753,11 @@ public fun Instant.minus(value: Long, unit: DateTimeUnit.TimeBased): Instant =
759753
* The value returned is negative or zero if this instant is earlier than the other,
760754
* and positive or zero if this instant is later than the other.
761755
*
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-
*
768756
* If the result does not fit in [Long], returns [Long.MAX_VALUE] for a positive result or [Long.MIN_VALUE] for a negative result.
769757
*
770758
* @throws DateTimeArithmeticException if `this` or [other] instant is too large to fit in [LocalDateTime].
771759
* @see Instant.until for the same operation but with swapped arguments.
760+
* @sample kotlinx.datetime.test.samples.InstantSamples.minusAsDateTimeUnit
772761
*/
773762
public fun Instant.minus(other: Instant, unit: DateTimeUnit, timeZone: TimeZone): Long =
774763
other.until(this, unit, timeZone)
@@ -779,15 +768,10 @@ public fun Instant.minus(other: Instant, unit: DateTimeUnit, timeZone: TimeZone)
779768
* The value returned is negative or zero if this instant is earlier than the other,
780769
* and positive or zero if this instant is later than the other.
781770
*
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-
*
788771
* If the result does not fit in [Long], returns [Long.MAX_VALUE] for a positive result or [Long.MIN_VALUE] for a negative result.
789772
*
790773
* @see Instant.until for the same operation but with swapped arguments.
774+
* @sample kotlinx.datetime.test.samples.InstantSamples.minusAsTimeBasedUnit
791775
*/
792776
public fun Instant.minus(other: Instant, unit: DateTimeUnit.TimeBased): Long =
793777
other.until(this, unit)
@@ -801,6 +785,8 @@ public fun Instant.minus(other: Instant, unit: DateTimeUnit.TimeBased): Long =
801785
* [DateTimeComponents.Formats.ISO_DATE_TIME_OFFSET] is a format very similar to the one used by [toString].
802786
* The only difference is that [Instant.toString] adds trailing zeros to the fraction-of-second component so that the
803787
* number of digits after a dot is a multiple of three.
788+
*
789+
* @sample kotlinx.datetime.test.samples.InstantSamples.formatting
804790
*/
805791
public fun Instant.format(format: DateTimeFormat<DateTimeComponents>, offset: UtcOffset = UtcOffset.ZERO): String {
806792
val instant = this

0 commit comments

Comments
 (0)