Skip to content

Commit 5635726

Browse files
committed
Delegate to JVM implementations in parseOrNull where possible
1 parent c7ea1ab commit 5635726

File tree

13 files changed

+88
-12
lines changed

13 files changed

+88
-12
lines changed

core/api/kotlinx-datetime.api

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,8 @@ public final class kotlinx/datetime/LocalDateJvmKt {
322322
public static final fun daysUntil (Lkotlinx/datetime/LocalDate;Lkotlinx/datetime/LocalDate;)I
323323
public static final fun minus (Lkotlinx/datetime/LocalDate;ILkotlinx/datetime/DateTimeUnit$DateBased;)Lkotlinx/datetime/LocalDate;
324324
public static final fun monthsUntil (Lkotlinx/datetime/LocalDate;Lkotlinx/datetime/LocalDate;)I
325+
public static final fun parseOrNull (Lkotlinx/datetime/LocalDate$Companion;Ljava/lang/CharSequence;Lkotlinx/datetime/format/DateTimeFormat;)Lkotlinx/datetime/LocalDate;
326+
public static synthetic fun parseOrNull$default (Lkotlinx/datetime/LocalDate$Companion;Ljava/lang/CharSequence;Lkotlinx/datetime/format/DateTimeFormat;ILjava/lang/Object;)Lkotlinx/datetime/LocalDate;
325327
public static final fun periodUntil (Lkotlinx/datetime/LocalDate;Lkotlinx/datetime/LocalDate;)Lkotlinx/datetime/DatePeriod;
326328
public static final fun plus (Lkotlinx/datetime/LocalDate;ILkotlinx/datetime/DateTimeUnit$DateBased;)Lkotlinx/datetime/LocalDate;
327329
public static final fun plus (Lkotlinx/datetime/LocalDate;JLkotlinx/datetime/DateTimeUnit$DateBased;)Lkotlinx/datetime/LocalDate;
@@ -344,8 +346,6 @@ public final class kotlinx/datetime/LocalDateKt {
344346
public static final fun minus (Lkotlinx/datetime/LocalDate;Lkotlinx/datetime/DatePeriod;)Lkotlinx/datetime/LocalDate;
345347
public static final fun minus (Lkotlinx/datetime/LocalDate;Lkotlinx/datetime/DateTimeUnit$DateBased;)Lkotlinx/datetime/LocalDate;
346348
public static final fun minus (Lkotlinx/datetime/LocalDate;Lkotlinx/datetime/LocalDate;)Lkotlinx/datetime/DatePeriod;
347-
public static final fun parseOrNull (Lkotlinx/datetime/LocalDate$Companion;Ljava/lang/CharSequence;Lkotlinx/datetime/format/DateTimeFormat;)Lkotlinx/datetime/LocalDate;
348-
public static synthetic fun parseOrNull$default (Lkotlinx/datetime/LocalDate$Companion;Ljava/lang/CharSequence;Lkotlinx/datetime/format/DateTimeFormat;ILjava/lang/Object;)Lkotlinx/datetime/LocalDate;
349349
public static final fun plus (Lkotlinx/datetime/LocalDate;ILkotlinx/datetime/DateTimeUnit$DateBased;)Lkotlinx/datetime/LocalDate;
350350
public static final fun toLocalDate (Ljava/lang/String;)Lkotlinx/datetime/LocalDate;
351351
}
@@ -617,14 +617,14 @@ public final class kotlinx/datetime/UtcOffset$Formats {
617617
public final class kotlinx/datetime/UtcOffsetJvmKt {
618618
public static final fun UtcOffset (Ljava/lang/Integer;Ljava/lang/Integer;Ljava/lang/Integer;)Lkotlinx/datetime/UtcOffset;
619619
public static synthetic fun UtcOffset$default (Ljava/lang/Integer;Ljava/lang/Integer;Ljava/lang/Integer;ILjava/lang/Object;)Lkotlinx/datetime/UtcOffset;
620+
public static final fun parseOrNull (Lkotlinx/datetime/UtcOffset$Companion;Ljava/lang/CharSequence;Lkotlinx/datetime/format/DateTimeFormat;)Lkotlinx/datetime/UtcOffset;
621+
public static synthetic fun parseOrNull$default (Lkotlinx/datetime/UtcOffset$Companion;Ljava/lang/CharSequence;Lkotlinx/datetime/format/DateTimeFormat;ILjava/lang/Object;)Lkotlinx/datetime/UtcOffset;
620622
}
621623

622624
public final class kotlinx/datetime/UtcOffsetKt {
623625
public static final fun UtcOffset ()Lkotlinx/datetime/UtcOffset;
624626
public static final fun asTimeZone (Lkotlinx/datetime/UtcOffset;)Lkotlinx/datetime/FixedOffsetTimeZone;
625627
public static final fun format (Lkotlinx/datetime/UtcOffset;Lkotlinx/datetime/format/DateTimeFormat;)Ljava/lang/String;
626-
public static final fun parseOrNull (Lkotlinx/datetime/UtcOffset$Companion;Ljava/lang/CharSequence;Lkotlinx/datetime/format/DateTimeFormat;)Lkotlinx/datetime/UtcOffset;
627-
public static synthetic fun parseOrNull$default (Lkotlinx/datetime/UtcOffset$Companion;Ljava/lang/CharSequence;Lkotlinx/datetime/format/DateTimeFormat;ILjava/lang/Object;)Lkotlinx/datetime/UtcOffset;
628628
}
629629

630630
public final class kotlinx/datetime/format/AmPmMarker : java/lang/Enum {

core/common/src/LocalDate.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,9 @@ public expect class LocalDate : Comparable<LocalDate> {
305305
* @see LocalDate.parse for a version of this function that throws an exception on faulty input.
306306
* @sample kotlinx.datetime.test.samples.LocalDateSamples.parseOrNull
307307
*/
308-
public fun LocalDate.Companion.parseOrNull(
308+
public expect fun LocalDate.Companion.parseOrNull(
309309
input: CharSequence, format: DateTimeFormat<LocalDate> = getIsoDateFormat()
310-
): LocalDate? = format.parseOrNull(input)
310+
): LocalDate?
311311

312312
/**
313313
* @suppress

core/common/src/LocalDateTime.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,9 +410,9 @@ public expect class LocalDateTime : Comparable<LocalDateTime> {
410410
* @see LocalDateTime.parse for a version of this function that throws an exception on faulty input.
411411
* @sample kotlinx.datetime.test.samples.LocalDateTimeSamples.parseOrNull
412412
*/
413-
public fun LocalDateTime.Companion.parseOrNull(
413+
public expect fun LocalDateTime.Companion.parseOrNull(
414414
input: CharSequence, format: DateTimeFormat<LocalDateTime> = getIsoDateTimeFormat()
415-
): LocalDateTime? = format.parseOrNull(input)
415+
): LocalDateTime?
416416

417417
/**
418418
* @suppress

core/common/src/LocalTime.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,9 @@ public expect class LocalTime : Comparable<LocalTime> {
367367
* @see LocalTime.parse for a version of this function that throws an exception on faulty input.
368368
* @sample kotlinx.datetime.test.samples.LocalTimeSamples.parseOrNull
369369
*/
370-
public fun LocalTime.Companion.parseOrNull(
370+
public expect fun LocalTime.Companion.parseOrNull(
371371
input: CharSequence, format: DateTimeFormat<LocalTime> = getIsoTimeFormat()
372-
): LocalTime? = format.parseOrNull(input)
372+
): LocalTime?
373373

374374
/**
375375
* Formats this value using the given [format].

core/common/src/UtcOffset.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,9 @@ public expect class UtcOffset {
208208
* @see UtcOffset.parse for a version of this function that throws an exception on faulty input.
209209
* @sample kotlinx.datetime.test.samples.UtcOffsetSamples.parseOrNull
210210
*/
211-
public fun UtcOffset.Companion.parseOrNull(
211+
public expect fun UtcOffset.Companion.parseOrNull(
212212
input: CharSequence, format: DateTimeFormat<UtcOffset> = getIsoUtcOffsetFormat()
213-
): UtcOffset? = format.parseOrNull(input)
213+
): UtcOffset?
214214

215215
/**
216216
* Formats this value using the given [format].

core/commonKotlin/src/LocalDate.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,10 @@ public actual class LocalDate actual constructor(public actual val year: Int, mo
214214
actual override fun toString(): String = format(Formats.ISO)
215215
}
216216

217+
public actual fun LocalDate.Companion.parseOrNull(
218+
input: CharSequence, format: DateTimeFormat<LocalDate>
219+
): LocalDate? = format.parseOrNull(input)
220+
217221
@Deprecated("Use the plus overload with an explicit number of units", ReplaceWith("this.plus(1, unit)"))
218222
public actual fun LocalDate.plus(unit: DateTimeUnit.DateBased): LocalDate = plus(1, unit)
219223

core/commonKotlin/src/LocalDateTime.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ public actual constructor(public actual val date: LocalDate, public actual val t
8484
}
8585
}
8686

87+
public actual fun LocalDateTime.Companion.parseOrNull(
88+
input: CharSequence, format: DateTimeFormat<LocalDateTime>
89+
): LocalDateTime? = format.parseOrNull(input)
90+
8791
// org.threeten.bp.LocalDateTime#until
8892
internal fun LocalDateTime.until(other: LocalDateTime, unit: DateTimeUnit.DateBased): Long {
8993
var endDate: LocalDate = other.date

core/commonKotlin/src/LocalTime.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ public actual class LocalTime actual constructor(
136136

137137
}
138138

139+
public actual fun LocalTime.Companion.parseOrNull(
140+
input: CharSequence, format: DateTimeFormat<LocalTime>
141+
): LocalTime? = format.parseOrNull(input)
142+
139143
internal val ISO_TIME_OPTIONAL_SECONDS_TRAILING_ZEROS by lazy {
140144
LocalTimeFormat.build {
141145
hour()

core/commonKotlin/src/UtcOffset.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ public actual class UtcOffset private constructor(public actual val totalSeconds
9494
}
9595
}
9696

97+
public actual fun UtcOffset.Companion.parseOrNull(
98+
input: CharSequence, format: DateTimeFormat<UtcOffset>
99+
): UtcOffset? = format.parseOrNull(input)
100+
97101
@ThreadLocal
98102
private var utcOffsetCache: MutableMap<Int, UtcOffset> = mutableMapOf()
99103

core/jvm/src/LocalDate.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
@file:JvmName("LocalDateJvmKt")
66
package kotlinx.datetime
77

8+
import kotlinx.datetime.LocalDate.Formats
89
import kotlinx.datetime.format.*
910
import kotlinx.datetime.internal.safeAdd
1011
import kotlinx.datetime.internal.safeMultiply
@@ -110,6 +111,21 @@ public actual class LocalDate internal constructor(
110111
private fun writeReplace(): Any = Ser(Ser.DATE_TAG, this)
111112
}
112113

114+
public actual fun LocalDate.Companion.parseOrNull(
115+
input: CharSequence, format: DateTimeFormat<LocalDate>
116+
): LocalDate? =
117+
if (format === Formats.ISO) {
118+
try {
119+
val sanitizedInput = removeLeadingZerosFromLongYearFormLocalDate(input.toString())
120+
jtLocalDate.parse(sanitizedInput).let(::LocalDate)
121+
} catch (_: DateTimeParseException) {
122+
null
123+
}
124+
} else {
125+
format.parseOrNull(input)
126+
}
127+
128+
113129
/**
114130
* @suppress
115131
*/

core/jvm/src/LocalDateTimeJvm.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
@file:JvmMultifileClass
77
package kotlinx.datetime
88

9+
import kotlinx.datetime.LocalDateTime.Formats
910
import kotlinx.datetime.format.*
1011
import kotlinx.datetime.internal.removeLeadingZerosFromLongYearFormLocalDateTime
1112
import kotlinx.datetime.serializers.*
@@ -115,6 +116,20 @@ public actual class LocalDateTime internal constructor(
115116
private fun writeReplace(): Any = Ser(Ser.DATE_TIME_TAG, this)
116117
}
117118

119+
public actual fun LocalDateTime.Companion.parseOrNull(
120+
input: CharSequence, format: DateTimeFormat<LocalDateTime>
121+
): LocalDateTime? =
122+
if (format === Formats.ISO) {
123+
try {
124+
val sanitizedInput = removeLeadingZerosFromLongYearFormLocalDateTime(input.toString())
125+
jtLocalDateTime.parse(sanitizedInput).let(::LocalDateTime)
126+
} catch (_: DateTimeParseException) {
127+
null
128+
}
129+
} else {
130+
format.parseOrNull(input)
131+
}
132+
118133
/**
119134
* @suppress
120135
*/

core/jvm/src/LocalTimeJvm.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
package kotlinx.datetime
99

10+
import kotlinx.datetime.LocalTime.Formats
1011
import kotlinx.datetime.format.*
1112
import kotlinx.datetime.internal.*
1213
import kotlinx.datetime.serializers.*
@@ -95,6 +96,18 @@ public actual class LocalTime internal constructor(
9596
private fun writeReplace(): Any = Ser(Ser.TIME_TAG, this)
9697
}
9798

99+
public actual fun LocalTime.Companion.parseOrNull(input: CharSequence, format: DateTimeFormat<LocalTime>): LocalTime? =
100+
if (format === Formats.ISO) {
101+
try {
102+
jtLocalTime.parse(input).let(::LocalTime)
103+
} catch (_: DateTimeParseException) {
104+
null
105+
}
106+
} else {
107+
format.parseOrNull(input)
108+
}
109+
110+
98111
@Deprecated(
99112
"Use kotlinx.datetime.Month",
100113
ReplaceWith("atDate(year, month.toKotlinMonth(), dayOfMonth)")

core/jvm/src/UtcOffsetJvm.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package kotlinx.datetime
77

8+
import kotlinx.datetime.UtcOffset.Formats
89
import kotlinx.datetime.format.*
910
import kotlinx.datetime.serializers.*
1011
import kotlinx.serialization.Serializable
@@ -67,6 +68,15 @@ public actual fun UtcOffset(hours: Int? = null, minutes: Int? = null, seconds: I
6768
throw IllegalArgumentException(e)
6869
}
6970

71+
public actual fun UtcOffset.Companion.parseOrNull(
72+
input: CharSequence, format: DateTimeFormat<UtcOffset>
73+
): UtcOffset? = when {
74+
format === Formats.ISO -> parseWithFormatOrNull(input, isoFormat)
75+
format === Formats.ISO_BASIC -> parseWithFormatOrNull(input, isoBasicFormat)
76+
format === Formats.FOUR_DIGITS -> parseWithFormatOrNull(input, fourDigitsFormat)
77+
else -> format.parseOrNull(input)
78+
}
79+
7080
private val isoFormat by lazy {
7181
DateTimeFormatterBuilder().parseCaseInsensitive().appendOffsetId().toFormatter()
7282
}
@@ -82,3 +92,9 @@ private fun parseWithFormat(input: CharSequence, format: DateTimeFormatter) = tr
8292
} catch (e: DateTimeException) {
8393
throw DateTimeFormatException(e)
8494
}
95+
96+
private fun parseWithFormatOrNull(input: CharSequence, format: DateTimeFormatter) = try {
97+
format.parse(input, ZoneOffset::from).let(::UtcOffset)
98+
} catch (_: DateTimeException) {
99+
null
100+
}

0 commit comments

Comments
 (0)