Skip to content

Commit c8f737c

Browse files
committed
Fix JDK8 not recognizing +hh as a correct offset for OffsetDateTime
1 parent 3e11ed8 commit c8f737c

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

core/jvm/src/Instant.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,19 @@ public actual class Instant internal constructor(internal val value: jtInstant)
6464
Instant(jtInstant.ofEpochMilli(epochMilliseconds))
6565

6666
public actual fun parse(isoString: String): Instant = try {
67-
Instant(jtOffsetDateTime.parse(isoString).toInstant())
67+
Instant(jtOffsetDateTime.parse(fixOffsetRepresentation(isoString)).toInstant())
6868
} catch (e: DateTimeParseException) {
6969
throw DateTimeFormatException(e)
7070
}
7171

72+
/** A workaround for a quirk of the JDKs older than 11 where the string representations of Instant that have an
73+
* offset of the form "+XX" are not recognized by [jtOffsetDateTime.parse], while "+XX:XX" work fine. */
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"
78+
}
79+
7280
public actual fun fromEpochSeconds(epochSeconds: Long, nanosecondAdjustment: Long): Instant = try {
7381
Instant(jtInstant.ofEpochSecond(epochSeconds, nanosecondAdjustment))
7482
} catch (e: Exception) {

0 commit comments

Comments
 (0)