All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.io.islandtime.measures._Days.kt Maven / Gradle / Ivy

The newest version!
//
// This file is auto-generated by 'tools:code-generator'
//
@file:JvmMultifileClass
@file:JvmName("DaysKt")

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.DAYS_PER_WEEK
import io.islandtime.internal.HOURS_PER_DAY
import io.islandtime.internal.MICROSECONDS_PER_DAY
import io.islandtime.internal.MILLISECONDS_PER_DAY
import io.islandtime.internal.MINUTES_PER_DAY
import io.islandtime.internal.NANOSECONDS_PER_DAY
import io.islandtime.internal.SECONDS_PER_DAY
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.days as kotlinDays

/**
 * A number of days.
 */
inline class IntDays(
  /**
   * The underlying value.
   */
  val value: Int
) : Comparable {
  /**
   * The absolute value of this duration.
   * @throws ArithmeticException if overflow occurs
   */
  val absoluteValue: IntDays
    get() = if (value < 0) -this else this
  /**
   * Converts this duration to nanoseconds.
   * @throws ArithmeticException if overflow occurs
   */
  val inNanoseconds: LongNanoseconds
    get() = (value.toLong() timesExact NANOSECONDS_PER_DAY).nanoseconds

  /**
   * Converts this duration to nanoseconds without checking for overflow.
   */
  internal val inNanosecondsUnchecked: LongNanoseconds
    get() = (value.toLong() * NANOSECONDS_PER_DAY).nanoseconds

  /**
   * Converts this duration to microseconds.
   * @throws ArithmeticException if overflow occurs
   */
  val inMicroseconds: LongMicroseconds
    get() = (value.toLong() timesExact MICROSECONDS_PER_DAY).microseconds

  /**
   * Converts this duration to microseconds without checking for overflow.
   */
  internal val inMicrosecondsUnchecked: LongMicroseconds
    get() = (value.toLong() * MICROSECONDS_PER_DAY).microseconds

  /**
   * Converts this duration to milliseconds.
   */
  val inMilliseconds: LongMilliseconds
    get() = (value.toLong() * MILLISECONDS_PER_DAY).milliseconds

  /**
   * Converts this duration to seconds.
   * @throws ArithmeticException if overflow occurs
   */
  val inSeconds: IntSeconds
    get() = (value timesExact SECONDS_PER_DAY).seconds

  /**
   * Converts this duration to seconds without checking for overflow.
   */
  internal val inSecondsUnchecked: IntSeconds
    get() = (value * SECONDS_PER_DAY).seconds

  /**
   * Converts this duration to minutes.
   * @throws ArithmeticException if overflow occurs
   */
  val inMinutes: IntMinutes
    get() = (value timesExact MINUTES_PER_DAY).minutes

  /**
   * Converts this duration to minutes without checking for overflow.
   */
  internal val inMinutesUnchecked: IntMinutes
    get() = (value * MINUTES_PER_DAY).minutes

  /**
   * Converts this duration to hours.
   * @throws ArithmeticException if overflow occurs
   */
  val inHours: IntHours
    get() = (value timesExact HOURS_PER_DAY).hours

  /**
   * Converts this duration to hours without checking for overflow.
   */
  internal val inHoursUnchecked: IntHours
    get() = (value * HOURS_PER_DAY).hours

  /**
   * Converts this duration to the number of whole weeks.
   */
  val inWeeks: IntWeeks
    get() = (value / DAYS_PER_WEEK).weeks

  /**
   * 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: IntDays): Int = value.compareTo(other.value)

  /**
   * Converts this duration to an ISO-8601 time interval representation.
   */
  override fun toString(): String {
     return when (value) {
       0 -> "P0D"
       Int.MIN_VALUE -> "-P2147483648D"
       else -> buildString {
         if (value < 0) { append('-') }
         append("P")
         append(value.absoluteValue)
         append('D')
       }
     }
  }

  /**
   * Negates this duration.
   * @throws ArithmeticException if overflow occurs
   */
  operator fun unaryMinus() = IntDays(value.negateExact())

  /**
   * Negates this duration without checking for overflow.
   */
  internal fun negateUnchecked() = IntDays(-value)

  /**
   * Multiplies this duration by a scalar value.
   * @throws ArithmeticException if overflow occurs
   */
  operator fun times(scalar: Int) = IntDays(value timesExact scalar)

  /**
   * Multiplies this duration by a scalar value.
   * @throws ArithmeticException if overflow occurs
   */
  operator fun times(scalar: Long) = this.toLongDays() * scalar

  /**
   * Divides this duration by a scalar value.
   * @throws ArithmeticException if overflow occurs or the scalar is zero
   */
  operator fun div(scalar: Int): IntDays {
     return if (scalar == -1) {
       -this
     } else {
       IntDays(value / scalar)
     }
  }

  /**
   * Divides this duration by a scalar value.
   * @throws ArithmeticException if the scalar is zero
   */
  operator fun div(scalar: Long): LongDays = this.toLongDays() / scalar
  operator fun rem(scalar: Int) = IntDays(value % scalar)

  operator fun rem(scalar: Long) = this.toLongDays() % scalar

  operator fun plus(nanoseconds: IntNanoseconds) = this.inNanoseconds + nanoseconds

  operator fun minus(nanoseconds: IntNanoseconds) = this.inNanoseconds - nanoseconds

  operator fun plus(nanoseconds: LongNanoseconds) = this.toLongDays().inNanoseconds + nanoseconds

  operator fun minus(nanoseconds: LongNanoseconds) = this.toLongDays().inNanoseconds - nanoseconds

  operator fun plus(microseconds: IntMicroseconds) = this.inMicroseconds + microseconds

  operator fun minus(microseconds: IntMicroseconds) = this.inMicroseconds - microseconds

  operator fun plus(microseconds: LongMicroseconds) = this.toLongDays().inMicroseconds +
      microseconds

  operator fun minus(microseconds: LongMicroseconds) = this.toLongDays().inMicroseconds -
      microseconds

  operator fun plus(milliseconds: IntMilliseconds) = this.inMilliseconds + milliseconds

  operator fun minus(milliseconds: IntMilliseconds) = this.inMilliseconds - milliseconds

  operator fun plus(milliseconds: LongMilliseconds) = this.toLongDays().inMilliseconds +
      milliseconds

  operator fun minus(milliseconds: LongMilliseconds) = this.toLongDays().inMilliseconds -
      milliseconds

  operator fun plus(seconds: IntSeconds) = this.inSeconds + seconds

  operator fun minus(seconds: IntSeconds) = this.inSeconds - seconds

  operator fun plus(seconds: LongSeconds) = this.toLongDays().inSeconds + seconds

  operator fun minus(seconds: LongSeconds) = this.toLongDays().inSeconds - seconds

  operator fun plus(minutes: IntMinutes) = this.inMinutes + minutes

  operator fun minus(minutes: IntMinutes) = this.inMinutes - minutes

  operator fun plus(minutes: LongMinutes) = this.toLongDays().inMinutes + minutes

  operator fun minus(minutes: LongMinutes) = this.toLongDays().inMinutes - minutes

  operator fun plus(hours: IntHours) = this.inHours + hours

  operator fun minus(hours: IntHours) = this.inHours - hours

  operator fun plus(hours: LongHours) = this.toLongDays().inHours + hours

  operator fun minus(hours: LongHours) = this.toLongDays().inHours - hours

  operator fun plus(days: IntDays) = IntDays(value plusExact days.value)

  operator fun minus(days: IntDays) = IntDays(value minusExact days.value)

  operator fun plus(days: LongDays) = LongDays(value.toLong() plusExact days.value)

  operator fun minus(days: LongDays) = LongDays(value.toLong() minusExact days.value)

  operator fun plus(weeks: IntWeeks) = this + weeks.inDays

  operator fun minus(weeks: IntWeeks) = this - weeks.inDays

  operator fun plus(weeks: LongWeeks) = this.toLongDays() + weeks.inDays

  operator fun minus(weeks: LongWeeks) = this.toLongDays() - weeks.inDays

  inline fun  toComponents(action: (weeks: IntWeeks, days: IntDays) -> T): T {
    val weeks = (value / DAYS_PER_WEEK).weeks
    val days = (value % DAYS_PER_WEEK).days
    return action(weeks, days)
  }

  /**
   * Converts this duration to a [kotlin.time.Duration].
   */
  @ExperimentalTime
  fun toKotlinDuration(): KotlinDuration = value.kotlinDays

  /**
   * Converts this duration to [LongDays].
   */
  fun toLongDays() = LongDays(value.toLong())

  /**
   * Converts this duration to a `Long` value.
   */
  fun toLong() = value.toLong()

  companion object {
    /**
     * The smallest supported value.
     */
    val MIN: IntDays = IntDays(Int.MIN_VALUE)

    /**
     * The largest supported value.
     */
    val MAX: IntDays = IntDays(Int.MAX_VALUE)
  }
}

