Skip to content

Commit 283afa3

Browse files
committed
More samples
1 parent 15e5bda commit 283afa3

File tree

5 files changed

+542
-52
lines changed

5 files changed

+542
-52
lines changed

core/common/src/format/DateTimeComponents.kt

Lines changed: 63 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,58 +17,34 @@ import kotlin.reflect.*
1717
* A collection of date-time fields, used specifically for parsing and formatting.
1818
*
1919
* Its main purpose is to provide support for complex date-time formats that don't correspond to any of the standard
20-
* entities in the library. For example, a format that includes only the month and the day of the month, but not the
21-
* year, can not be represented and parsed as a [LocalDate], but it is valid for a [DateTimeComponents].
22-
*
23-
* Example:
24-
* ```
25-
* val input = "2020-03-16T23:59:59.999999999+03:00"
26-
* val components = DateTimeComponents.Formats.ISO_DATE_TIME_OFFSET.parse(input)
27-
* val localDateTime = components.toLocalDateTime() // LocalDateTime(2020, 3, 16, 23, 59, 59, 999_999_999)
28-
* val instant = components.toInstantUsingOffset() // Instant.parse("2020-03-16T20:59:59.999999999Z")
29-
* val offset = components.toUtcOffset() // UtcOffset(hours = 3)
30-
* ```
20+
* entities in the library. For example, a format that includes only the month and the day of the month but not the
21+
* year cannot be represented and parsed as a [LocalDate], but it is valid for a [DateTimeComponents].
22+
* See sample 1.
3123
*
3224
* Another purpose is to support parsing and formatting data with out-of-bounds values. For example, parsing
3325
* `23:59:60` as a [LocalTime] is not possible, but it is possible to parse it as a [DateTimeComponents], adjust the value by
3426
* setting [second] to `59`, and then convert it to a [LocalTime] via [toLocalTime].
35-
*
36-
* Example:
37-
* ```
38-
* val input = "23:59:60"
39-
* val extraDay: Boolean
40-
* val time = DateTimeComponents.Format {
41-
* time(LocalTime.Formats.ISO)
42-
* }.parse(input).apply {
43-
* if (hour == 23 && minute == 59 && second == 60) {
44-
* hour = 0; minute = 0; second = 0; extraDay = true
45-
* } else {
46-
* extraDay = false
47-
* }
48-
* }.toLocalTime()
49-
* ```
27+
* See sample 2.
5028
*
5129
* Because this class has limited applications, constructing it directly is not possible.
5230
* For formatting, use the [format] overload that accepts a lambda with a [DateTimeComponents] receiver.
53-
*
54-
* Example:
55-
* ```
56-
* // Mon, 16 Mar 2020 23:59:59 +0300
57-
* DateTimeComponents.Formats.RFC_1123.format {
58-
* setDateTimeOffset(LocalDateTime(2020, 3, 16, 23, 59, 59, 999_999_999))
59-
* setDateTimeOffset(UtcOffset(hours = 3))
60-
* }
61-
* ```
31+
* See sample 3.
6232
*
6333
* Accessing the fields of this class is not thread-safe.
6434
* Make sure to apply proper synchronization if you are using a single instance from multiple threads.
35+
*
36+
* @sample kotlinx.datetime.test.samples.format.DateTimeComponentsSamples.parsingComplexInput
37+
* @sample kotlinx.datetime.test.samples.format.DateTimeComponentsSamples.parsingInvalidInput
38+
* @sample kotlinx.datetime.test.samples.format.DateTimeComponentsSamples.simpleFormatting
6539
*/
6640
public class DateTimeComponents internal constructor(internal val contents: DateTimeComponentsContents = DateTimeComponentsContents()) {
6741
public companion object {
6842
/**
6943
* Creates a [DateTimeFormat] for [DateTimeComponents] values using [DateTimeFormatBuilder.WithDateTimeComponents].
7044
*
7145
* There is a collection of predefined formats in [DateTimeComponents.Formats].
46+
*
47+
* @sample kotlinx.datetime.test.samples.format.DateTimeComponentsSamples.customFormat
7248
*/
7349
@Suppress("FunctionName")
7450
public fun Format(block: DateTimeFormatBuilder.WithDateTimeComponents.() -> Unit): DateTimeFormat<DateTimeComponents> {
@@ -120,6 +96,8 @@ public class DateTimeComponents internal constructor(internal val contents: Date
12096
* val localDateTime = components.toLocalDateTime() // 2020-08-30T18:43:00.123456789
12197
* val offset = components.toUtcOffset() // UtcOffset(hours = 3)
12298
* ```
99+
*
100+
* @sample kotlinx.datetime.test.samples.format.DateTimeComponentsSamples.Formats.iso
123101
*/
124102
public val ISO_DATE_TIME_OFFSET: DateTimeFormat<DateTimeComponents> = Format {
125103
date(ISO_DATE)
@@ -153,6 +131,9 @@ public class DateTimeComponents internal constructor(internal val contents: Date
153131
* * `30 Jun 2008 11:05:30 UT`
154132
*
155133
* North American and military time zone abbreviations are not supported.
134+
*
135+
* @sample kotlinx.datetime.test.samples.format.DateTimeComponentsSamples.Formats.rfc1123parsing
136+
* @sample kotlinx.datetime.test.samples.format.DateTimeComponentsSamples.Formats.rfc1123formatting
156137
*/
157138
public val RFC_1123: DateTimeFormat<DateTimeComponents> = Format {
158139
alternativeParsing({
@@ -192,6 +173,8 @@ public class DateTimeComponents internal constructor(internal val contents: Date
192173
* The [localTime] is written to the [hour], [hourOfAmPm], [amPm], [minute], [second] and [nanosecond] fields.
193174
*
194175
* If any of the fields are already set, they will be overwritten.
176+
*
177+
* @sample kotlinx.datetime.test.samples.format.DateTimeComponentsSamples.time
195178
*/
196179
public fun setTime(localTime: LocalTime) {
197180
contents.time.populateFrom(localTime)
@@ -202,6 +185,8 @@ public class DateTimeComponents internal constructor(internal val contents: Date
202185
* The [localDate] is written to the [year], [monthNumber], [dayOfMonth], and [dayOfWeek] fields.
203186
*
204187
* If any of the fields are already set, they will be overwritten.
188+
*
189+
* @sample kotlinx.datetime.test.samples.format.DateTimeComponentsSamples.date
205190
*/
206191
public fun setDate(localDate: LocalDate) {
207192
contents.date.populateFrom(localDate)
@@ -214,6 +199,8 @@ public class DateTimeComponents internal constructor(internal val contents: Date
214199
* [hour], [hourOfAmPm], [amPm], [minute], [second] and [nanosecond] fields.
215200
*
216201
* If any of the fields are already set, they will be overwritten.
202+
*
203+
* @sample kotlinx.datetime.test.samples.format.DateTimeComponentsSamples.setDateTime
217204
*/
218205
public fun setDateTime(localDateTime: LocalDateTime) {
219206
contents.date.populateFrom(localDateTime.date)
@@ -226,6 +213,8 @@ public class DateTimeComponents internal constructor(internal val contents: Date
226213
* [offsetIsNegative] fields.
227214
*
228215
* If any of the fields are already set, they will be overwritten.
216+
*
217+
* @sample kotlinx.datetime.test.samples.format.DateTimeComponentsSamples.offset
229218
*/
230219
public fun setOffset(utcOffset: UtcOffset) {
231220
contents.offset.populateFrom(utcOffset)
@@ -242,6 +231,8 @@ public class DateTimeComponents internal constructor(internal val contents: Date
242231
* However, this also works for instants that are too large to be represented as a [LocalDateTime].
243232
*
244233
* If any of the fields are already set, they will be overwritten.
234+
*
235+
* @sample kotlinx.datetime.test.samples.format.DateTimeComponentsSamples.setDateTimeOffsetInstant
245236
*/
246237
public fun setDateTimeOffset(instant: Instant, utcOffset: UtcOffset) {
247238
val smallerInstant = Instant.fromEpochSeconds(
@@ -259,24 +250,31 @@ public class DateTimeComponents internal constructor(internal val contents: Date
259250
*
260251
* If [localDateTime] is obtained from an [Instant] using [LocalDateTime.toInstant], it is recommended to use
261252
* [setDateTimeOffset] that accepts an [Instant] directly.
253+
*
254+
* @sample kotlinx.datetime.test.samples.format.DateTimeComponentsSamples.setDateTimeOffset
262255
*/
263256
public fun setDateTimeOffset(localDateTime: LocalDateTime, utcOffset: UtcOffset) {
264257
setDateTime(localDateTime)
265258
setOffset(utcOffset)
266259
}
267260

268-
/** The year component of the date. */
261+
/**
262+
* The year component of the date.
263+
* @sample kotlinx.datetime.test.samples.format.DateTimeComponentsSamples.date
264+
*/
269265
public var year: Int? by contents.date::year
270266

271267
/**
272268
* The number-of-month (1..12) component of the date.
273269
* @throws IllegalArgumentException during assignment if the value is outside the `0..99` range.
270+
* @sample kotlinx.datetime.test.samples.format.DateTimeComponentsSamples.date
274271
*/
275272
public var monthNumber: Int? by TwoDigitNumber(contents.date::monthNumber)
276273

277274
/**
278275
* The month ([Month]) component of the date.
279276
* @throws IllegalArgumentException during getting if [monthNumber] is outside the `1..12` range.
277+
* @sample kotlinx.datetime.test.samples.format.DateTimeComponentsSamples.date
280278
*/
281279
public var month: Month?
282280
get() = monthNumber?.let { Month(it) }
@@ -287,10 +285,14 @@ public class DateTimeComponents internal constructor(internal val contents: Date
287285
/**
288286
* The day-of-month component of the date.
289287
* @throws IllegalArgumentException during assignment if the value is outside the `0..99` range.
288+
* @sample kotlinx.datetime.test.samples.format.DateTimeComponentsSamples.date
290289
*/
291290
public var dayOfMonth: Int? by TwoDigitNumber(contents.date::dayOfMonth)
292291

293-
/** The day-of-week component of the date. */
292+
/**
293+
* The day-of-week component of the date.
294+
* @sample kotlinx.datetime.test.samples.format.DateTimeComponentsSamples.dayOfWeek
295+
*/
294296
public var dayOfWeek: DayOfWeek?
295297
get() = contents.date.isoDayOfWeek?.let { DayOfWeek(it) }
296298
set(value) {
@@ -302,37 +304,43 @@ public class DateTimeComponents internal constructor(internal val contents: Date
302304
/**
303305
* The hour-of-day (0..23) time component.
304306
* @throws IllegalArgumentException during assignment if the value is outside the `0..99` range.
307+
* @sample kotlinx.datetime.test.samples.format.DateTimeComponentsSamples.time
305308
*/
306309
public var hour: Int? by TwoDigitNumber(contents.time::hour)
307310

308311
/**
309312
* The 12-hour (1..12) time component.
310313
* @throws IllegalArgumentException during assignment if the value is outside the `0..99` range.
311314
* @see amPm
315+
* @sample kotlinx.datetime.test.samples.format.DateTimeComponentsSamples.timeAmPm
312316
*/
313317
public var hourOfAmPm: Int? by TwoDigitNumber(contents.time::hourOfAmPm)
314318

315319
/**
316320
* The AM/PM state of the time component.
317321
* @see hourOfAmPm
322+
* @sample kotlinx.datetime.test.samples.format.DateTimeComponentsSamples.timeAmPm
318323
*/
319324
public var amPm: AmPmMarker? by contents.time::amPm
320325

321326
/**
322327
* The minute-of-hour component.
323328
* @throws IllegalArgumentException during assignment if the value is outside the `0..99` range.
329+
* @sample kotlinx.datetime.test.samples.format.DateTimeComponentsSamples.time
324330
*/
325331
public var minute: Int? by TwoDigitNumber(contents.time::minute)
326332

327333
/**
328334
* The second-of-minute component.
329335
* @throws IllegalArgumentException during assignment if the value is outside the `0..99` range.
336+
* @sample kotlinx.datetime.test.samples.format.DateTimeComponentsSamples.time
330337
*/
331338
public var second: Int? by TwoDigitNumber(contents.time::second)
332339

333340
/**
334341
* The nanosecond-of-second component.
335342
* @throws IllegalArgumentException during assignment if the value is outside the `0..999_999_999` range.
343+
* @sample kotlinx.datetime.test.samples.format.DateTimeComponentsSamples.time
336344
*/
337345
public var nanosecond: Int?
338346
get() = contents.time.nanosecond
@@ -343,28 +351,37 @@ public class DateTimeComponents internal constructor(internal val contents: Date
343351
contents.time.nanosecond = value
344352
}
345353

346-
/** True if the offset is negative. */
354+
/**
355+
* True if the offset is negative.
356+
* @sample kotlinx.datetime.test.samples.format.DateTimeComponentsSamples.offset
357+
*/
347358
public var offsetIsNegative: Boolean? by contents.offset::isNegative
348359

349360
/**
350361
* The total amount of full hours in the UTC offset, in the range [0; 18].
351362
* @throws IllegalArgumentException during assignment if the value is outside the `0..99` range.
363+
* @sample kotlinx.datetime.test.samples.format.DateTimeComponentsSamples.offset
352364
*/
353365
public var offsetHours: Int? by TwoDigitNumber(contents.offset::totalHoursAbs)
354366

355367
/**
356368
* The amount of minutes that don't add to a whole hour in the UTC offset, in the range [0; 59].
357369
* @throws IllegalArgumentException during assignment if the value is outside the `0..99` range.
370+
* @sample kotlinx.datetime.test.samples.format.DateTimeComponentsSamples.offset
358371
*/
359372
public var offsetMinutesOfHour: Int? by TwoDigitNumber(contents.offset::minutesOfHour)
360373

361374
/**
362375
* The amount of seconds that don't add to a whole minute in the UTC offset, in the range [0; 59].
363376
* @throws IllegalArgumentException during assignment if the value is outside the `0..99` range.
377+
* @sample kotlinx.datetime.test.samples.format.DateTimeComponentsSamples.offset
364378
*/
365379
public var offsetSecondsOfMinute: Int? by TwoDigitNumber(contents.offset::secondsOfMinute)
366380

367-
/** The timezone identifier, for example, "Europe/Berlin". */
381+
/**
382+
* The timezone identifier, for example, "Europe/Berlin".
383+
* @sample kotlinx.datetime.test.samples.format.DateTimeComponentsSamples.timeZoneId
384+
*/
368385
public var timeZoneId: String? by contents::timeZoneId
369386

370387
/**
@@ -377,6 +394,7 @@ public class DateTimeComponents internal constructor(internal val contents: Date
377394
* * [offsetSecondsOfMinute] (default value is 0)
378395
*
379396
* @throws IllegalArgumentException if any of the fields has an out-of-range value.
397+
* @sample kotlinx.datetime.test.samples.format.DateTimeComponentsSamples.toUtcOffset
380398
*/
381399
public fun toUtcOffset(): UtcOffset = contents.offset.toUtcOffset()
382400

@@ -391,6 +409,7 @@ public class DateTimeComponents internal constructor(internal val contents: Date
391409
* Also, [dayOfWeek] is checked for consistency with the other fields.
392410
*
393411
* @throws IllegalArgumentException if any of the fields is missing or invalid.
412+
* @sample kotlinx.datetime.test.samples.format.DateTimeComponentsSamples.toLocalDate
394413
*/
395414
public fun toLocalDate(): LocalDate = contents.date.toLocalDate()
396415

@@ -405,6 +424,7 @@ public class DateTimeComponents internal constructor(internal val contents: Date
405424
*
406425
* @throws IllegalArgumentException if hours or minutes are not present, if any of the fields are invalid, or
407426
* [hourOfAmPm] and [amPm] are inconsistent with [hour].
427+
* @sample kotlinx.datetime.test.samples.format.DateTimeComponentsSamples.toLocalTime
408428
*/
409429
public fun toLocalTime(): LocalTime = contents.time.toLocalTime()
410430

@@ -427,6 +447,7 @@ public class DateTimeComponents internal constructor(internal val contents: Date
427447
*
428448
* @see toLocalDate
429449
* @see toLocalTime
450+
* @sample kotlinx.datetime.test.samples.format.DateTimeComponentsSamples.toLocalDateTime
430451
*/
431452
public fun toLocalDateTime(): LocalDateTime = toLocalDate().atTime(toLocalTime())
432453

@@ -440,6 +461,7 @@ public class DateTimeComponents internal constructor(internal val contents: Date
440461
*
441462
* @throws IllegalArgumentException if any of the required fields are not present, out-of-range, or inconsistent
442463
* with one another.
464+
* @sample kotlinx.datetime.test.samples.format.DateTimeComponentsSamples.toInstantUsingOffset
443465
*/
444466
public fun toInstantUsingOffset(): Instant {
445467
val offset = toUtcOffset()
@@ -482,6 +504,7 @@ public class DateTimeComponents internal constructor(internal val contents: Date
482504
* @throws IllegalStateException if some values needed for the format are not present or can not be formatted:
483505
* for example, trying to format [DateTimeFormatBuilder.WithDate.monthName] using a [DateTimeComponents.monthNumber]
484506
* value of 20.
507+
* @sample kotlinx.datetime.test.samples.format.DateTimeComponentsSamples.formatting
485508
*/
486509
public fun DateTimeFormat<DateTimeComponents>.format(block: DateTimeComponents.() -> Unit): String =
487510
format(DateTimeComponents().apply { block() })
@@ -494,6 +517,7 @@ public fun DateTimeFormat<DateTimeComponents>.format(block: DateTimeComponents.(
494517
* matches.
495518
*
496519
* @throws IllegalArgumentException if the text does not match the format.
520+
* @sample kotlinx.datetime.test.samples.format.DateTimeComponentsSamples.parsing
497521
*/
498522
public fun DateTimeComponents.Companion.parse(
499523
input: CharSequence,

core/common/src/format/DateTimeFormatBuilder.kt

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public sealed interface DateTimeFormatBuilder {
3434
* this padding can be disabled or changed to space padding by passing [padding].
3535
* For years outside this range, it's formatted as a decimal number with a leading sign, so the year 12345
3636
* is formatted as "+12345".
37+
*
38+
* @sample kotlinx.datetime.test.samples.format.LocalDateFormatSamples.year
3739
*/
3840
public fun year(padding: Padding = Padding.ZERO)
3941

@@ -51,50 +53,47 @@ public sealed interface DateTimeFormatBuilder {
5153
* When given a two-digit year, it returns a year in the valid range, so "93" becomes 1993,
5254
* and when given a full year number with a leading sign, it parses the full year number,
5355
* so "+1850" becomes 1850.
56+
*
57+
* @sample kotlinx.datetime.test.samples.format.LocalDateFormatSamples.yearTwoDigits
5458
*/
5559
public fun yearTwoDigits(baseYear: Int)
5660

5761
/**
5862
* A month-of-year number, from 1 to 12.
5963
*
6064
* By default, it's padded with zeros to two digits. This can be changed by passing [padding].
65+
*
66+
* @sample kotlinx.datetime.test.samples.format.LocalDateFormatSamples.monthNumber
6167
*/
6268
public fun monthNumber(padding: Padding = Padding.ZERO)
6369

6470
/**
6571
* A month name (for example, "January").
6672
*
67-
* Example:
68-
* ```
69-
* monthName(MonthNames.ENGLISH_FULL)
70-
* ```
73+
* @sample kotlinx.datetime.test.samples.format.LocalDateFormatSamples.monthName
7174
*/
7275
public fun monthName(names: MonthNames)
7376

7477
/**
7578
* A day-of-month number, from 1 to 31.
7679
*
7780
* By default, it's padded with zeros to two digits. This can be changed by passing [padding].
81+
*
82+
* @sample kotlinx.datetime.test.samples.format.LocalDateFormatSamples.dayOfMonth
7883
*/
7984
public fun dayOfMonth(padding: Padding = Padding.ZERO)
8085

8186
/**
8287
* A day-of-week name (for example, "Thursday").
8388
*
84-
* Example:
85-
* ```
86-
* dayOfWeek(DayOfWeekNames.ENGLISH_FULL)
87-
* ```
89+
* @sample kotlinx.datetime.test.samples.format.LocalDateFormatSamples.dayOfWeek
8890
*/
8991
public fun dayOfWeek(names: DayOfWeekNames)
9092

9193
/**
9294
* An existing [DateTimeFormat] for the date part.
9395
*
94-
* Example:
95-
* ```
96-
* date(LocalDate.Formats.ISO)
97-
* ```
96+
* @sample kotlinx.datetime.test.samples.format.LocalDateFormatSamples.date
9897
*/
9998
public fun date(format: DateTimeFormat<LocalDate>)
10099
}

0 commit comments

Comments
 (0)