to Instant
Returns an instant that corresponds to this civil datetime value in the time zone provided as an implicit receiver.
Note that the conversion is not always well-defined. There can be the following possible situations:
Only one instant has this datetime value in the timeZone. In this case, the conversion is unambiguous.
No instant has this datetime value in the timeZone. Such a situation appears when the time zone experiences a transition from a lesser to a greater offset. In this case, the conversion is performed with the lesser (earlier) offset, as if the time gap didn't occur yet.
Two possible instants can have these datetime components in the timeZone. In this case, the earlier instant is returned.
See also
Samples
import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.test.*
fun main() {
//sampleStart
// Converting a local date-time to an instant in a specific time zone
val zone = TimeZone.of("America/New_York")
val localDateTime = LocalDate(2023, 6, 2).atTime(12, 30)
val instant = with(zone) {
localDateTime.toInstant()
}
check(instant == Instant.parse("2023-06-02T16:30:00Z"))
//sampleEnd
}