Skip to content

Commit f2feedc

Browse files
committed
Check that we can replicate java.time.Instant.parse
1 parent 2e8d537 commit f2feedc

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

core/jvm/test/InstantParsing.kt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package kotlinx.datetime
2+
3+
import kotlinx.datetime.format.*
4+
import kotlin.test.*
5+
6+
class InstantParsing {
7+
@Test
8+
fun testParsingInvalidInstants() {
9+
fun parseInstantLikeJavaDoes(input: String): Instant =
10+
DateTimeComponents.Formats.ISO_DATE_TIME_OFFSET.parse(input).apply {
11+
when {
12+
hour == 24 && minute == 0 && second == 0 && nanosecond == 0 -> {
13+
setDate(toLocalDate().plus(1, DateTimeUnit.DAY))
14+
hour = 0
15+
}
16+
hour == 23 && minute == 59 && second == 60 -> second = 59
17+
}
18+
}.toInstantUsingOffset()
19+
fun formatTwoDigits(i: Int) = if (i < 10) "0$i" else "$i"
20+
for (hour in 23..25) {
21+
for (minuteRange in listOf(0..5, 58..62)) {
22+
for (minute in minuteRange) {
23+
for (secondRange in listOf(0..5, 58..62)) {
24+
for (second in secondRange) {
25+
val input = "2020-03-16T${hour}:${formatTwoDigits(minute)}:${formatTwoDigits(second)}Z"
26+
assertEquals(
27+
runCatching { java.time.Instant.parse(input) }.getOrNull(),
28+
kotlin.runCatching { parseInstantLikeJavaDoes(input).toJavaInstant() }.getOrNull()
29+
)
30+
}
31+
}
32+
}
33+
}
34+
}
35+
}
36+
37+
}

0 commit comments

Comments
 (0)