KT-58058 Duration truncation internal helper function

This commit is contained in:
Ilya Gorbunov
2023-04-09 14:28:57 +02:00
committed by Space Team
parent b3decace6b
commit 17d977977c
2 changed files with 55 additions and 0 deletions
@@ -628,6 +628,17 @@ public value class Duration internal constructor(private val rawValue: Long) : C
return this.toDouble(coarserUnit) / other.toDouble(coarserUnit)
}
/**
* Returns a duration whose value is this duration value truncated to the specified duration [unit].
*/
internal fun truncateTo(unit: DurationUnit): Duration {
val storageUnit = storageUnit
if (unit <= storageUnit || this.isInfinite()) return this
val scale = convertDurationUnit(1, unit, storageUnit)
val result = value - value % scale
return result.toDuration(storageUnit)
}
/** Returns true, if the duration value is less than zero. */
public fun isNegative(): Boolean = rawValue < 0