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

commonMain.io.wojciechosak.calendar.utils.CalendarExt.kt Maven / Gradle / Ivy

Go to download

Kotlin Compose Multiplatform UI library that simplifies usage of calendar views

There is a newer version: 1.0.2
Show newest version
package io.wojciechosak.calendar.utils

import kotlinx.datetime.LocalDate
import kotlinx.datetime.LocalDateTime
import kotlinx.datetime.Month

fun isLeapYear(year: Int): Boolean {
    return try {
        LocalDate(year, 2, 29)
        true
    } catch (exception: IllegalArgumentException) {
        false
    }
}

fun monthLength(month: Month, year: Int): Int {
    val isLeapYear = isLeapYear(year)
    return when (month) {
        Month.FEBRUARY -> if (isLeapYear) 29 else 28
        Month.APRIL, Month.JUNE, Month.SEPTEMBER, Month.NOVEMBER -> 30
        else -> 31
    }
}

internal fun LocalDateTime.toLocalDate(): LocalDate {
    return LocalDate(year, month, dayOfMonth)
}

internal fun LocalDate.copy(
    year: Int = this.year,
    month: Month = this.month,
    day: Int = this.dayOfMonth
): LocalDate {
    return LocalDate(year, month, day)
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy