Skip to content

Commit c24595b

Browse files
committed
Samples for LocalDate
1 parent d3aa236 commit c24595b

File tree

2 files changed

+344
-73
lines changed

2 files changed

+344
-73
lines changed

core/common/src/LocalDate.kt

Lines changed: 70 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -32,44 +32,27 @@ import kotlinx.serialization.Serializable
3232
* ### Construction, serialization, and deserialization
3333
*
3434
* [LocalDate] can be constructed directly from its components, using the constructor.
35-
*
36-
* ```
37-
* LocalDate(year = 2023, monthNumber = 1, dayOfMonth = 2) == LocalDate(2023, Month.JANUARY, 2)
38-
* ```
35+
* See sample 1.
3936
*
4037
* [fromEpochDays] can be used to obtain a [LocalDate] from the number of days since the epoch day `1970-01-01`;
4138
* [toEpochDays] is the inverse operation.
42-
*
43-
* ```
44-
* LocalDate.fromEpochDays(0) == LocalDate(1970, Month.JANUARY, 1)
45-
* LocalDate(1970, Month.JANUARY, 31).toEpochDays() == 30
46-
* ```
39+
* See sample 2.
4740
*
4841
* [parse] and [toString] methods can be used to obtain a [LocalDate] from and convert it to a string in the
4942
* ISO 8601 extended format.
50-
*
51-
* ```
52-
* LocalDate.parse("2023-01-02") == LocalDate(2023, Month.JANUARY, 2)
53-
* LocalDate(2023, Month.JANUARY, 2).toString() == "2023-01-02"
54-
* ```
43+
* See sample 3.
5544
*
5645
* [parse] and [LocalDate.format] both support custom formats created with [Format] or defined in [Formats].
57-
*
58-
* ```
59-
* val customFormat = LocalDate.Format {
60-
* monthName(MonthNames.ENGLISH_ABBREVIATED)
61-
* char(' ')
62-
* dayOfMonth()
63-
* char(' ')
64-
* year()
65-
* }
66-
* LocalDate.parse("Jan 05 2020", customFormat) == LocalDate(2020, Month.JANUARY, 5)
67-
* LocalDate(2020, Month.JANUARY, 5).format(customFormat) == "Jan 05 2020"
68-
* ```
46+
* See sample 4.
6947
*
7048
* Additionally, there are several `kotlinx-serialization` serializers for [LocalDate]:
7149
* - [LocalDateIso8601Serializer] for the ISO 8601 extended format,
7250
* - [LocalDateComponentSerializer] for an object with components.
51+
*
52+
* @sample kotlinx.datetime.test.samples.LocalDateSamples.constructorFunctionMonthNumber
53+
* @sample kotlinx.datetime.test.samples.LocalDateSamples.fromAndToEpochDays
54+
* @sample kotlinx.datetime.test.samples.LocalDateSamples.simpleParsingAndFormatting
55+
* @sample kotlinx.datetime.test.samples.LocalDateSamples.customFormat
7356
*/
7457
@Serializable(with = LocalDateIso8601Serializer::class)
7558
public expect class LocalDate : Comparable<LocalDate> {
@@ -86,39 +69,29 @@ public expect class LocalDate : Comparable<LocalDate> {
8669
*
8770
* @see LocalDate.toString for formatting using the default format.
8871
* @see LocalDate.format for formatting using a custom format.
72+
* @sample kotlinx.datetime.test.samples.LocalDateSamples.parsing
8973
*/
9074
public fun parse(input: CharSequence, format: DateTimeFormat<LocalDate> = getIsoDateFormat()): LocalDate
9175

9276
/**
9377
* Returns a [LocalDate] that is [epochDays] number of days from the epoch day `1970-01-01`.
9478
*
9579
* @throws IllegalArgumentException if the result exceeds the platform-specific boundaries of [LocalDate].
96-
*
9780
* @see LocalDate.toEpochDays
81+
* @sample kotlinx.datetime.test.samples.LocalDateSamples.fromAndToEpochDays
9882
*/
9983
public fun fromEpochDays(epochDays: Int): LocalDate
10084

10185
/**
10286
* Creates a new format for parsing and formatting [LocalDate] values.
10387
*
104-
* Example:
105-
* ```
106-
* // 2020 Jan 05
107-
* LocalDate.Format {
108-
* year()
109-
* char(' ')
110-
* monthName(MonthNames.ENGLISH_ABBREVIATED)
111-
* char(' ')
112-
* dayOfMonth()
113-
* }
114-
* ```
115-
*
11688
* Only parsing and formatting of well-formed values is supported. If the input does not fit the boundaries
11789
* (for example, [dayOfMonth] is 31 for February), consider using [DateTimeComponents.Format] instead.
11890
*
11991
* There is a collection of predefined formats in [LocalDate.Formats].
12092
*
12193
* @throws IllegalArgumentException if parsing using this format is ambiguous.
94+
* @sample kotlinx.datetime.test.samples.LocalDateSamples.customFormat
12295
*/
12396
@Suppress("FunctionName")
12497
public fun Format(block: DateTimeFormatBuilder.WithDate.() -> Unit): DateTimeFormat<LocalDate>
@@ -146,6 +119,8 @@ public expect class LocalDate : Comparable<LocalDate> {
146119
* - `+12020-08-30`
147120
* - `0000-08-30`
148121
* - `-0001-08-30`
122+
*
123+
* @sample kotlinx.datetime.test.samples.LocalDateSamples.Formats.iso
149124
*/
150125
public val ISO: DateTimeFormat<LocalDate>
151126

@@ -157,6 +132,8 @@ public expect class LocalDate : Comparable<LocalDate> {
157132
* - `+120200830`
158133
* - `00000830`
159134
* - `-00010830`
135+
*
136+
* @sample kotlinx.datetime.test.samples.LocalDateSamples.Formats.isoBasic
160137
*/
161138
public val ISO_BASIC: DateTimeFormat<LocalDate>
162139
}
@@ -174,6 +151,7 @@ public expect class LocalDate : Comparable<LocalDate> {
174151
*
175152
* @throws IllegalArgumentException if any parameter is out of range, or if [dayOfMonth] is invalid for the
176153
* given [monthNumber] and [year].
154+
* @sample kotlinx.datetime.test.samples.LocalDateSamples.constructorFunctionMonthNumber
177155
*/
178156
public constructor(year: Int, monthNumber: Int, dayOfMonth: Int)
179157

@@ -188,25 +166,50 @@ public expect class LocalDate : Comparable<LocalDate> {
188166
*
189167
* @throws IllegalArgumentException if any parameter is out of range, or if [dayOfMonth] is invalid for the
190168
* given [month] and [year].
169+
* @sample kotlinx.datetime.test.samples.LocalDateSamples.constructorFunction
191170
*/
192171
public constructor(year: Int, month: Month, dayOfMonth: Int)
193172

194-
/** Returns the year component of the date. */
173+
/**
174+
* Returns the year component of the date.
175+
*
176+
* @sample kotlinx.datetime.test.samples.LocalDateSamples.year
177+
*/
195178
public val year: Int
196179

197-
/** Returns the number-of-the-month (1..12) component of the date. */
180+
/**
181+
* Returns the number-of-the-month (1..12) component of the date.
182+
*
183+
* Shortcut for `month.number`.
184+
*/
198185
public val monthNumber: Int
199186

200-
/** Returns the month ([Month]) component of the date. */
187+
/**
188+
* Returns the month ([Month]) component of the date.
189+
*
190+
* @sample kotlinx.datetime.test.samples.LocalDateSamples.month
191+
*/
201192
public val month: Month
202193

203-
/** Returns the day-of-month component of the date. */
194+
/**
195+
* Returns the day-of-month (`1..31`) component of the date.
196+
*
197+
* @sample kotlinx.datetime.test.samples.LocalDateSamples.dayOfMonth
198+
*/
204199
public val dayOfMonth: Int
205200

206-
/** Returns the day-of-week component of the date. */
201+
/**
202+
* Returns the day-of-week component of the date.
203+
*
204+
* @sample kotlinx.datetime.test.samples.LocalDateSamples.dayOfWeek
205+
*/
207206
public val dayOfWeek: DayOfWeek
208207

209-
/** Returns the day-of-year component of the date. */
208+
/**
209+
* Returns the day-of-year (`1..366`) component of the date.
210+
*
211+
* @sample kotlinx.datetime.test.samples.LocalDateSamples.dayOfYear
212+
*/
210213
public val dayOfYear: Int
211214

212215
/**
@@ -215,6 +218,7 @@ public expect class LocalDate : Comparable<LocalDate> {
215218
* If the result does not fit in [Int], returns [Int.MAX_VALUE] for a positive result or [Int.MIN_VALUE] for a negative result.
216219
*
217220
* @see LocalDate.fromEpochDays
221+
* @sample kotlinx.datetime.test.samples.LocalDateSamples.toEpochDays
218222
*/
219223
public fun toEpochDays(): Int
220224

@@ -223,6 +227,8 @@ public expect class LocalDate : Comparable<LocalDate> {
223227
* Returns zero if this date represents the same day as the other (i.e., equal to other),
224228
* a negative number if this date is earlier than the other,
225229
* and a positive number if this date is later than the other.
230+
*
231+
* @sample kotlinx.datetime.test.samples.LocalDateSamples.compareToSample
226232
*/
227233
public override fun compareTo(other: LocalDate): Int
228234

@@ -232,13 +238,16 @@ public expect class LocalDate : Comparable<LocalDate> {
232238
* @see Formats.ISO for the format details.
233239
* @see parse for the dual operation: obtaining [LocalDate] from a string.
234240
* @see LocalDate.format for formatting using a custom format.
241+
* @sample kotlinx.datetime.test.samples.LocalDateSamples.toStringSample
235242
*/
236243
public override fun toString(): String
237244
}
238245

239246
/**
240247
* Formats this value using the given [format].
241248
* Equivalent to calling [DateTimeFormat.format] on [format] with `this`.
249+
*
250+
* @sample kotlinx.datetime.test.samples.LocalDateSamples.formatting
242251
*/
243252
public fun LocalDate.format(format: DateTimeFormat<LocalDate>): String = format.format(this)
244253

@@ -253,6 +262,8 @@ public fun String.toLocalDate(): LocalDate = LocalDate.parse(this)
253262
*
254263
* For finding an instant that corresponds to the start of a date in a particular time zone consider using
255264
* [LocalDate.atStartOfDayIn] function because a day does not always start at the fixed time 0:00:00.
265+
*
266+
* @sample kotlinx.datetime.test.samples.LocalDateSamples.atTimeInline
256267
*/
257268
public fun LocalDate.atTime(hour: Int, minute: Int, second: Int = 0, nanosecond: Int = 0): LocalDateTime =
258269
LocalDateTime(year, monthNumber, dayOfMonth, hour, minute, second, nanosecond)
@@ -267,6 +278,8 @@ public fun LocalDate.atTime(hour: Int, minute: Int, second: Int = 0, nanosecond:
267278
* exist in the implicit time zone.
268279
* For example, `LocalDate(2021, 3, 28).atTime(LocalTime(2, 16, 20))` will successfully create a [LocalDateTime],
269280
* even though in Berlin, times between 2:00 and 3:00 do not exist on March 28, 2021 due to the transition to DST.
281+
*
282+
* @sample kotlinx.datetime.test.samples.LocalDateSamples.atTime
270283
*/
271284
public fun LocalDate.atTime(time: LocalTime): LocalDateTime = LocalDateTime(this, time)
272285

@@ -275,33 +288,21 @@ public fun LocalDate.atTime(time: LocalTime): LocalDateTime = LocalDateTime(this
275288
* Returns a date that is the result of adding components of [DatePeriod] to this date. The components are
276289
* added in the order from the largest units to the smallest: first years and months, then days.
277290
*
278-
* ```
279-
* LocalDate(2023, Month.JANUARY, 30) + DatePeriod(years = 1, months = 2, days = 2) == LocalDate(2024, Month.APRIL, 1)
280-
* // 2023-01-30 + 1 year = 2024-01-30
281-
* // 2024-01-30 + 2 months = 2024-03-30
282-
* // 2024-03-30 + 2 days = 2024-04-01
283-
* ```
284-
*
285291
* @see LocalDate.periodUntil
286292
* @throws DateTimeArithmeticException if this value or the results of intermediate computations are too large to fit in
287293
* [LocalDate].
294+
* @sample kotlinx.datetime.test.samples.LocalDateSamples.plusPeriod
288295
*/
289296
public expect operator fun LocalDate.plus(period: DatePeriod): LocalDate
290297

291298
/**
292299
* Returns a date that is the result of subtracting components of [DatePeriod] from this date. The components are
293300
* subtracted in the order from the largest units to the smallest: first years and months, then days.
294301
*
295-
* ```
296-
* LocalDate(2023, Month.JANUARY, 2) - DatePeriod(years = 1, months = 2, days = 3) == LocalDate(2021, Month.OCTOBER, 30)
297-
* // 2023-01-02 - 1 year = 2022-01-02
298-
* // 2022-01-02 - 2 months = 2021-11-02
299-
* // 2021-11-02 - 3 days = 2021-10-30
300-
* ```
301-
*
302302
* @see LocalDate.periodUntil
303303
* @throws DateTimeArithmeticException if this value or the results of intermediate computations are too large to fit in
304304
* [LocalDate].
305+
* @sample kotlinx.datetime.test.samples.LocalDateSamples.minusPeriod
305306
*/
306307
public operator fun LocalDate.minus(period: DatePeriod): LocalDate =
307308
if (period.days != Int.MIN_VALUE && period.months != Int.MIN_VALUE) {
@@ -322,13 +323,10 @@ public operator fun LocalDate.minus(period: DatePeriod): LocalDate =
322323
* - negative or zero if this date is later than the other,
323324
* - exactly zero if this date is equal to the other.
324325
*
325-
* ```
326-
* LocalDate(2023, Month.JANUARY, 2).periodUntil(LocalDate(2024, Month.APRIL, 1)) == DatePeriod(years = 1, months = 2, days = 30)
327-
* ```
328-
*
329326
* @throws DateTimeArithmeticException if the number of months between the two dates exceeds an Int (JVM only).
330327
*
331328
* @see LocalDate.minus for the same operation with the order of arguments reversed.
329+
* @sample kotlinx.datetime.test.samples.LocalDateSamples.periodUntil
332330
*/
333331
public expect fun LocalDate.periodUntil(other: LocalDate): DatePeriod
334332

@@ -342,13 +340,10 @@ public expect fun LocalDate.periodUntil(other: LocalDate): DatePeriod
342340
* - positive or zero if this date is later than the other,
343341
* - exactly zero if this date is equal to the other.
344342
*
345-
* ```
346-
* LocalDate(2024, Month.APRIL, 1) - LocalDate(2023, Month.JANUARY, 2) == DatePeriod(years = 1, months = 2, days = 30)
347-
* ```
348-
*
349343
* @throws DateTimeArithmeticException if the number of months between the two dates exceeds an Int (JVM only).
350344
*
351345
* @see LocalDate.periodUntil for the same operation with the order of arguments reversed.
346+
* @sample kotlinx.datetime.test.samples.LocalDateSamples.minusDate
352347
*/
353348
public operator fun LocalDate.minus(other: LocalDate): DatePeriod = other.periodUntil(this)
354349

@@ -361,18 +356,13 @@ public operator fun LocalDate.minus(other: LocalDate): DatePeriod = other.period
361356
* - zero if this date is equal to the other.
362357
*
363358
* The value is rounded toward zero.
364-
*
365-
* ```
366-
* LocalDate(2023, Month.JANUARY, 2).until(LocalDate(2024, Month.APRIL, 1), DateTimeUnit.MONTH) == 14
367-
* // one year, two months, and 30 days, rounded toward zero.
368-
* ```
369359
370360
* If the result does not fit in [Int], returns [Int.MAX_VALUE] for a positive result or [Int.MIN_VALUE] for a negative result.
371361
*
372362
* @see LocalDate.daysUntil
373363
* @see LocalDate.monthsUntil
374364
* @see LocalDate.yearsUntil
375-
*
365+
* @sample kotlinx.datetime.test.samples.LocalDateSamples.until
376366
*/
377367
public expect fun LocalDate.until(other: LocalDate, unit: DateTimeUnit.DateBased): Int
378368

@@ -384,6 +374,7 @@ public expect fun LocalDate.until(other: LocalDate, unit: DateTimeUnit.DateBased
384374
* If the result does not fit in [Int], returns [Int.MAX_VALUE] for a positive result or [Int.MIN_VALUE] for a negative result.
385375
*
386376
* @see LocalDate.until
377+
* @sample kotlinx.datetime.test.samples.LocalDateSamples.daysUntil
387378
*/
388379
public expect fun LocalDate.daysUntil(other: LocalDate): Int
389380

@@ -395,6 +386,7 @@ public expect fun LocalDate.daysUntil(other: LocalDate): Int
395386
* If the result does not fit in [Int], returns [Int.MAX_VALUE] for a positive result or [Int.MIN_VALUE] for a negative result.
396387
*
397388
* @see LocalDate.until
389+
* @sample kotlinx.datetime.test.samples.LocalDateSamples.monthsUntil
398390
*/
399391
public expect fun LocalDate.monthsUntil(other: LocalDate): Int
400392

@@ -404,6 +396,7 @@ public expect fun LocalDate.monthsUntil(other: LocalDate): Int
404396
* If the result does not fit in [Int], returns [Int.MAX_VALUE] for a positive result or [Int.MIN_VALUE] for a negative result.
405397
*
406398
* @see LocalDate.until
399+
* @sample kotlinx.datetime.test.samples.LocalDateSamples.yearsUntil
407400
*/
408401
public expect fun LocalDate.yearsUntil(other: LocalDate): Int
409402

@@ -440,6 +433,7 @@ public fun LocalDate.minus(unit: DateTimeUnit.DateBased): LocalDate = plus(-1, u
440433
* The value is rounded toward zero.
441434
*
442435
* @throws DateTimeArithmeticException if the result exceeds the boundaries of [LocalDate].
436+
* @sample kotlinx.datetime.test.samples.LocalDateSamples.plusInt
443437
*/
444438
public expect fun LocalDate.plus(value: Int, unit: DateTimeUnit.DateBased): LocalDate
445439

@@ -452,6 +446,7 @@ public expect fun LocalDate.plus(value: Int, unit: DateTimeUnit.DateBased): Loca
452446
* The value is rounded toward zero.
453447
*
454448
* @throws DateTimeArithmeticException if the result exceeds the boundaries of [LocalDate].
449+
* @sample kotlinx.datetime.test.samples.LocalDateSamples.minusInt
455450
*/
456451
public expect fun LocalDate.minus(value: Int, unit: DateTimeUnit.DateBased): LocalDate
457452

@@ -464,6 +459,7 @@ public expect fun LocalDate.minus(value: Int, unit: DateTimeUnit.DateBased): Loc
464459
* The value is rounded toward zero.
465460
*
466461
* @throws DateTimeArithmeticException if the result exceeds the boundaries of [LocalDate].
462+
* @sample kotlinx.datetime.test.samples.LocalDateSamples.plusLong
467463
*/
468464
public expect fun LocalDate.plus(value: Long, unit: DateTimeUnit.DateBased): LocalDate
469465

@@ -476,6 +472,7 @@ public expect fun LocalDate.plus(value: Long, unit: DateTimeUnit.DateBased): Loc
476472
* The value is rounded toward zero.
477473
*
478474
* @throws DateTimeArithmeticException if the result exceeds the boundaries of [LocalDate].
475+
* @sample kotlinx.datetime.test.samples.LocalDateSamples.minusLong
479476
*/
480477
public fun LocalDate.minus(value: Long, unit: DateTimeUnit.DateBased): LocalDate = plus(-value, unit)
481478

0 commit comments

Comments
 (0)