File tree 3 files changed +16
-11
lines changed 3 files changed +16
-11
lines changed Original file line number Diff line number Diff line change @@ -94,8 +94,7 @@ class InstantTest {
94
94
Pair (" 2020-01-01T00:01:01.010203040+17:59" , " 2019-12-31T06:02:01.010203040Z" ),
95
95
Pair (" 2020-01-01T00:01:01+00" , " 2020-01-01T00:01:01Z" ),
96
96
)
97
- strings.forEach {
98
- val (str, strInZ) = it
97
+ strings.forEach { (str, strInZ) ->
99
98
val instant = Instant .parse(str)
100
99
assertEquals(Instant .parse(strInZ), instant, str)
101
100
assertEquals(strInZ, instant.toString(), str)
@@ -124,7 +123,7 @@ class InstantTest {
124
123
125
124
for (instant in instants) {
126
125
for (offset in offsets) {
127
- val str = instant.toStringWithOffset(TimeZone .of( " +03:12:14 " ) as ZoneOffset )
126
+ val str = instant.toStringWithOffset(offset )
128
127
assertEquals(instant, Instant .parse(str))
129
128
}
130
129
}
Original file line number Diff line number Diff line change @@ -83,13 +83,16 @@ public actual class Instant internal constructor(internal val value: jtInstant)
83
83
throw e
84
84
}
85
85
86
- /* * A workaround for a bug where the string representations of Instant that have an offset of the form
87
- * "+XX" are not recognized by [jtOffsetDateTime.parse], while "+XX:XX" work fine.
86
+ /* * A workaround for the string representations of Instant that have an offset of the form
87
+ * "+XX" not being recognized by [jtOffsetDateTime.parse], while "+XX:XX" work fine.
88
88
* See [the Github issue](https://github.com/js-joda/js-joda/issues/492). */
89
89
private fun fixOffsetRepresentation (isoString : String ): String {
90
- val time = isoString.split(" T" ).elementAtOrNull(1 ) ? : return isoString
91
- val offset = time.split(" +" , " -" ).elementAtOrNull(1 ) ? : return isoString
92
- return if (offset.contains(" :" )) isoString else " $isoString :00"
90
+ val time = isoString.indexOf(' T' , ignoreCase = true )
91
+ if (time == - 1 ) return isoString // the string is malformed
92
+ val offset = isoString.indexOfLast { c -> c == ' +' || c == ' -' }
93
+ if (offset < time) return isoString // the offset is 'Z' and not +/- something else
94
+ val separator = isoString.indexOf(' :' , offset) // if there is a ':' in the offset, no changes needed
95
+ return if (separator != - 1 ) isoString else " $isoString :00"
93
96
}
94
97
95
98
public actual fun fromEpochSeconds (epochSeconds : Long , nanosecondAdjustment : Long ): Instant = try {
Original file line number Diff line number Diff line change @@ -72,9 +72,12 @@ public actual class Instant internal constructor(internal val value: jtInstant)
72
72
/* * A workaround for a quirk of the JDKs older than 11 where the string representations of Instant that have an
73
73
* offset of the form "+XX" are not recognized by [jtOffsetDateTime.parse], while "+XX:XX" work fine. */
74
74
private fun fixOffsetRepresentation (isoString : String ): String {
75
- val time = isoString.split(" T" ).elementAtOrNull(1 ) ? : return isoString
76
- val offset = time.split(" +" , " -" ).elementAtOrNull(1 ) ? : return isoString
77
- return if (offset.contains(" :" )) isoString else " $isoString :00"
75
+ val time = isoString.indexOf(' T' , ignoreCase = true )
76
+ if (time == - 1 ) return isoString // the string is malformed
77
+ val offset = isoString.indexOfLast { c -> c == ' +' || c == ' -' }
78
+ if (offset < time) return isoString // the offset is 'Z' and not +/- something else
79
+ val separator = isoString.indexOf(' :' , offset) // if there is a ':' in the offset, no changes needed
80
+ return if (separator != - 1 ) isoString else " $isoString :00"
78
81
}
79
82
80
83
public actual fun fromEpochSeconds (epochSeconds : Long , nanosecondAdjustment : Long ): Instant = try {
You can’t perform that action at this time.
0 commit comments