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

commonMain.com.copperleaf.ballast.debugger.utils.utils.kt Maven / Gradle / Ivy

There is a newer version: 3.0.2
Show newest version
package com.copperleaf.ballast.debugger.utils

import kotlinx.datetime.Clock
import kotlinx.datetime.LocalDateTime
import kotlinx.datetime.TimeZone
import kotlinx.datetime.toInstant
import kotlinx.datetime.toLocalDateTime
import kotlin.time.Duration
import kotlin.time.DurationUnit
import kotlin.time.ExperimentalTime
import kotlin.time.toDuration

public fun LocalDateTime.Companion.now(): LocalDateTime {
    return Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault())
}

@ExperimentalTime
public operator fun LocalDateTime.minus(other: LocalDateTime): Duration {
    return this.toInstant(TimeZone.currentSystemDefault()) - other.toInstant(TimeZone.currentSystemDefault())
}

@ExperimentalTime
public fun Duration.removeFraction(minUnit: DurationUnit): Duration {
    for (unit in DurationUnit.values().reversed()) {
        val wholeNumberInUnit = this.toLong(unit)

        if (wholeNumberInUnit > 0) return wholeNumberInUnit.toDuration(unit)
        if (unit == minUnit) break
    }

    return this.toLong(minUnit).toDuration(minUnit)
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy