commonMain.io.islandtime.measures._Microseconds.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!
//
// This file is auto-generated by 'tools:code-generator'
//
@file:JvmMultifileClass
@file:JvmName("MicrosecondsKt")
package io.islandtime.measures
import dev.erikchristensen.javamath2kmp.minusExact
import dev.erikchristensen.javamath2kmp.negateExact
import dev.erikchristensen.javamath2kmp.plusExact
import dev.erikchristensen.javamath2kmp.timesExact
import dev.erikchristensen.javamath2kmp.toIntExact
import io.islandtime.internal.MICROSECONDS_PER_DAY
import io.islandtime.internal.MICROSECONDS_PER_HOUR
import io.islandtime.internal.MICROSECONDS_PER_MILLISECOND
import io.islandtime.internal.MICROSECONDS_PER_MINUTE
import io.islandtime.internal.MICROSECONDS_PER_SECOND
import io.islandtime.internal.NANOSECONDS_PER_MICROSECOND
import io.islandtime.internal.toZeroPaddedString
import kotlin.Boolean
import kotlin.Comparable
import kotlin.Int
import kotlin.Long
import kotlin.PublishedApi
import kotlin.String
import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName
import kotlin.math.absoluteValue
import kotlin.time.ExperimentalTime
import kotlin.time.Duration as KotlinDuration
import kotlin.time.microseconds as kotlinMicroseconds
/**
* A number of microseconds.
*/
inline class IntMicroseconds(
/**
* The underlying value.
*/
val value: Int
) : Comparable {
/**
* The absolute value of this duration.
* @throws ArithmeticException if overflow occurs
*/
val absoluteValue: IntMicroseconds
get() = if (value < 0) -this else this
/**
* Converts this duration to nanoseconds.
*/
val inNanoseconds: LongNanoseconds
get() = (value.toLong() * NANOSECONDS_PER_MICROSECOND).nanoseconds
/**
* Converts this duration to the number of whole milliseconds.
*/
val inMilliseconds: IntMilliseconds
get() = (value / MICROSECONDS_PER_MILLISECOND).milliseconds
/**
* Converts this duration to the number of whole seconds.
*/
val inSeconds: IntSeconds
get() = (value / MICROSECONDS_PER_SECOND).seconds
/**
* Converts this duration to the number of whole minutes.
*/
val inMinutes: IntMinutes
get() = (value / MICROSECONDS_PER_MINUTE).minutes
/**
* Converts this duration to the number of whole hours.
*/
val inHours: IntHours
get() = (value / MICROSECONDS_PER_HOUR).toInt().hours
/**
* Converts this duration to the number of whole days.
*/
val inDays: IntDays
get() = (value / MICROSECONDS_PER_DAY).toInt().days
/**
* Checks if this duration is zero.
*/
fun isZero(): Boolean = value == 0
/**
* Checks if this duration is negative.
*/
fun isNegative(): Boolean = value < 0
/**
* Checks if this duration is positive.
*/
fun isPositive(): Boolean = value > 0
override fun compareTo(other: IntMicroseconds): Int = value.compareTo(other.value)
/**
* Converts this duration to an ISO-8601 time interval representation.
*/
override fun toString(): String {
return if (value == 0) {
"PT0S"
} else {
buildString {
val wholePart = (value / 1000000).absoluteValue
val fractionalPart = (value % 1000000).absoluteValue
if (value < 0) { append('-') }
append("PT")
append(wholePart)
if (fractionalPart > 0) {
append('.')
append(fractionalPart.toZeroPaddedString(6).dropLastWhile { it == '0' })
}
append('S')
}
}
}
/**
* Negates this duration.
* @throws ArithmeticException if overflow occurs
*/
operator fun unaryMinus() = IntMicroseconds(value.negateExact())
/**
* Negates this duration without checking for overflow.
*/
internal fun negateUnchecked() = IntMicroseconds(-value)
/**
* Multiplies this duration by a scalar value.
* @throws ArithmeticException if overflow occurs
*/
operator fun times(scalar: Int) = this.toLongMicroseconds() * scalar
/**
* Multiplies this duration by a scalar value.
* @throws ArithmeticException if overflow occurs
*/
operator fun times(scalar: Long) = this.toLongMicroseconds() * scalar
/**
* Divides this duration by a scalar value.
* @throws ArithmeticException if overflow occurs or the scalar is zero
*/
operator fun div(scalar: Int): IntMicroseconds {
return if (scalar == -1) {
-this
} else {
IntMicroseconds(value / scalar)
}
}
/**
* Divides this duration by a scalar value.
* @throws ArithmeticException if the scalar is zero
*/
operator fun div(scalar: Long): LongMicroseconds = this.toLongMicroseconds() / scalar
operator fun rem(scalar: Int) = IntMicroseconds(value % scalar)
operator fun rem(scalar: Long) = this.toLongMicroseconds() % scalar
operator fun plus(nanoseconds: IntNanoseconds) = this.toLongMicroseconds().inNanoseconds +
nanoseconds.toLongNanoseconds()
operator fun minus(nanoseconds: IntNanoseconds) = this.toLongMicroseconds().inNanoseconds -
nanoseconds.toLongNanoseconds()
operator fun plus(nanoseconds: LongNanoseconds) = this.toLongMicroseconds().inNanoseconds +
nanoseconds
operator fun minus(nanoseconds: LongNanoseconds) = this.toLongMicroseconds().inNanoseconds -
nanoseconds
operator fun plus(microseconds: IntMicroseconds) = LongMicroseconds(value.toLong() plusExact
microseconds.value)
operator fun minus(microseconds: IntMicroseconds) = LongMicroseconds(value.toLong() minusExact
microseconds.value)
operator fun plus(microseconds: LongMicroseconds) = LongMicroseconds(value.toLong() plusExact
microseconds.value)
operator fun minus(microseconds: LongMicroseconds) = LongMicroseconds(value.toLong() minusExact
microseconds.value)
operator fun plus(milliseconds: IntMilliseconds) = this.toLongMicroseconds() +
milliseconds.inMicroseconds
operator fun minus(milliseconds: IntMilliseconds) = this.toLongMicroseconds() -
milliseconds.inMicroseconds
operator fun plus(milliseconds: LongMilliseconds) = this.toLongMicroseconds() +
milliseconds.inMicroseconds
operator fun minus(milliseconds: LongMilliseconds) = this.toLongMicroseconds() -
milliseconds.inMicroseconds
operator fun plus(seconds: IntSeconds) = this.toLongMicroseconds() + seconds.inMicroseconds
operator fun minus(seconds: IntSeconds) = this.toLongMicroseconds() - seconds.inMicroseconds
operator fun plus(seconds: LongSeconds) = this.toLongMicroseconds() + seconds.inMicroseconds
operator fun minus(seconds: LongSeconds) = this.toLongMicroseconds() - seconds.inMicroseconds
operator fun plus(minutes: IntMinutes) = this.toLongMicroseconds() + minutes.inMicroseconds
operator fun minus(minutes: IntMinutes) = this.toLongMicroseconds() - minutes.inMicroseconds
operator fun plus(minutes: LongMinutes) = this.toLongMicroseconds() + minutes.inMicroseconds
operator fun minus(minutes: LongMinutes) = this.toLongMicroseconds() - minutes.inMicroseconds
operator fun plus(hours: IntHours) = this.toLongMicroseconds() + hours.inMicroseconds
operator fun minus(hours: IntHours) = this.toLongMicroseconds() - hours.inMicroseconds
operator fun plus(hours: LongHours) = this.toLongMicroseconds() + hours.inMicroseconds
operator fun minus(hours: LongHours) = this.toLongMicroseconds() - hours.inMicroseconds
operator fun plus(days: IntDays) = this.toLongMicroseconds() + days.inMicroseconds
operator fun minus(days: IntDays) = this.toLongMicroseconds() - days.inMicroseconds
operator fun plus(days: LongDays) = this.toLongMicroseconds() + days.inMicroseconds
operator fun minus(days: LongDays) = this.toLongMicroseconds() - days.inMicroseconds
inline fun toComponents(action: (milliseconds: IntMilliseconds,
microseconds: IntMicroseconds) -> T): T {
val milliseconds = (value / MICROSECONDS_PER_MILLISECOND).milliseconds
val microseconds = (value % MICROSECONDS_PER_MILLISECOND).microseconds
return action(milliseconds, microseconds)
}
inline fun toComponents(action: (
seconds: IntSeconds,
milliseconds: IntMilliseconds,
microseconds: IntMicroseconds
) -> T): T {
val seconds = (value / MICROSECONDS_PER_SECOND).seconds
val milliseconds = ((value % MICROSECONDS_PER_SECOND) /
MICROSECONDS_PER_MILLISECOND).milliseconds
val microseconds = (value % MICROSECONDS_PER_MILLISECOND).microseconds
return action(seconds, milliseconds, microseconds)
}
inline fun toComponents(action: (
minutes: IntMinutes,
seconds: IntSeconds,
milliseconds: IntMilliseconds,
microseconds: IntMicroseconds
) -> T): T {
val minutes = (value / MICROSECONDS_PER_MINUTE).minutes
val seconds = ((value % MICROSECONDS_PER_MINUTE) / MICROSECONDS_PER_SECOND).seconds
val milliseconds = ((value % MICROSECONDS_PER_SECOND) /
MICROSECONDS_PER_MILLISECOND).milliseconds
val microseconds = (value % MICROSECONDS_PER_MILLISECOND).microseconds
return action(minutes, seconds, milliseconds, microseconds)
}
inline fun toComponents(action: (
hours: IntHours,
minutes: IntMinutes,
seconds: IntSeconds,
milliseconds: IntMilliseconds,
microseconds: IntMicroseconds
) -> T): T {
val hours = (value / MICROSECONDS_PER_HOUR).toInt().hours
val minutes = ((value % MICROSECONDS_PER_HOUR) / MICROSECONDS_PER_MINUTE).toInt().minutes
val seconds = ((value % MICROSECONDS_PER_MINUTE) / MICROSECONDS_PER_SECOND).seconds
val milliseconds = ((value % MICROSECONDS_PER_SECOND) /
MICROSECONDS_PER_MILLISECOND).milliseconds
val microseconds = (value % MICROSECONDS_PER_MILLISECOND).microseconds
return action(hours, minutes, seconds, milliseconds, microseconds)
}
inline fun toComponents(action: (
days: IntDays,
hours: IntHours,
minutes: IntMinutes,
seconds: IntSeconds,
milliseconds: IntMilliseconds,
microseconds: IntMicroseconds
) -> T): T {
val days = (value / MICROSECONDS_PER_DAY).toInt().days
val hours = ((value % MICROSECONDS_PER_DAY) / MICROSECONDS_PER_HOUR).toInt().hours
val minutes = ((value % MICROSECONDS_PER_HOUR) / MICROSECONDS_PER_MINUTE).toInt().minutes
val seconds = ((value % MICROSECONDS_PER_MINUTE) / MICROSECONDS_PER_SECOND).seconds
val milliseconds = ((value % MICROSECONDS_PER_SECOND) /
MICROSECONDS_PER_MILLISECOND).milliseconds
val microseconds = (value % MICROSECONDS_PER_MILLISECOND).microseconds
return action(days, hours, minutes, seconds, milliseconds, microseconds)
}
/**
* Converts this duration to a [kotlin.time.Duration].
*/
@ExperimentalTime
fun toKotlinDuration(): KotlinDuration = value.kotlinMicroseconds
/**
* Converts this duration to [LongMicroseconds].
*/
fun toLongMicroseconds() = LongMicroseconds(value.toLong())
/**
* Converts this duration to a `Long` value.
*/
fun toLong() = value.toLong()
companion object {
/**
* The smallest supported value.
*/
val MIN: IntMicroseconds = IntMicroseconds(Int.MIN_VALUE)
/**
* The largest supported value.
*/
val MAX: IntMicroseconds = IntMicroseconds(Int.MAX_VALUE)
}
}
/**
* Converts this value to a duration of microseconds.
*/
val Int.microseconds: IntMicroseconds
get() = IntMicroseconds(this)
/**
* Multiplies this value by a duration of microseconds.
* @throws ArithmeticException if overflow occurs
*/
operator fun Int.times(microseconds: IntMicroseconds) = microseconds * this
/**
* Multiplies this value by a duration of microseconds.
* @throws ArithmeticException if overflow occurs
*/
operator fun Long.times(microseconds: IntMicroseconds) = microseconds * this
/**
* A number of microseconds.
*/
inline class LongMicroseconds(
/**
* The underlying value.
*/
val value: Long
) : Comparable {
/**
* The absolute value of this duration.
* @throws ArithmeticException if overflow occurs
*/
val absoluteValue: LongMicroseconds
get() = if (value < 0) -this else this
/**
* Converts this duration to nanoseconds.
* @throws ArithmeticException if overflow occurs
*/
val inNanoseconds: LongNanoseconds
get() = (value timesExact NANOSECONDS_PER_MICROSECOND).nanoseconds
/**
* Converts this duration to nanoseconds without checking for overflow.
*/
internal val inNanosecondsUnchecked: LongNanoseconds
get() = (value * NANOSECONDS_PER_MICROSECOND).nanoseconds
/**
* Converts this duration to the number of whole milliseconds.
*/
val inMilliseconds: LongMilliseconds
get() = (value / MICROSECONDS_PER_MILLISECOND).milliseconds
/**
* Converts this duration to the number of whole seconds.
*/
val inSeconds: LongSeconds
get() = (value / MICROSECONDS_PER_SECOND).seconds
/**
* Converts this duration to the number of whole minutes.
*/
val inMinutes: LongMinutes
get() = (value / MICROSECONDS_PER_MINUTE).minutes
/**
* Converts this duration to the number of whole hours.
*/
val inHours: LongHours
get() = (value / MICROSECONDS_PER_HOUR).hours
/**
* Converts this duration to the number of whole days.
*/
val inDays: LongDays
get() = (value / MICROSECONDS_PER_DAY).days
/**
* Checks if this duration is zero.
*/
fun isZero(): Boolean = value == 0L
/**
* Checks if this duration is negative.
*/
fun isNegative(): Boolean = value < 0L
/**
* Checks if this duration is positive.
*/
fun isPositive(): Boolean = value > 0L
override fun compareTo(other: LongMicroseconds): Int = value.compareTo(other.value)
/**
* Converts this duration to an ISO-8601 time interval representation.
*/
override fun toString(): String {
return if (value == 0L) {
"PT0S"
} else {
buildString {
val wholePart = (value / 1000000).absoluteValue
val fractionalPart = ((value % 1000000).toInt()).absoluteValue
if (value < 0) { append('-') }
append("PT")
append(wholePart)
if (fractionalPart > 0) {
append('.')
append(fractionalPart.toZeroPaddedString(6).dropLastWhile { it == '0' })
}
append('S')
}
}
}
/**
* Negates this duration.
* @throws ArithmeticException if overflow occurs
*/
operator fun unaryMinus() = LongMicroseconds(value.negateExact())
/**
* Negates this duration without checking for overflow.
*/
internal fun negateUnchecked() = LongMicroseconds(-value)
/**
* Multiplies this duration by a scalar value.
* @throws ArithmeticException if overflow occurs
*/
operator fun times(scalar: Int) = LongMicroseconds(value timesExact scalar)
/**
* Multiplies this duration by a scalar value.
* @throws ArithmeticException if overflow occurs
*/
operator fun times(scalar: Long) = LongMicroseconds(value timesExact scalar)
/**
* Divides this duration by a scalar value.
* @throws ArithmeticException if overflow occurs or the scalar is zero
*/
operator fun div(scalar: Int): LongMicroseconds {
return if (scalar == -1) {
-this
} else {
LongMicroseconds(value / scalar)
}
}
/**
* Divides this duration by a scalar value.
* @throws ArithmeticException if overflow occurs or the scalar is zero
*/
operator fun div(scalar: Long): LongMicroseconds {
return if (scalar == -1L) {
-this
} else {
LongMicroseconds(value / scalar)
}
}
operator fun rem(scalar: Int) = LongMicroseconds(value % scalar)
operator fun rem(scalar: Long) = LongMicroseconds(value % scalar)
operator fun plus(nanoseconds: IntNanoseconds) = this.inNanoseconds + nanoseconds
operator fun minus(nanoseconds: IntNanoseconds) = this.inNanoseconds - nanoseconds
operator fun plus(nanoseconds: LongNanoseconds) = this.inNanoseconds + nanoseconds
operator fun minus(nanoseconds: LongNanoseconds) = this.inNanoseconds - nanoseconds
operator fun plus(microseconds: IntMicroseconds) = LongMicroseconds(value plusExact
microseconds.value)
operator fun minus(microseconds: IntMicroseconds) = LongMicroseconds(value minusExact
microseconds.value)
operator fun plus(microseconds: LongMicroseconds) = LongMicroseconds(value plusExact
microseconds.value)
operator fun minus(microseconds: LongMicroseconds) = LongMicroseconds(value minusExact
microseconds.value)
operator fun plus(milliseconds: IntMilliseconds) = this + milliseconds.inMicroseconds
operator fun minus(milliseconds: IntMilliseconds) = this - milliseconds.inMicroseconds
operator fun plus(milliseconds: LongMilliseconds) = this + milliseconds.inMicroseconds
operator fun minus(milliseconds: LongMilliseconds) = this - milliseconds.inMicroseconds
operator fun plus(seconds: IntSeconds) = this + seconds.inMicroseconds
operator fun minus(seconds: IntSeconds) = this - seconds.inMicroseconds
operator fun plus(seconds: LongSeconds) = this + seconds.inMicroseconds
operator fun minus(seconds: LongSeconds) = this - seconds.inMicroseconds
operator fun plus(minutes: IntMinutes) = this + minutes.inMicroseconds
operator fun minus(minutes: IntMinutes) = this - minutes.inMicroseconds
operator fun plus(minutes: LongMinutes) = this + minutes.inMicroseconds
operator fun minus(minutes: LongMinutes) = this - minutes.inMicroseconds
operator fun plus(hours: IntHours) = this + hours.inMicroseconds
operator fun minus(hours: IntHours) = this - hours.inMicroseconds
operator fun plus(hours: LongHours) = this + hours.inMicroseconds
operator fun minus(hours: LongHours) = this - hours.inMicroseconds
operator fun plus(days: IntDays) = this + days.inMicroseconds
operator fun minus(days: IntDays) = this - days.inMicroseconds
operator fun plus(days: LongDays) = this + days.inMicroseconds
operator fun minus(days: LongDays) = this - days.inMicroseconds
inline fun toComponents(action: (milliseconds: LongMilliseconds,
microseconds: IntMicroseconds) -> T): T {
val milliseconds = (value / MICROSECONDS_PER_MILLISECOND).milliseconds
val microseconds = (value % MICROSECONDS_PER_MILLISECOND).toInt().microseconds
return action(milliseconds, microseconds)
}
inline fun toComponents(action: (
seconds: LongSeconds,
milliseconds: IntMilliseconds,
microseconds: IntMicroseconds
) -> T): T {
val seconds = (value / MICROSECONDS_PER_SECOND).seconds
val milliseconds = ((value % MICROSECONDS_PER_SECOND) /
MICROSECONDS_PER_MILLISECOND).toInt().milliseconds
val microseconds = (value % MICROSECONDS_PER_MILLISECOND).toInt().microseconds
return action(seconds, milliseconds, microseconds)
}
inline fun toComponents(action: (
minutes: LongMinutes,
seconds: IntSeconds,
milliseconds: IntMilliseconds,
microseconds: IntMicroseconds
) -> T): T {
val minutes = (value / MICROSECONDS_PER_MINUTE).minutes
val seconds = ((value % MICROSECONDS_PER_MINUTE) / MICROSECONDS_PER_SECOND).toInt().seconds
val milliseconds = ((value % MICROSECONDS_PER_SECOND) /
MICROSECONDS_PER_MILLISECOND).toInt().milliseconds
val microseconds = (value % MICROSECONDS_PER_MILLISECOND).toInt().microseconds
return action(minutes, seconds, milliseconds, microseconds)
}
inline fun toComponents(action: (
hours: LongHours,
minutes: IntMinutes,
seconds: IntSeconds,
milliseconds: IntMilliseconds,
microseconds: IntMicroseconds
) -> T): T {
val hours = (value / MICROSECONDS_PER_HOUR).hours
val minutes = ((value % MICROSECONDS_PER_HOUR) / MICROSECONDS_PER_MINUTE).toInt().minutes
val seconds = ((value % MICROSECONDS_PER_MINUTE) / MICROSECONDS_PER_SECOND).toInt().seconds
val milliseconds = ((value % MICROSECONDS_PER_SECOND) /
MICROSECONDS_PER_MILLISECOND).toInt().milliseconds
val microseconds = (value % MICROSECONDS_PER_MILLISECOND).toInt().microseconds
return action(hours, minutes, seconds, milliseconds, microseconds)
}
inline fun toComponents(action: (
days: LongDays,
hours: IntHours,
minutes: IntMinutes,
seconds: IntSeconds,
milliseconds: IntMilliseconds,
microseconds: IntMicroseconds
) -> T): T {
val days = (value / MICROSECONDS_PER_DAY).days
val hours = ((value % MICROSECONDS_PER_DAY) / MICROSECONDS_PER_HOUR).toInt().hours
val minutes = ((value % MICROSECONDS_PER_HOUR) / MICROSECONDS_PER_MINUTE).toInt().minutes
val seconds = ((value % MICROSECONDS_PER_MINUTE) / MICROSECONDS_PER_SECOND).toInt().seconds
val milliseconds = ((value % MICROSECONDS_PER_SECOND) /
MICROSECONDS_PER_MILLISECOND).toInt().milliseconds
val microseconds = (value % MICROSECONDS_PER_MILLISECOND).toInt().microseconds
return action(days, hours, minutes, seconds, milliseconds, microseconds)
}
/**
* Converts this duration to a [kotlin.time.Duration].
*/
@ExperimentalTime
fun toKotlinDuration(): KotlinDuration = value.kotlinMicroseconds
/**
* Converts this duration to [IntMicroseconds].
* @throws ArithmeticException if overflow occurs
*/
fun toIntMicroseconds() = IntMicroseconds(value.toIntExact())
/**
* Converts this duration to [IntMicroseconds] without checking for overflow.
*/
@PublishedApi
internal fun toIntMicrosecondsUnchecked() = IntMicroseconds(value.toInt())
/**
* Converts this duration to an `Int` value.
* @throws ArithmeticException if overflow occurs
*/
fun toInt() = value.toIntExact()
/**
* Converts this duration to an `Int` value without checking for overflow.
*/
internal fun toIntUnchecked() = value.toInt()
companion object {
/**
* The smallest supported value.
*/
val MIN: LongMicroseconds = LongMicroseconds(Long.MIN_VALUE)
/**
* The largest supported value.
*/
val MAX: LongMicroseconds = LongMicroseconds(Long.MAX_VALUE)
}
}
/**
* Converts this value to a duration of microseconds.
*/
val Long.microseconds: LongMicroseconds
get() = LongMicroseconds(this)
/**
* Multiplies this value by a duration of microseconds.
* @throws ArithmeticException if overflow occurs
*/
operator fun Int.times(microseconds: LongMicroseconds) = microseconds * this
/**
* Multiplies this value by a duration of microseconds.
* @throws ArithmeticException if overflow occurs
*/
operator fun Long.times(microseconds: LongMicroseconds) = microseconds * this
/**
* Converts this duration to Island Time [LongMicroseconds].
*/
@ExperimentalTime
fun KotlinDuration.toIslandMicroseconds() =
LongMicroseconds(this.toLong(kotlin.time.DurationUnit.MICROSECONDS))
© 2015 - 2025 Weber Informatics LLC | Privacy Policy