Skip to content

Commit 7e8f55e

Browse files
committed
Add samples for format builders
1 parent 283afa3 commit 7e8f55e

File tree

5 files changed

+193
-16
lines changed

5 files changed

+193
-16
lines changed

core/common/src/format/DateTimeFormatBuilder.kt

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ public sealed interface DateTimeFormatBuilder {
106106
* The hour of the day, from 0 to 23.
107107
*
108108
* By default, it's zero-padded to two digits, but this can be changed with [padding].
109+
*
110+
* @sample kotlinx.datetime.test.samples.format.LocalTimeFormatSamples.hhmmss
109111
*/
110112
public fun hour(padding: Padding = Padding.ZERO)
111113

@@ -122,6 +124,7 @@ public sealed interface DateTimeFormatBuilder {
122124
* By default, it's zero-padded to two digits, but this can be changed with [padding].
123125
*
124126
* @see [amPmMarker]
127+
* @sample kotlinx.datetime.test.samples.format.LocalTimeFormatSamples.amPm
125128
*/
126129
public fun amPmHour(padding: Padding = Padding.ZERO)
127130

@@ -131,13 +134,16 @@ public sealed interface DateTimeFormatBuilder {
131134
* [am] is used for the AM marker (0-11 hours), [pm] is used for the PM marker (12-23 hours).
132135
*
133136
* @see [amPmHour]
137+
* @sample kotlinx.datetime.test.samples.format.LocalTimeFormatSamples.amPm
134138
*/
135139
public fun amPmMarker(am: String, pm: String)
136140

137141
/**
138142
* The minute of hour.
139143
*
140144
* By default, it's zero-padded to two digits, but this can be changed with [padding].
145+
*
146+
* @sample kotlinx.datetime.test.samples.format.LocalTimeFormatSamples.hhmmss
141147
*/
142148
public fun minute(padding: Padding = Padding.ZERO)
143149

@@ -147,6 +153,8 @@ public sealed interface DateTimeFormatBuilder {
147153
* By default, it's zero-padded to two digits, but this can be changed with [padding].
148154
*
149155
* This field has the default value of 0. If you want to omit it, use [optional].
156+
*
157+
* @sample kotlinx.datetime.test.samples.format.LocalTimeFormatSamples.hhmmss
150158
*/
151159
public fun second(padding: Padding = Padding.ZERO)
152160

@@ -166,6 +174,8 @@ public sealed interface DateTimeFormatBuilder {
166174
* part.
167175
*
168176
* @throws IllegalArgumentException if [minLength] is greater than [maxLength] or if either is not in the range 1..9.
177+
*
178+
* @sample kotlinx.datetime.test.samples.format.LocalTimeFormatSamples.hhmmss
169179
*/
170180
public fun secondFraction(minLength: Int = 1, maxLength: Int = 9)
171181

@@ -186,6 +196,7 @@ public sealed interface DateTimeFormatBuilder {
186196
* @throws IllegalArgumentException if [fixedLength] is not in the range 1..9.
187197
*
188198
* @see secondFraction that accepts two parameters.
199+
* @sample kotlinx.datetime.test.samples.format.LocalTimeFormatSamples.fixedLengthSecondFraction
189200
*/
190201
public fun secondFraction(fixedLength: Int) {
191202
secondFraction(fixedLength, fixedLength)
@@ -194,10 +205,7 @@ public sealed interface DateTimeFormatBuilder {
194205
/**
195206
* An existing [DateTimeFormat] for the time part.
196207
*
197-
* Example:
198-
* ```
199-
* time(LocalTime.Formats.ISO)
200-
* ```
208+
* @sample kotlinx.datetime.test.samples.format.LocalTimeFormatSamples.time
201209
*/
202210
public fun time(format: DateTimeFormat<LocalTime>)
203211
}
@@ -209,10 +217,7 @@ public sealed interface DateTimeFormatBuilder {
209217
/**
210218
* An existing [DateTimeFormat] for the date-time part.
211219
*
212-
* Example:
213-
* ```
214-
* dateTime(LocalDateTime.Formats.ISO)
215-
* ```
220+
* @sample kotlinx.datetime.test.samples.format.LocalDateTimeFormatSamples.dateTime
216221
*/
217222
public fun dateTime(format: DateTimeFormat<LocalDateTime>)
218223
}
@@ -227,6 +232,8 @@ public sealed interface DateTimeFormatBuilder {
227232
* By default, it's zero-padded to two digits, but this can be changed with [padding].
228233
*
229234
* This field has the default value of 0. If you want to omit it, use [optional].
235+
*
236+
* @sample kotlinx.datetime.test.samples.format.UtcOffsetFormatSamples.isoOrGmt
230237
*/
231238
public fun offsetHours(padding: Padding = Padding.ZERO)
232239

@@ -236,6 +243,8 @@ public sealed interface DateTimeFormatBuilder {
236243
* By default, it's zero-padded to two digits, but this can be changed with [padding].
237244
*
238245
* This field has the default value of 0. If you want to omit it, use [optional].
246+
*
247+
* @sample kotlinx.datetime.test.samples.format.UtcOffsetFormatSamples.isoOrGmt
239248
*/
240249
public fun offsetMinutesOfHour(padding: Padding = Padding.ZERO)
241250

@@ -245,16 +254,15 @@ public sealed interface DateTimeFormatBuilder {
245254
* By default, it's zero-padded to two digits, but this can be changed with [padding].
246255
*
247256
* This field has the default value of 0. If you want to omit it, use [optional].
257+
*
258+
* @sample kotlinx.datetime.test.samples.format.UtcOffsetFormatSamples.isoOrGmt
248259
*/
249260
public fun offsetSecondsOfMinute(padding: Padding = Padding.ZERO)
250261

251262
/**
252263
* An existing [DateTimeFormat] for the UTC offset part.
253264
*
254-
* Example:
255-
* ```
256-
* offset(UtcOffset.Formats.FOUR_DIGITS)
257-
* ```
265+
* @sample kotlinx.datetime.test.samples.format.UtcOffsetFormatSamples.offset
258266
*/
259267
public fun offset(format: DateTimeFormat<UtcOffset>)
260268
}
@@ -269,16 +277,15 @@ public sealed interface DateTimeFormatBuilder {
269277
*
270278
* When formatting, the timezone identifier is supplied as is, without any validation.
271279
* On parsing, [TimeZone.availableZoneIds] is used to validate the identifier.
280+
*
281+
* @sample kotlinx.datetime.test.samples.format.DateTimeComponentsFormatSamples.timeZoneId
272282
*/
273283
public fun timeZoneId()
274284

275285
/**
276286
* An existing [DateTimeFormat].
277287
*
278-
* Example:
279-
* ```
280-
* dateTimeComponents(DateTimeComponents.Formats.RFC_1123)
281-
* ```
288+
* @sample kotlinx.datetime.test.samples.format.DateTimeComponentsFormatSamples.dateTimeComponents
282289
*/
283290
public fun dateTimeComponents(format: DateTimeFormat<DateTimeComponents>)
284291
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2019-2024 JetBrains s.r.o. and contributors.
3+
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
4+
*/
5+
6+
package kotlinx.datetime.test.samples.format
7+
8+
import kotlinx.datetime.*
9+
import kotlinx.datetime.format.*
10+
import kotlin.test.*
11+
12+
class DateTimeComponentsFormatSamples {
13+
@Test
14+
fun timeZoneId() {
15+
val format = DateTimeComponents.Format {
16+
dateTime(LocalDateTime.Formats.ISO)
17+
char('[')
18+
timeZoneId()
19+
char(']')
20+
}
21+
val formatted = format.format {
22+
setDateTime(LocalDate(2021, 1, 13).atTime(9, 34, 58, 120_000_000))
23+
timeZoneId = "Europe/Paris"
24+
}
25+
check(formatted == "2021-01-13T09:34:58.12[Europe/Paris]")
26+
val parsed = format.parse("2021-01-13T09:34:58.12[Europe/Paris]")
27+
check(parsed.toLocalDateTime() == LocalDate(2021, 1, 13).atTime(9, 34, 58, 120_000_000))
28+
check(parsed.timeZoneId == "Europe/Paris")
29+
}
30+
31+
@Test
32+
fun dateTimeComponents() {
33+
val format = DateTimeComponents.Format {
34+
char('{')
35+
dateTimeComponents(DateTimeComponents.Formats.RFC_1123)
36+
char('}')
37+
}
38+
val formatted = format.format {
39+
setDateTimeOffset(
40+
LocalDate(2021, 1, 13).atTime(9, 34, 58, 120_000_000),
41+
UtcOffset(hours = 3, minutes = 30)
42+
)
43+
}
44+
check(formatted == "{Wed, 13 Jan 2021 09:34:58 +0330}")
45+
}
46+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2019-2024 JetBrains s.r.o. and contributors.
3+
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
4+
*/
5+
6+
package kotlinx.datetime.test.samples.format
7+
8+
import kotlinx.datetime.*
9+
import kotlinx.datetime.format.*
10+
import kotlin.test.*
11+
12+
class LocalDateTimeFormatSamples {
13+
@Test
14+
fun dateTime() {
15+
val format = DateTimeComponents.Format {
16+
dateTime(LocalDateTime.Formats.ISO)
17+
offset(UtcOffset.Formats.FOUR_DIGITS)
18+
}
19+
val formatted = format.format {
20+
setDateTimeOffset(
21+
LocalDate(2021, 1, 13).atTime(9, 34, 58, 120_000_000),
22+
UtcOffset(hours = 2)
23+
)
24+
}
25+
check(formatted == "2021-01-13T09:34:58.12+0200")
26+
}
27+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2019-2024 JetBrains s.r.o. and contributors.
3+
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
4+
*/
5+
6+
package kotlinx.datetime.test.samples.format
7+
8+
import kotlinx.datetime.*
9+
import kotlinx.datetime.format.*
10+
import kotlin.test.*
11+
12+
class LocalTimeFormatSamples {
13+
@Test
14+
fun hhmmss() {
15+
// format the local time as a single number
16+
val format = LocalTime.Format {
17+
hour(); minute(); second()
18+
optional { char('.'); secondFraction(1, 9) }
19+
}
20+
val formatted = format.format(LocalTime(9, 34, 58, 120_000_000))
21+
check(formatted == "093458.12")
22+
}
23+
24+
@Test
25+
fun amPm() {
26+
val format = LocalTime.Format {
27+
amPmHour(); char(':'); minute(); char(':'); second()
28+
char(' '); amPmMarker("AM", "PM")
29+
}
30+
val formatted = format.format(LocalTime(9, 34, 58, 120_000_000))
31+
check(formatted == "09:34:58 AM")
32+
}
33+
34+
@Test
35+
fun fixedLengthSecondFraction() {
36+
val format = LocalTime.Format {
37+
hour(); char(':'); minute(); char(':'); second()
38+
char('.'); secondFraction(fixedLength = 3)
39+
}
40+
val formatted = format.format(LocalTime(9, 34, 58, 120_000_000))
41+
check(formatted == "09:34:58.120")
42+
}
43+
44+
@Test
45+
fun time() {
46+
val format = LocalDateTime.Format {
47+
date(LocalDate.Formats.ISO)
48+
char(' ')
49+
time(LocalTime.Formats.ISO)
50+
}
51+
val formatted = format.format(LocalDateTime(2021, 1, 13, 9, 34, 58, 120_000_000))
52+
check(formatted == "2021-01-13 09:34:58.12")
53+
}
54+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2019-2024 JetBrains s.r.o. and contributors.
3+
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
4+
*/
5+
6+
package kotlinx.datetime.test.samples.format
7+
8+
import kotlinx.datetime.*
9+
import kotlinx.datetime.format.*
10+
import kotlin.test.*
11+
12+
class UtcOffsetFormatSamples {
13+
@Test
14+
fun isoOrGmt() {
15+
val format = UtcOffset.Format {
16+
// if the offset is zero, `GMT` is printed
17+
optional("GMT") {
18+
offsetHours(); char(':'); offsetMinutesOfHour()
19+
// if seconds are zero, they are omitted
20+
optional { char(':'); offsetSecondsOfMinute() }
21+
}
22+
}
23+
check(format.format(UtcOffset.ZERO) == "GMT")
24+
check(format.format(UtcOffset(hours = -2)) == "-02:00")
25+
check(format.format(UtcOffset(hours = -2, minutes = -30)) == "-02:30")
26+
check(format.format(UtcOffset(hours = -2, minutes = -30, seconds = -59)) == "-02:30:59")
27+
}
28+
29+
@Test
30+
fun offset() {
31+
val format = DateTimeComponents.Format {
32+
dateTime(LocalDateTime.Formats.ISO)
33+
offset(UtcOffset.Formats.FOUR_DIGITS)
34+
}
35+
val formatted = format.format {
36+
setDateTimeOffset(
37+
LocalDate(2021, 1, 13).atTime(9, 34, 58, 120_000_000),
38+
UtcOffset(hours = 2)
39+
)
40+
}
41+
check(formatted == "2021-01-13T09:34:58.12+0200")
42+
}
43+
}

0 commit comments

Comments
 (0)