File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments