commonMain.io.islandtime.ranges.internal.Common.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of core-metadata Show documentation
Show all versions of core-metadata Show documentation
A multiplatform library for working with dates and times
The newest version!
package io.islandtime.ranges.internal
import io.islandtime.DateTime
import io.islandtime.measures.*
import io.islandtime.measures.internal.minusWithOverflow
import io.islandtime.ranges.Interval
internal val MAX_INCLUSIVE_END_DATE_TIME = DateTime.MAX - 2.nanoseconds
internal fun secondsBetween(
startSeconds: LongSeconds,
startNanoseconds: IntNanoseconds,
endExclusiveSeconds: LongSeconds,
endExclusiveNanoseconds: IntNanoseconds
): LongSeconds {
val secondDiff = endExclusiveSeconds - startSeconds
val nanoDiff = endExclusiveNanoseconds minusWithOverflow startNanoseconds
return when {
secondDiff.value > 0 && nanoDiff.value < 0 -> secondDiff - 1.seconds
secondDiff.value < 0 && nanoDiff.value > 0 -> secondDiff + 1.seconds
else -> secondDiff
}
}
internal fun millisecondsBetween(
startSeconds: LongSeconds,
startNanoseconds: IntNanoseconds,
endExclusiveSeconds: LongSeconds,
endExclusiveNanoseconds: IntNanoseconds
): LongMilliseconds {
return (endExclusiveSeconds - startSeconds).inMilliseconds +
(endExclusiveNanoseconds - startNanoseconds).inMilliseconds
}
internal fun microsecondsBetween(
startSeconds: LongSeconds,
startNanoseconds: IntNanoseconds,
endExclusiveSeconds: LongSeconds,
endExclusiveNanoseconds: IntNanoseconds
): LongMicroseconds {
return (endExclusiveSeconds - startSeconds).inMicroseconds +
(endExclusiveNanoseconds - startNanoseconds).inMicroseconds
}
internal fun nanosecondsBetween(
startSeconds: LongSeconds,
startNanoseconds: IntNanoseconds,
endExclusiveSeconds: LongSeconds,
endExclusiveNanoseconds: IntNanoseconds
): LongNanoseconds {
return (endExclusiveSeconds - startSeconds).inNanoseconds +
(endExclusiveNanoseconds - startNanoseconds)
}
internal inline fun Interval.buildIsoString(
maxElementSize: Int,
inclusive: Boolean,
appendFunction: StringBuilder.(T) -> StringBuilder
): String {
return if (isEmpty()) {
""
} else {
buildString(2 * maxElementSize + 1) {
if (hasBoundedStart()) {
appendFunction(start)
} else {
append("..")
}
append('/')
if (hasBoundedEnd()) {
appendFunction(if (inclusive) endInclusive else endExclusive)
} else {
append("..")
}
}
}
}
internal fun throwUnboundedIntervalException(): Nothing {
throw UnsupportedOperationException(
"An interval cannot be represented as a period or duration unless it is bounded"
)
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy