Skip to content

Commit 5f66827

Browse files
committed
Ensure that creation of ambiguous formats fails
Remove delayed initialization of parsing for this purpose and document the thrown exception.
1 parent f552d01 commit 5f66827

File tree

7 files changed

+22
-2
lines changed

7 files changed

+22
-2
lines changed

core/common/src/LocalDate.kt

+2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ public expect class LocalDate : Comparable<LocalDate> {
6565
* (for example, [dayOfMonth] is 31 for February), consider using [DateTimeComponents.Format] instead.
6666
*
6767
* There is a collection of predefined formats in [LocalDate.Formats].
68+
*
69+
* @throws IllegalArgumentException if parsing using this format is ambiguous.
6870
*/
6971
@Suppress("FunctionName")
7072
public fun Format(block: DateTimeFormatBuilder.WithDate.() -> Unit): DateTimeFormat<LocalDate>

core/common/src/LocalDateTime.kt

+2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ public expect class LocalDateTime : Comparable<LocalDateTime> {
6262
* (for example, [dayOfMonth] is 31 for February), consider using [DateTimeComponents.Format] instead.
6363
*
6464
* There is a collection of predefined formats in [LocalDateTime.Formats].
65+
*
66+
* @throws IllegalArgumentException if parsing using this format is ambiguous.
6567
*/
6668
@Suppress("FunctionName")
6769
public fun Format(builder: DateTimeFormatBuilder.WithDateTime.() -> Unit): DateTimeFormat<LocalDateTime>

core/common/src/LocalTime.kt

+2
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ public expect class LocalTime : Comparable<LocalTime> {
9797
* (for example, [second] is 60), consider using [DateTimeComponents.Format] instead.
9898
*
9999
* There is a collection of predefined formats in [LocalTime.Formats].
100+
*
101+
* @throws IllegalArgumentException if parsing using this format is ambiguous.
100102
*/
101103
@Suppress("FunctionName")
102104
public fun Format(builder: DateTimeFormatBuilder.WithTime.() -> Unit): DateTimeFormat<LocalTime>

core/common/src/UtcOffset.kt

+2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ public expect class UtcOffset {
6767
* [DateTimeFormatBuilder.WithUtcOffset.offset] in a format builder for a larger data structure.
6868
*
6969
* There is a collection of predefined formats in [UtcOffset.Formats].
70+
*
71+
* @throws IllegalArgumentException if parsing using this format is ambiguous.
7072
*/
7173
@Suppress("FunctionName")
7274
public fun Format(block: DateTimeFormatBuilder.WithUtcOffset.() -> Unit): DateTimeFormat<UtcOffset>

core/common/src/format/DateTimeComponents.kt

+2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ public class DateTimeComponents internal constructor(internal val contents: Date
6969
* Creates a [DateTimeFormat] for [DateTimeComponents] values using [DateTimeFormatBuilder.WithDateTimeComponents].
7070
*
7171
* There is a collection of predefined formats in [DateTimeComponents.Formats].
72+
*
73+
* @throws IllegalArgumentException if parsing using this format is ambiguous.
7274
*/
7375
@Suppress("FunctionName")
7476
public fun Format(block: DateTimeFormatBuilder.WithDateTimeComponents.() -> Unit): DateTimeFormat<DateTimeComponents> {

core/common/src/internal/format/FormatStructure.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,8 @@ internal open class ConcatenatedFormatStructure<in T>(
231231

232232
internal class CachedFormatStructure<in T>(formats: List<NonConcatenatedFormatStructure<T>>) :
233233
ConcatenatedFormatStructure<T>(formats) {
234-
private val cachedFormatter: FormatterStructure<T> by lazy { super.formatter() }
235-
private val cachedParser: ParserStructure<T> by lazy { super.parser() }
234+
private val cachedFormatter: FormatterStructure<T> = super.formatter()
235+
private val cachedParser: ParserStructure<T> = super.parser()
236236

237237
override fun formatter(): FormatterStructure<T> = cachedFormatter
238238

core/common/test/format/DateTimeFormatTest.kt

+10
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,14 @@ class DateTimeFormatTest {
127127
DateTimeComponents.Format { chars(format) }.parse(format)
128128
}
129129
}
130+
131+
@Test
132+
fun testCreatingAmbiguousFormat() {
133+
assertFailsWith< IllegalArgumentException> {
134+
DateTimeComponents.Format {
135+
monthNumber(Padding.NONE)
136+
dayOfMonth(Padding.NONE)
137+
}
138+
}
139+
}
130140
}

0 commit comments

Comments
 (0)