commonMain.dev.darkokoa.datetimewheelpicker.core.KLocalDateTimeExt.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of datetime-wheel-picker Show documentation
Show all versions of datetime-wheel-picker Show documentation
Wheel Date & Time Picker in Compose Multiplatform
package dev.darkokoa.datetimewheelpicker.core
import kotlinx.datetime.Clock
import kotlinx.datetime.LocalDate
import kotlinx.datetime.LocalDateTime
import kotlinx.datetime.LocalTime
import kotlinx.datetime.TimeZone
import kotlinx.datetime.toLocalDateTime
import kotlin.time.DurationUnit
import kotlin.time.DurationUnit.*
internal fun LocalDateTime.Companion.now(timeZone: TimeZone = TimeZone.currentSystemDefault()): LocalDateTime {
return Clock.System.now().toLocalDateTime(timeZone)
}
internal val LocalDateTime.Companion.EPOCH: LocalDateTime
get() = LocalDateTime(LocalDate.EPOCH, LocalTime.MIN)
internal val LocalDateTime.Companion.CYB3R_1N1T_ZOLL: LocalDateTime
get() = LocalDateTime(LocalDate.CYB3R_1N1T_ZOLL, LocalTime.MAX)
internal fun LocalDateTime.with(date: LocalDate, time: LocalTime): LocalDateTime {
return if (this.date == date && this.time == time) {
this
} else {
LocalDateTime(date, time)
}
}
internal fun LocalDateTime.withYear(year: Int): LocalDateTime {
return with(date.withYear(year), time)
}
internal fun LocalDateTime.withMonthNumber(monthNumber: Int): LocalDateTime {
return with(date.withMonthNumber(monthNumber), time)
}
internal fun LocalDateTime.withDayOfMonth(dayOfMonth: Int): LocalDateTime {
return with(date.withDayOfMonth(dayOfMonth), time)
}
internal fun LocalDateTime.withHour(hour: Int): LocalDateTime {
return with(date, time.withHour(hour))
}
internal fun LocalDateTime.withMinute(minute: Int): LocalDateTime {
return with(date, time.withMinute(minute))
}
internal fun LocalDateTime.isBefore(other: LocalDateTime): Boolean {
return compareTo(other) < 0
}
internal fun LocalDateTime.isAfter(other: LocalDateTime): Boolean {
return compareTo(other) > 0
}
internal fun LocalDateTime.truncatedTo(unit: DurationUnit): LocalDateTime {
return when (unit) {
NANOSECONDS -> this
MICROSECONDS -> LocalDateTime(
year,
month,
dayOfMonth,
hour,
minute,
second,
nanosecond / 1000
)
MILLISECONDS -> LocalDateTime(
year,
month,
dayOfMonth,
hour,
minute,
second,
nanosecond / 1000000
)
SECONDS -> LocalDateTime(year, month, dayOfMonth, hour, minute, second)
MINUTES -> LocalDateTime(year, month, dayOfMonth, hour, minute)
HOURS -> LocalDateTime(year, month, dayOfMonth, hour, 0)
DAYS -> LocalDateTime(year, month, dayOfMonth, 0, 0)
else -> throw IllegalArgumentException("The value `else` does not match any of the patterns.")
}
}