Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Instant.parse succeeding even when seconds are omitted on the JVM and JS #370

Merged
merged 4 commits into from
May 13, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove unused functions
  • Loading branch information
dkhalanskyjb committed May 13, 2024
commit 07c5a5689ae3192dea9f52156c2781194cfff1de
12 changes: 0 additions & 12 deletions core/commonJs/src/Instant.kt
Original file line number Diff line number Diff line change
@@ -7,7 +7,6 @@ package kotlinx.datetime

import kotlinx.datetime.format.*
import kotlinx.datetime.internal.JSJoda.Instant as jtInstant
import kotlinx.datetime.internal.JSJoda.OffsetDateTime as jtOffsetDateTime
import kotlinx.datetime.internal.JSJoda.Duration as jtDuration
import kotlinx.datetime.internal.JSJoda.Clock as jtClock
import kotlinx.datetime.internal.JSJoda.ChronoUnit as jtChronoUnit
@@ -84,17 +83,6 @@ public actual class Instant internal constructor(internal val value: jtInstant)
@Deprecated("This overload is only kept for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun parse(isoString: String): Instant = parse(input = isoString)

/** A workaround for the string representations of Instant that have an offset of the form
* "+XX" not being recognized by [jtOffsetDateTime.parse], while "+XX:XX" work fine. */
private fun fixOffsetRepresentation(isoString: String): String {
val time = isoString.indexOf('T', ignoreCase = true)
if (time == -1) return isoString // the string is malformed
val offset = isoString.indexOfLast { c -> c == '+' || c == '-' }
if (offset < time) return isoString // the offset is 'Z' and not +/- something else
val separator = isoString.indexOf(':', offset) // if there is a ':' in the offset, no changes needed
return if (separator != -1) isoString else "$isoString:00"
}

public actual fun fromEpochSeconds(epochSeconds: Long, nanosecondAdjustment: Long): Instant = try {
/* Performing normalization here because otherwise this fails:
assertEquals((Long.MAX_VALUE % 1_000_000_000).toInt(),
13 changes: 0 additions & 13 deletions core/jvm/src/Instant.kt
Original file line number Diff line number Diff line change
@@ -12,13 +12,11 @@ import kotlinx.datetime.internal.*
import kotlinx.datetime.serializers.InstantIso8601Serializer
import kotlinx.serialization.Serializable
import java.time.DateTimeException
import java.time.format.*
import java.time.temporal.*
import kotlin.time.*
import kotlin.time.Duration.Companion.nanoseconds
import kotlin.time.Duration.Companion.seconds
import java.time.Instant as jtInstant
import java.time.OffsetDateTime as jtOffsetDateTime
import java.time.Clock as jtClock

@Serializable(with = InstantIso8601Serializer::class)
@@ -83,17 +81,6 @@ public actual class Instant internal constructor(internal val value: jtInstant)
@Deprecated("This overload is only kept for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun parse(isoString: String): Instant = parse(input = isoString)

/** A workaround for a quirk of the JDKs older than 11 where the string representations of Instant that have an
* offset of the form "+XX" are not recognized by [jtOffsetDateTime.parse], while "+XX:XX" work fine. */
private fun fixOffsetRepresentation(isoString: CharSequence): CharSequence {
val time = isoString.indexOf('T', ignoreCase = true)
if (time == -1) return isoString // the string is malformed
val offset = isoString.indexOfLast { c -> c == '+' || c == '-' }
if (offset < time) return isoString // the offset is 'Z' and not +/- something else
val separator = isoString.indexOf(':', offset) // if there is a ':' in the offset, no changes needed
return if (separator != -1) isoString else "$isoString:00"
}

public actual fun fromEpochSeconds(epochSeconds: Long, nanosecondAdjustment: Long): Instant = try {
Instant(jtInstant.ofEpochSecond(epochSeconds, nanosecondAdjustment))
} catch (e: Exception) {