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

commonMain.io.github.lyxnx.util.time.DateTime.kt Maven / Gradle / Ivy

There is a newer version: 1.6.1
Show newest version
package io.github.lyxnx.util.time

import kotlinx.datetime.Clock
import kotlinx.datetime.LocalDate
import kotlinx.datetime.LocalDateTime
import kotlinx.datetime.LocalTime
import kotlinx.datetime.TimeZone
import kotlinx.datetime.offsetAt
import kotlinx.datetime.toLocalDateTime
import kotlin.jvm.JvmSynthetic
import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds

/**
 * Convenience function to get the current date and time in the specified [timezone]
 */
@JvmSynthetic
public fun LocalDateTime.Companion.now(timezone: TimeZone = TimeZone.currentSystemDefault()): LocalDateTime {
    return Clock.System.now().toLocalDateTime(timezone)
}

/**
 * Convenience function to get the current date without time component in the specified [timezone]
 */
@JvmSynthetic
public fun LocalDate.Companion.now(timezone: TimeZone = TimeZone.currentSystemDefault()): LocalDate {
    return LocalDateTime.now(timezone).date
}

/**
 * Convenience function to get the current time without date component in the specified [timezone]
 */
@JvmSynthetic
public fun LocalTime.Companion.now(timezone: TimeZone = TimeZone.currentSystemDefault()): LocalTime {
    return LocalDateTime.now(timezone).time
}

/**
 * Calculate the offset difference as a duration between this timezone and [other] given the current time
 */
@JvmSynthetic
public fun TimeZone.calculateOffsetDifference(other: TimeZone = TimeZone.currentSystemDefault()): Duration {
    val now = Clock.System.now()
    return (offsetAt(now).totalSeconds - other.offsetAt(now).totalSeconds).seconds
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy