Skip to content

Commit 3cf0fa5

Browse files
committed
Fixes
1 parent c8f737c commit 3cf0fa5

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

core/common/test/InstantTest.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ class InstantTest {
9494
Pair("2020-01-01T00:01:01.010203040+17:59", "2019-12-31T06:02:01.010203040Z"),
9595
Pair("2020-01-01T00:01:01+00", "2020-01-01T00:01:01Z"),
9696
)
97-
strings.forEach {
98-
val (str, strInZ) = it
97+
strings.forEach { (str, strInZ) ->
9998
val instant = Instant.parse(str)
10099
assertEquals(Instant.parse(strInZ), instant, str)
101100
assertEquals(strInZ, instant.toString(), str)
@@ -124,7 +123,7 @@ class InstantTest {
124123

125124
for (instant in instants) {
126125
for (offset in offsets) {
127-
val str = instant.toStringWithOffset(TimeZone.of("+03:12:14") as ZoneOffset)
126+
val str = instant.toStringWithOffset(offset)
128127
assertEquals(instant, Instant.parse(str))
129128
}
130129
}

core/js/src/Instant.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,16 @@ public actual class Instant internal constructor(internal val value: jtInstant)
8383
throw e
8484
}
8585

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.
8888
* See [the Github issue](https://github.com/js-joda/js-joda/issues/492). */
8989
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"
9396
}
9497

9598
public actual fun fromEpochSeconds(epochSeconds: Long, nanosecondAdjustment: Long): Instant = try {

core/jvm/src/Instant.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,12 @@ public actual class Instant internal constructor(internal val value: jtInstant)
7272
/** A workaround for a quirk of the JDKs older than 11 where the string representations of Instant that have an
7373
* offset of the form "+XX" are not recognized by [jtOffsetDateTime.parse], while "+XX:XX" work fine. */
7474
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"
7881
}
7982

8083
public actual fun fromEpochSeconds(epochSeconds: Long, nanosecondAdjustment: Long): Instant = try {

0 commit comments

Comments
 (0)