Skip to content

Commit d3aa236

Browse files
committed
More samples; only LocalDate and Instant left
1 parent 04d4e07 commit d3aa236

File tree

7 files changed

+418
-45
lines changed

7 files changed

+418
-45
lines changed

core/common/src/DateTimeUnit.kt

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,17 @@ import kotlin.time.Duration.Companion.nanoseconds
5656
* [DateTimeUnitSerializer], [DateBasedDateTimeUnitSerializer], [DayBasedDateTimeUnitSerializer],
5757
* [MonthBasedDateTimeUnitSerializer], and [TimeBasedDateTimeUnitSerializer] are provided, with varying levels of
5858
* specificity of the type they handle.
59+
*
60+
* @sample kotlinx.datetime.test.samples.DateTimeUnitSamples.construction
5961
*/
6062
@Serializable(with = DateTimeUnitSerializer::class)
6163
public sealed class DateTimeUnit {
6264

6365
/**
6466
* Produces a date-time unit that is a multiple of this unit times the specified integer [scalar] value.
6567
*
66-
* ```
67-
* val quarter = DateTimeUnit.MONTH * 3
68-
* ```
69-
*
7068
* @throws ArithmeticException if the result overflows.
69+
* @sample kotlinx.datetime.test.samples.DateTimeUnitSamples.multiplication
7170
*/
7271
public abstract operator fun times(scalar: Int): DateTimeUnit
7372

@@ -78,11 +77,14 @@ public sealed class DateTimeUnit {
7877
* Any such unit can be represented as some fixed number of nanoseconds.
7978
*
8079
* @see DateTimeUnit for a description of date-time units in general.
80+
* @sample kotlinx.datetime.test.samples.DateTimeUnitSamples.timeBasedUnit
8181
*/
8282
@Serializable(with = TimeBasedDateTimeUnitSerializer::class)
8383
public class TimeBased(
8484
/**
8585
* The length of this unit in nanoseconds.
86+
*
87+
* @sample kotlinx.datetime.test.samples.DateTimeUnitSamples.timeBasedUnit
8688
*/
8789
public val nanoseconds: Long
8890
) : DateTimeUnit() {
@@ -124,6 +126,8 @@ public sealed class DateTimeUnit {
124126

125127
/**
126128
* The length of this unit as a [Duration].
129+
*
130+
* @sample kotlinx.datetime.test.samples.DateTimeUnitSamples.timeBasedUnit
127131
*/
128132
public val duration: Duration
129133
get() = nanoseconds.nanoseconds
@@ -144,6 +148,8 @@ public sealed class DateTimeUnit {
144148
* the operation with the date component of these `LocalDateTime` values.
145149
*
146150
* @see DateTimeUnit for a description of date-time units in general.
151+
* @see DateTimeUnit.DayBased for specifically day-based units.
152+
* @see DateTimeUnit.MonthBased for specifically month-based units.
147153
*/
148154
@Serializable(with = DateBasedDateTimeUnitSerializer::class)
149155
public sealed class DateBased : DateTimeUnit() {
@@ -167,11 +173,14 @@ public sealed class DateTimeUnit {
167173
* between the two date-times.
168174
*
169175
* @see DateTimeUnit for a description of date-time units in general.
176+
* @sample kotlinx.datetime.test.samples.DateTimeUnitSamples.dayBasedUnit
170177
*/
171178
@Serializable(with = DayBasedDateTimeUnitSerializer::class)
172179
public class DayBased(
173180
/**
174181
* The length of this unit in days.
182+
*
183+
* @sample kotlinx.datetime.test.samples.DateTimeUnitSamples.dayBasedUnit
175184
*/
176185
public val days: Int
177186
) : DateBased() {
@@ -198,11 +207,14 @@ public sealed class DateTimeUnit {
198207
* Since different months have different number of days, a `MonthBased`-unit cannot be expressed a multiple of some [DayBased]-unit.
199208
*
200209
* @see DateTimeUnit for a description of date-time units in general.
210+
* @sample kotlinx.datetime.test.samples.DateTimeUnitSamples.monthBasedUnit
201211
*/
202212
@Serializable(with = MonthBasedDateTimeUnitSerializer::class)
203213
public class MonthBased(
204214
/**
205215
* The length of this unit in months.
216+
*
217+
* @sample kotlinx.datetime.test.samples.DateTimeUnitSamples.monthBasedUnit
206218
*/
207219
public val months: Int
208220
) : DateBased() {

core/common/src/DayOfWeek.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ package kotlinx.datetime
1010
*
1111
* Usually acquired from [LocalDate.dayOfWeek], but can be constructed using the `DayOfWeek` factory function that
1212
* accepts the ISO 8601 day number. This number can be obtained from the [isoDayNumber] property.
13+
*
14+
* @sample kotlinx.datetime.test.samples.DayOfWeekSamples.usage
1315
*/
1416
public expect enum class DayOfWeek {
1517
MONDAY,
@@ -23,13 +25,16 @@ public expect enum class DayOfWeek {
2325

2426
/**
2527
* The ISO 8601 number of the given day of the week. Monday is 1, Sunday is 7.
28+
*
29+
* @sample kotlinx.datetime.test.samples.DayOfWeekSamples.isoDayNumber
2630
*/
2731
public val DayOfWeek.isoDayNumber: Int get() = ordinal + 1
2832

2933
/**
3034
* Returns the [DayOfWeek] instance for the given ISO 8601 week day number. Monday is 1, Sunday is 7.
3135
*
3236
* @throws IllegalArgumentException if the day number is not in the range 1..7
37+
* @sample kotlinx.datetime.test.samples.DayOfWeekSamples.constructorFunction
3338
*/
3439
public fun DayOfWeek(isoDayNumber: Int): DayOfWeek {
3540
require(isoDayNumber in 1..7) { "Expected ISO day-of-week number in 1..7, got $isoDayNumber" }

core/common/src/LocalDateTime.kt

Lines changed: 86 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -69,41 +69,18 @@ import kotlinx.serialization.Serializable
6969
* The recommended pattern is to convert a [LocalDateTime] to [Instant] as soon as possible (see
7070
* [LocalDateTime.toInstant]) and work with [Instant] values instead.
7171
*
72-
* [LocalDateTime] can be constructed directly from its components, [LocalDate] and [LocalTime], using the constructor:
73-
*
74-
* ```
75-
* val date = LocalDate(2021, 3, 27)
76-
* val time = LocalTime(hour = 2, minute = 16, second = 20)
77-
* LocalDateTime(date, time)
78-
* ```
72+
* [LocalDateTime] can be constructed directly from its components, [LocalDate] and [LocalTime], using the constructor.
73+
* See sample 1.
7974
*
8075
* Some additional constructors that accept the date's and time's fields directly are provided for convenience.
81-
*
82-
* ```
83-
* LocalDateTime(year = 2021, monthNumber = 3, dayOfMonth = 27, hour = 2, minute = 16, second = 20)
84-
* LocalDateTime(
85-
* year = 2021, month = Month.MARCH, dayOfMonth = 27,
86-
* hour = 2, minute = 16, second = 20, nanosecond = 999_999_999
87-
* )
88-
* ```
76+
* See sample 2.
8977
*
9078
* [parse] and [toString] methods can be used to obtain a [LocalDateTime] from and convert it to a string in the
9179
* ISO 8601 extended format (for example, `2023-01-02T22:35:01`).
92-
*
93-
* ```
94-
* LocalDateTime.parse("2023-01-02T22:35:01").toString() // 2023-01-02T22:35:01
95-
* ```
80+
* See sample 3.
9681
*
9782
* [parse] and [LocalDateTime.format] both support custom formats created with [Format] or defined in [Formats].
98-
*
99-
* ```
100-
* val customFormat = LocalDateTime.Format {
101-
* date(LocalDate.Formats.ISO)
102-
* char(' ')
103-
* time(LocalTime.Formats.ISO)
104-
* }
105-
* LocalDateTime.parse("2023-01-02 22:35:01", customFormat).format(customFormat) // 2023-01-02 22:35:01
106-
* ```
83+
* See sample 4.
10784
*
10885
* Additionally, there are several `kotlinx-serialization` serializers for [LocalDateTime]:
10986
* - [LocalDateTimeIso8601Serializer] for the ISO 8601 extended format,
@@ -112,6 +89,10 @@ import kotlinx.serialization.Serializable
11289
* @see LocalDate for only the date part of the date/time value.
11390
* @see LocalTime for only the time part of the date/time value.
11491
* @see Instant for the representation of a specific moment in time independent of a time zone.
92+
* @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.fromDateAndTime
93+
* @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.alternativeConstruction
94+
* @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.simpleParsingAndFormatting
95+
* @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.customFormat
11596
*/
11697
@Serializable(with = LocalDateTimeIso8601Serializer::class)
11798
public expect class LocalDateTime : Comparable<LocalDateTime> {
@@ -130,6 +111,8 @@ public expect class LocalDateTime : Comparable<LocalDateTime> {
130111
*
131112
* @throws IllegalArgumentException if the text cannot be parsed or the boundaries of [LocalDateTime] are
132113
* exceeded.
114+
*
115+
* @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.parsing
133116
*/
134117
public fun parse(input: CharSequence, format: DateTimeFormat<LocalDateTime> = getIsoDateTimeFormat()): LocalDateTime
135118

@@ -156,6 +139,8 @@ public expect class LocalDateTime : Comparable<LocalDateTime> {
156139
* There is a collection of predefined formats in [LocalDateTime.Formats].
157140
*
158141
* @throws IllegalArgumentException if parsing using this format is ambiguous.
142+
*
143+
* @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.customFormat
159144
*/
160145
@Suppress("FunctionName")
161146
public fun Format(builder: DateTimeFormatBuilder.WithDateTime.() -> Unit): DateTimeFormat<LocalDateTime>
@@ -186,6 +171,8 @@ public expect class LocalDateTime : Comparable<LocalDateTime> {
186171
* Fractional parts of the second are included if non-zero.
187172
*
188173
* Guaranteed to parse all strings that [LocalDateTime.toString] produces.
174+
*
175+
* @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.Formats.iso
189176
*/
190177
public val ISO: DateTimeFormat<LocalDateTime>
191178
}
@@ -207,6 +194,8 @@ public expect class LocalDateTime : Comparable<LocalDateTime> {
207194
*
208195
* @throws IllegalArgumentException if any parameter is out of range,
209196
* or if [dayOfMonth] is invalid for the given [monthNumber] and [year].
197+
*
198+
* @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.constructorFunctionWithMonthNumber
210199
*/
211200
public constructor(
212201
year: Int,
@@ -233,6 +222,8 @@ public expect class LocalDateTime : Comparable<LocalDateTime> {
233222
*
234223
* @throws IllegalArgumentException if any parameter is out of range,
235224
* or if [dayOfMonth] is invalid for the given [month] and [year].
225+
*
226+
* @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.constructorFunction
236227
*/
237228
public constructor(
238229
year: Int,
@@ -246,43 +237,93 @@ public expect class LocalDateTime : Comparable<LocalDateTime> {
246237

247238
/**
248239
* Constructs a [LocalDateTime] instance by combining the given [date] and [time] parts.
240+
*
241+
* @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.fromDateAndTime
249242
*/
250243
public constructor(date: LocalDate, time: LocalTime)
251244

252-
/** Returns the year component of the date. */
245+
/**
246+
* Returns the year component of the [date]. Can be negative.
247+
*
248+
* @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.dateComponents
249+
*/
253250
public val year: Int
254251

255-
/** Returns the number-of-the-month (1..12) component of the date. */
252+
/**
253+
* Returns the number-of-the-month (1..12) component of the [date].
254+
*
255+
* @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.dateComponents
256+
*/
256257
public val monthNumber: Int
257258

258-
/** Returns the month ([Month]) component of the date. */
259+
/**
260+
* Returns the month ([Month]) component of the [date].
261+
*
262+
* @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.dateComponents
263+
*/
259264
public val month: Month
260265

261-
/** Returns the day-of-month component of the date. */
266+
/**
267+
* Returns the day-of-month (`1..31`) component of the [date].
268+
*
269+
* @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.dateComponents
270+
*/
262271
public val dayOfMonth: Int
263272

264-
/** Returns the day-of-week component of the date. */
273+
/**
274+
* Returns the day-of-week component of the [date].
275+
*
276+
* @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.dateComponents
277+
*/
265278
public val dayOfWeek: DayOfWeek
266279

267-
/** Returns the 1-based day-of-year component of the date. */
280+
/**
281+
* Returns the 1-based day-of-year component of the [date].
282+
*
283+
* @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.dateComponents
284+
*/
268285
public val dayOfYear: Int
269286

270-
/** Returns the hour-of-day time component of this date/time value. */
287+
/**
288+
* Returns the hour-of-day (`0..59`) [time] component of this date/time value.
289+
*
290+
* @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.timeComponents
291+
*/
271292
public val hour: Int
272293

273-
/** Returns the minute-of-hour time component of this date/time value. */
294+
/**
295+
* Returns the minute-of-hour (`0..59`) [time] component of this date/time value.
296+
*
297+
* @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.timeComponents
298+
*/
274299
public val minute: Int
275300

276-
/** Returns the second-of-minute time component of this date/time value. */
301+
/**
302+
* Returns the second-of-minute (`0..59`) [time] component of this date/time value.
303+
*
304+
* @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.timeComponents
305+
*/
277306
public val second: Int
278307

279-
/** Returns the nanosecond-of-second time component of this date/time value. */
308+
/**
309+
* Returns the nanosecond-of-second (`0..999_999_999`) [time] component of this date/time value.
310+
*
311+
* @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.timeComponents
312+
*/
280313
public val nanosecond: Int
281314

282-
/** Returns the date part of this date/time value. */
315+
/**
316+
* Returns the date part of this date/time value.
317+
*
318+
* @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.dateAndTime
319+
*/
283320
public val date: LocalDate
284321

285-
/** Returns the time part of this date/time value. */
322+
/**
323+
* Returns the time part of this date/time value.
324+
*
325+
* @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.dateAndTime
326+
*/
286327
public val time: LocalTime
287328

288329
/**
@@ -300,6 +341,8 @@ public expect class LocalDateTime : Comparable<LocalDateTime> {
300341
* val ldt2 = Clock.System.now().toLocalDateTime(zone) // 2021-10-31T02:01:20
301342
* ldt2 > ldt1 // returns `false`
302343
* ```
344+
*
345+
* @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.compareToSample
303346
*/
304347
public override operator fun compareTo(other: LocalDateTime): Int
305348

@@ -321,6 +364,7 @@ public expect class LocalDateTime : Comparable<LocalDateTime> {
321364
* even if they are zero, and will not add trailing zeros to the fractional part of the second for readability.
322365
* @see parse for the dual operation: obtaining [LocalDateTime] from a string.
323366
* @see LocalDateTime.format for formatting using a custom format.
367+
* @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.toStringSample
324368
*/
325369
public override fun toString(): String
326370
}
@@ -330,6 +374,8 @@ public expect class LocalDateTime : Comparable<LocalDateTime> {
330374
* Equivalent to calling [DateTimeFormat.format] on [format] with `this`.
331375
*
332376
* See [LocalDateTime.Formats] and [LocalDateTime.Format] for predefined and custom formats.
377+
*
378+
* @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.formatting
333379
*/
334380
public fun LocalDateTime.format(format: DateTimeFormat<LocalDateTime>): String = format.format(this)
335381

core/common/test/samples/ClockSamples.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class ClockSamples {
2020
@Test
2121
fun todayIn() {
2222
val clock = object : Clock {
23-
override fun now(): Instant = Instant.parse("2020-01-01T12:00:00Z")
23+
override fun now(): Instant = Instant.parse("2020-01-01T02:00:00Z")
2424
}
2525
check(clock.todayIn(TimeZone.UTC) == LocalDate(2020, 1, 1))
2626
check(clock.todayIn(TimeZone.of("America/New_York")) == LocalDate(2019, 12, 31))

0 commit comments

Comments
 (0)