/**
 * Converts this value to a duration of days.
 */
val Int.days: IntDays
  get() = IntDays(this)

/**
 * Multiplies this value by a duration of days.
 * @throws ArithmeticException if overflow occurs
 */
operator fun Int.times(days: IntDays) = days * this

/**
 * Multiplies this value by a duration of days.
 * @throws ArithmeticException if overflow occurs
 */
operator fun Long.times(days: IntDays) = days * this

/**
 * A number of days.
 */
inline class LongDays(
  /**
   * The underlying value.
   */
  val value: Long
) : Comparable {
  /**
   * The absolute value of this duration.
   * @throws ArithmeticException if overflow occurs
   */
  val absoluteValue: LongDays
    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_DAY).nanoseconds

  /**
   * Converts this duration to nanoseconds without checking for overflow.
   */
  internal val inNanosecondsUnchecked: LongNanoseconds
    get() = (value * NANOSECONDS_PER_DAY).nanoseconds

  /**
   * Converts this duration to microseconds.
   * @throws ArithmeticException if overflow occurs
   */
  val inMicroseconds: LongMicroseconds
    get() = (value timesExact MICROSECONDS_PER_DAY).microseconds

  /**
   * Converts this duration to microseconds without checking for overflow.
   */
  internal val inMicrosecondsUnchecked: LongMicroseconds
    get() = (value * MICROSECONDS_PER_DAY).microseconds

  /**
   * Converts this duration to milliseconds.
   * @throws ArithmeticException if overflow occurs
   */
  val inMilliseconds: LongMilliseconds
    get() = (value timesExact MILLISECONDS_PER_DAY).milliseconds

  /**
   * Converts this duration to milliseconds without checking for overflow.
   */
  internal val inMillisecondsUnchecked: LongMilliseconds
    get() = (value * MILLISECONDS_PER_DAY).milliseconds

  /**
   * Converts this duration to seconds.
   * @throws ArithmeticException if overflow occurs
   */
  val inSeconds: LongSeconds
    get() = (value timesExact SECONDS_PER_DAY).seconds

  /**
   * Converts this duration to seconds without checking for overflow.
   */
  internal val inSecondsUnchecked: LongSeconds
    get() = (value * SECONDS_PER_DAY).seconds

  /**
   * Converts this duration to minutes.
   * @throws ArithmeticException if overflow occurs
   */
  val inMinutes: LongMinutes
    get() = (value timesExact MINUTES_PER_DAY).minutes

  /**
   * Converts this duration to minutes without checking for overflow.
   */
  internal val inMinutesUnchecked: LongMinutes
    get() = (value * MINUTES_PER_DAY).minutes

  /**
   * Converts this duration to hours.
   * @throws ArithmeticException if overflow occurs
   */
  val inHours: LongHours
    get() = (value timesExact HOURS_PER_DAY).hours

  /**
   * Converts this duration to hours without checking for overflow.
   */
  internal val inHoursUnchecked: LongHours
    get() = (value * HOURS_PER_DAY).hours

  /**
   * Converts this duration to the number of whole weeks.
   */
  val inWeeks: LongWeeks
    get() = (value / DAYS_PER_WEEK).weeks

  /**
   * 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: LongDays): Int = value.compareTo(other.value)

  /**
   * Converts this duration to an ISO-8601 time interval representation.
   */
  override fun toString(): String {
     return when (value) {
       0L -> "P0D"
       Long.MIN_VALUE -> "-P9223372036854775808D"
       else -> buildString {
         if (value < 0) { append('-') }
         append("P")
         append(value.absoluteValue)
         append('D')
       }
     }
  }

  /**
   * Negates this duration.
   * @throws ArithmeticException if overflow occurs
   */
  operator fun unaryMinus() = LongDays(value.negateExact())

  /**
   * Negates this duration without checking for overflow.
   */
  internal fun negateUnchecked() = LongDays(-value)

  /**
   * Multiplies this duration by a scalar value.
   * @throws ArithmeticException if overflow occurs
   */
  operator fun times(scalar: Int) = LongDays(value timesExact scalar)

  /**
   * Multiplies this duration by a scalar value.
   * @throws ArithmeticException if overflow occurs
   */
  operator fun times(scalar: Long) = LongDays(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): LongDays {
     return if (scalar == -1) {
       -this
     } else {
       LongDays(value / scalar)
     }
  }

  /**
   * Divides this duration by a scalar value.
   * @throws ArithmeticException if overflow occurs or the scalar is zero
   */
  operator fun div(scalar: Long): LongDays {
     return if (scalar == -1L) {
       -this
     } else {
       LongDays(value / scalar)
     }
  }

  operator fun rem(scalar: Int) = LongDays(value % scalar)

  operator fun rem(scalar: Long) = LongDays(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) = this.inMicroseconds + microseconds

  operator fun minus(microseconds: IntMicroseconds) = this.inMicroseconds - microseconds

  operator fun plus(microseconds: LongMicroseconds) = this.inMicroseconds + microseconds

  operator fun minus(microseconds: LongMicroseconds) = this.inMicroseconds - microseconds

  operator fun plus(milliseconds: IntMilliseconds) = this.inMilliseconds + milliseconds

  operator fun minus(milliseconds: IntMilliseconds) = this.inMilliseconds - milliseconds

  operator fun plus(milliseconds: LongMilliseconds) = this.inMilliseconds + milliseconds

  operator fun minus(milliseconds: LongMilliseconds) = this.inMilliseconds - milliseconds

  operator fun plus(seconds: IntSeconds) = this.inSeconds + seconds

  operator fun minus(seconds: IntSeconds) = this.inSeconds - seconds

  operator fun plus(seconds: LongSeconds) = this.inSeconds + seconds

  operator fun minus(seconds: LongSeconds) = this.inSeconds - seconds

  operator fun plus(minutes: IntMinutes) = this.inMinutes + minutes

  operator fun minus(minutes: IntMinutes) = this.inMinutes - minutes

  operator fun plus(minutes: LongMinutes) = this.inMinutes + minutes

  operator fun minus(minutes: LongMinutes) = this.inMinutes - minutes

  operator fun plus(hours: IntHours) = this.inHours + hours

  operator fun minus(hours: IntHours) = this.inHours - hours

  operator fun plus(hours: LongHours) = this.inHours + hours

  operator fun minus(hours: LongHours) = this.inHours - hours

  operator fun plus(days: IntDays) = LongDays(value plusExact days.value)

  operator fun minus(days: IntDays) = LongDays(value minusExact days.value)

  operator fun plus(days: LongDays) = LongDays(value plusExact days.value)

  operator fun minus(days: LongDays) = LongDays(value minusExact days.value)

  operator fun plus(weeks: IntWeeks) = this + weeks.inDays

  operator fun minus(weeks: IntWeeks) = this - weeks.inDays

  operator fun plus(weeks: LongWeeks) = this + weeks.inDays

  operator fun minus(weeks: LongWeeks) = this - weeks.inDays

  inline fun  toComponents(action: (weeks: LongWeeks, days: IntDays) -> T): T {
    val weeks = (value / DAYS_PER_WEEK).weeks
    val days = (value % DAYS_PER_WEEK).toInt().days
    return action(weeks, days)
  }

  /**
   * Converts this duration to a [kotlin.time.Duration].
   */
  @ExperimentalTime
  fun toKotlinDuration(): KotlinDuration = value.kotlinDays

  /**
   * Converts this duration to [IntDays].
   * @throws ArithmeticException if overflow occurs
   */
  fun toIntDays() = IntDays(value.toIntExact())

  /**
   * Converts this duration to [IntDays] without checking for overflow.
   */
  @PublishedApi
  internal fun toIntDaysUnchecked() = IntDays(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: LongDays = LongDays(Long.MIN_VALUE)

    /**
     * The largest supported value.
     */
    val MAX: LongDays = LongDays(Long.MAX_VALUE)
  }
}

/**
 * Converts this value to a duration of days.
 */
val Long.days: LongDays
  get() = LongDays(this)

/**
 * Multiplies this value by a duration of days.
 * @throws ArithmeticException if overflow occurs
 */
operator fun Int.times(days: LongDays) = days * this

/**
 * Multiplies this value by a duration of days.
 * @throws ArithmeticException if overflow occurs
 */
operator fun Long.times(days: LongDays) = days * this

/**
 * Converts this duration to Island Time [LongDays].
 */
@ExperimentalTime
fun KotlinDuration.toIslandDays() = LongDays(this.toLong(kotlin.time.DurationUnit.DAYS))




© 2015 - 2025 Weber Informatics LLC | Privacy Policy