@@ -52,25 +52,20 @@ import kotlinx.serialization.Serializable
52
52
* will be returned if all time components happen to be zero.
53
53
*
54
54
* A `DateTimePeriod` can be constructed using the constructor function with the same name.
55
- *
56
- * ```
57
- * val dateTimePeriod = DateTimePeriod(months = 24, days = -3)
58
- * val datePeriod = dateTimePeriod as DatePeriod // the same as DatePeriod(years = 2, days = -3)
59
- * ```
55
+ * See sample 1.
60
56
*
61
57
* [parse] and [toString] methods can be used to obtain a [DateTimePeriod] from and convert it to a string in the
62
58
* ISO 8601 extended format.
63
- *
64
- * ```
65
- * val dateTimePeriod = DateTimePeriod.parse("P1Y2M6DT13H1S") // 1 year, 2 months, 6 days, 13 hours, 1 second
66
- * val string = dateTimePeriod.toString() // "P1Y2M6DT13H1S"
67
- * ```
59
+ * See sample 2.
68
60
*
69
61
* `DateTimePeriod` can also be returned as the result of instant arithmetic operations (see [Instant.periodUntil]).
70
62
*
71
63
* Additionally, there are several `kotlinx-serialization` serializers for [DateTimePeriod]:
72
64
* - [DateTimePeriodIso8601Serializer] for the ISO 8601 format;
73
65
* - [DateTimePeriodComponentSerializer] for an object with components.
66
+ *
67
+ * @sample kotlinx.datetime.test.samples.DateTimePeriodSamples.construction
68
+ * @sample kotlinx.datetime.test.samples.DateTimePeriodSamples.simpleParsingAndFormatting
74
69
*/
75
70
@Serializable(with = DateTimePeriodIso8601Serializer ::class )
76
71
// TODO: could be error-prone without explicitly named params
@@ -82,40 +77,54 @@ public sealed class DateTimePeriod {
82
77
*
83
78
* Note that a calendar day is not identical to 24 hours, see [DateTimeUnit.DayBased] for details.
84
79
* Also, this field does not overflow into months, so values larger than 31 can be present.
80
+ *
81
+ * @sample kotlinx.datetime.test.samples.DateTimePeriodSamples.valueNormalization
85
82
*/
86
83
public abstract val days: Int
87
84
internal abstract val totalNanoseconds: Long
88
85
89
86
/* *
90
87
* The number of whole years. Can be negative.
88
+ *
89
+ * @sample kotlinx.datetime.test.samples.DateTimePeriodSamples.valueNormalization
91
90
*/
92
91
public val years: Int get() = totalMonths / 12
93
92
94
93
/* *
95
94
* The number of months in this period that don't form a whole year, so this value is always in `(-11..11)`.
95
+ *
96
+ * @sample kotlinx.datetime.test.samples.DateTimePeriodSamples.valueNormalization
96
97
*/
97
98
public val months: Int get() = totalMonths % 12
98
99
99
100
/* *
100
101
* The number of whole hours in this period. Can be negative.
101
102
*
102
103
* This field does not overflow into days, so values larger than 23 or smaller than -23 can be present.
104
+ *
105
+ * @sample kotlinx.datetime.test.samples.DateTimePeriodSamples.valueNormalization
103
106
*/
104
107
public open val hours: Int get() = (totalNanoseconds / 3_600_000_000_000 ).toInt()
105
108
106
109
/* *
107
110
* The number of whole minutes in this period that don't form a whole hour, so this value is always in `(-59..59)`.
111
+ *
112
+ * @sample kotlinx.datetime.test.samples.DateTimePeriodSamples.valueNormalization
108
113
*/
109
114
public open val minutes: Int get() = ((totalNanoseconds % 3_600_000_000_000 ) / 60_000_000_000 ).toInt()
110
115
111
116
/* *
112
117
* The number of whole seconds in this period that don't form a whole minute, so this value is always in `(-59..59)`.
118
+ *
119
+ * @sample kotlinx.datetime.test.samples.DateTimePeriodSamples.valueNormalization
113
120
*/
114
121
public open val seconds: Int get() = ((totalNanoseconds % 60_000_000_000 ) / NANOS_PER_ONE ).toInt()
115
122
116
123
/* *
117
124
* The number of whole nanoseconds in this period that don't form a whole second, so this value is always in
118
125
* `(-999_999_999..999_999_999)`.
126
+ *
127
+ * @sample kotlinx.datetime.test.samples.DateTimePeriodSamples.valueNormalization
119
128
*/
120
129
public open val nanoseconds: Int get() = (totalNanoseconds % NANOS_PER_ONE ).toInt()
121
130
@@ -136,6 +145,7 @@ public sealed class DateTimePeriod {
136
145
* minus four seconds, minus 123456789 nanoseconds;
137
146
*
138
147
* @see DateTimePeriod.parse for the detailed description of the format.
148
+ * @sample kotlinx.datetime.test.samples.DateTimePeriodSamples.toStringSample
139
149
*/
140
150
override fun toString (): String = buildString {
141
151
val sign = if (allNonpositive()) { append(' -' ); - 1 } else 1
@@ -215,6 +225,7 @@ public sealed class DateTimePeriod {
215
225
*
216
226
* @throws IllegalArgumentException if the text cannot be parsed or the boundaries of [DateTimePeriod] are
217
227
* exceeded.
228
+ * @sample kotlinx.datetime.test.samples.DateTimePeriodSamples.parsing
218
229
*/
219
230
public fun parse (text : String ): DateTimePeriod {
220
231
fun parseException (message : String , position : Int ): Nothing =
@@ -400,14 +411,10 @@ public fun String.toDateTimePeriod(): DateTimePeriod = DateTimePeriod.parse(this
400
411
* are not zero, and [DatePeriodIso8601Serializer] and [DatePeriodComponentSerializer], mirroring those of
401
412
* [DateTimePeriod].
402
413
*
403
- * ```
404
- * val datePeriod1 = DatePeriod(years = 1, days = 3)
405
- * val string = datePeriod1.toString() // "P1Y3D"
406
- * val datePeriod2 = DatePeriod.parse(string) // 1 year and 3 days
407
- * ```
408
- *
409
414
* `DatePeriod` values are used in operations on [LocalDates][LocalDate] and are returned from operations
410
415
* on [LocalDates][LocalDate], but they also can be passed anywhere a [DateTimePeriod] is expected.
416
+ *
417
+ * @sample kotlinx.datetime.test.samples.DateTimePeriodSamples.DatePeriodSamples.simpleParsingAndFormatting
411
418
*/
412
419
@Serializable(with = DatePeriodIso8601Serializer ::class )
413
420
public class DatePeriod internal constructor(
@@ -428,6 +435,7 @@ public class DatePeriod internal constructor(
428
435
* For example, instead of `DatePeriod(months = 6)`, one can use `DateTimeUnit.MONTH * 6`.
429
436
*
430
437
* @throws IllegalArgumentException if the total number of months in [years] and [months] overflows an [Int].
438
+ * @sample kotlinx.datetime.test.samples.DateTimePeriodSamples.DatePeriodSamples.construction
431
439
*/
432
440
public constructor (years: Int = 0 , months: Int = 0 , days: Int = 0 ): this (totalMonths(years, months), days)
433
441
// avoiding excessive computations
@@ -455,6 +463,7 @@ public class DatePeriod internal constructor(
455
463
* or any time components are not zero.
456
464
*
457
465
* @see DateTimePeriod.parse
466
+ * @sample kotlinx.datetime.test.samples.DateTimePeriodSamples.DatePeriodSamples.parsing
458
467
*/
459
468
public fun parse (text : String ): DatePeriod =
460
469
when (val period = DateTimePeriod .parse(text)) {
@@ -521,6 +530,7 @@ internal fun buildDateTimePeriod(totalMonths: Int = 0, days: Int = 0, totalNanos
521
530
* @throws IllegalArgumentException if the total number of months in [years] and [months] overflows an [Int].
522
531
* @throws IllegalArgumentException if the total number of months in [hours], [minutes], [seconds] and [nanoseconds]
523
532
* overflows a [Long].
533
+ * @sample kotlinx.datetime.test.samples.DateTimePeriodSamples.constructorFunction
524
534
*/
525
535
public fun DateTimePeriod (
526
536
years : Int = 0,
@@ -544,16 +554,17 @@ public fun DateTimePeriod(
544
554
* whereas in `kotlinx-datetime`, a day is a calendar day, which can be different from 24 hours.
545
555
* See [DateTimeUnit.DayBased] for details.
546
556
*
547
- * ```
548
- * 2.days.toDateTimePeriod() // 0 days, 48 hours
549
- * ```
557
+ * @sample kotlinx.datetime.test.samples.DateTimePeriodSamples.durationToDateTimePeriod
550
558
*/
551
559
// TODO: maybe it's more consistent to throw here on overflow?
552
560
public fun Duration.toDateTimePeriod (): DateTimePeriod = buildDateTimePeriod(totalNanoseconds = inWholeNanoseconds)
553
561
554
562
/* *
555
563
* Adds two [DateTimePeriod] instances.
556
564
*
565
+ * **Pitfall**: given three instants, adding together the periods between the first and the second and between the
566
+ * second and the third *does not* necessarily equal the period between the first and the third.
567
+ *
557
568
* @throws DateTimeArithmeticException if arithmetic overflow happens.
558
569
*/
559
570
public operator fun DateTimePeriod.plus (other : DateTimePeriod ): DateTimePeriod = buildDateTimePeriod(
@@ -565,6 +576,9 @@ public operator fun DateTimePeriod.plus(other: DateTimePeriod): DateTimePeriod =
565
576
/* *
566
577
* Adds two [DatePeriod] instances.
567
578
*
579
+ * **Pitfall**: given three dates, adding together the periods between the first and the second and between the
580
+ * second and the third *does not* necessarily equal the period between the first and the third.
581
+ *
568
582
* @throws DateTimeArithmeticException if arithmetic overflow happens.
569
583
*/
570
584
public operator fun DatePeriod.plus (other : DatePeriod ): DatePeriod = DatePeriod (
0 commit comments