到底
This commit is contained in:
@@ -0,0 +1 @@
|
||||
/build
|
||||
@@ -0,0 +1,37 @@
|
||||
public final class io/github/boguszpawlowski/composecalendar/kotlinxDateTime/ConvertersKt {
|
||||
public static final fun toJavaYearMonth (Lio/github/boguszpawlowski/composecalendar/kotlinxDateTime/YearMonth;)Ljava/time/YearMonth;
|
||||
public static final fun toKotlinYearMonth (Ljava/time/YearMonth;)Lio/github/boguszpawlowski/composecalendar/kotlinxDateTime/YearMonth;
|
||||
}
|
||||
|
||||
public final class io/github/boguszpawlowski/composecalendar/kotlinxDateTime/DateTimeExtensionsKt {
|
||||
public static final fun getFirstDayOfWeek (Ljava/util/Locale;)Ljava/time/DayOfWeek;
|
||||
public static final fun now (Lkotlinx/datetime/LocalDate$Companion;)Lkotlinx/datetime/LocalDate;
|
||||
}
|
||||
|
||||
public final class io/github/boguszpawlowski/composecalendar/kotlinxDateTime/YearMonth {
|
||||
public static final field Companion Lio/github/boguszpawlowski/composecalendar/kotlinxDateTime/YearMonth$Companion;
|
||||
public synthetic fun <init> (ILjava/time/Month;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
|
||||
public final fun component1 ()I
|
||||
public final fun component2 ()Ljava/time/Month;
|
||||
public final fun copy (ILjava/time/Month;)Lio/github/boguszpawlowski/composecalendar/kotlinxDateTime/YearMonth;
|
||||
public static synthetic fun copy$default (Lio/github/boguszpawlowski/composecalendar/kotlinxDateTime/YearMonth;ILjava/time/Month;ILjava/lang/Object;)Lio/github/boguszpawlowski/composecalendar/kotlinxDateTime/YearMonth;
|
||||
public final fun dec ()Lio/github/boguszpawlowski/composecalendar/kotlinxDateTime/YearMonth;
|
||||
public fun equals (Ljava/lang/Object;)Z
|
||||
public final fun getMonth ()Ljava/time/Month;
|
||||
public final fun getYear ()I
|
||||
public fun hashCode ()I
|
||||
public final fun inc ()Lio/github/boguszpawlowski/composecalendar/kotlinxDateTime/YearMonth;
|
||||
public final fun minus (ILkotlinx/datetime/DateTimeUnit$MonthBased;)Lio/github/boguszpawlowski/composecalendar/kotlinxDateTime/YearMonth;
|
||||
public final fun minus (JLkotlinx/datetime/DateTimeUnit$MonthBased;)Lio/github/boguszpawlowski/composecalendar/kotlinxDateTime/YearMonth;
|
||||
public final fun plus (ILkotlinx/datetime/DateTimeUnit$MonthBased;)Lio/github/boguszpawlowski/composecalendar/kotlinxDateTime/YearMonth;
|
||||
public final fun plus (JLkotlinx/datetime/DateTimeUnit$MonthBased;)Lio/github/boguszpawlowski/composecalendar/kotlinxDateTime/YearMonth;
|
||||
public fun toString ()Ljava/lang/String;
|
||||
}
|
||||
|
||||
public final class io/github/boguszpawlowski/composecalendar/kotlinxDateTime/YearMonth$Companion {
|
||||
public final fun now ()Lio/github/boguszpawlowski/composecalendar/kotlinxDateTime/YearMonth;
|
||||
public final fun of (II)Lio/github/boguszpawlowski/composecalendar/kotlinxDateTime/YearMonth;
|
||||
public final fun of (ILjava/time/Month;)Lio/github/boguszpawlowski/composecalendar/kotlinxDateTime/YearMonth;
|
||||
public final fun parse (Ljava/lang/String;)Lio/github/boguszpawlowski/composecalendar/kotlinxDateTime/YearMonth;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
plugins {
|
||||
kotlin(Kotlin.JvmPluginId)
|
||||
id(MavenPublish.PluginId)
|
||||
}
|
||||
|
||||
kotlin {
|
||||
explicitApi()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
api(Kotlin.DateTime)
|
||||
|
||||
testImplementation(Kotest.Assertions)
|
||||
testImplementation(Kotest.RunnerJunit5)
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
POM_ARTIFACT_ID=kotlinx-datetime
|
||||
POM_DESCRIPTION=A set of utilities supporting usage of kotlin datetime instead of java.time.
|
||||
POM_PACKAGING=aar
|
||||
SONATYPE_HOST=S01
|
||||
RELEASE_SIGNING_ENABLED=true
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package io.github.boguszpawlowski.composecalendar.kotlinxDateTime
|
||||
|
||||
public fun YearMonth.toJavaYearMonth(): java.time.YearMonth = java.time.YearMonth.of(year, month)
|
||||
|
||||
public fun java.time.YearMonth.toKotlinYearMonth(): YearMonth = YearMonth.of(year, month)
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package io.github.boguszpawlowski.composecalendar.kotlinxDateTime
|
||||
|
||||
import kotlinx.datetime.Clock
|
||||
import kotlinx.datetime.DayOfWeek
|
||||
import kotlinx.datetime.LocalDate
|
||||
import kotlinx.datetime.TimeZone
|
||||
import kotlinx.datetime.toLocalDateTime
|
||||
import java.time.temporal.WeekFields
|
||||
import java.util.Locale
|
||||
|
||||
public fun LocalDate.Companion.now(): LocalDate =
|
||||
Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault()).date
|
||||
|
||||
public val Locale.firstDayOfWeek: DayOfWeek
|
||||
get() = WeekFields.of(this).firstDayOfWeek
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
package io.github.boguszpawlowski.composecalendar.kotlinxDateTime
|
||||
|
||||
import kotlinx.datetime.DateTimeUnit
|
||||
import kotlinx.datetime.LocalDate
|
||||
import kotlinx.datetime.Month
|
||||
import java.time.temporal.ChronoField.YEAR
|
||||
|
||||
/**
|
||||
* Kotlin implementation of `YearMonth` available in java.time, since `KotlinxDateTime` doesn't
|
||||
* include a substitute for it: https://github.com/Kotlin/kotlinx-datetime/issues/168
|
||||
*
|
||||
* For construction use factory methods 'of' provided in companion object
|
||||
*/
|
||||
@Suppress("DataClassPrivateConstructor")
|
||||
public data class YearMonth private constructor(
|
||||
val year: Int,
|
||||
val month: Month,
|
||||
) {
|
||||
|
||||
/**
|
||||
* Increment by one month
|
||||
*/
|
||||
public operator fun inc(): YearMonth = plus(1, DateTimeUnit.MONTH)
|
||||
|
||||
/**
|
||||
* Decrement by one month
|
||||
*/
|
||||
public operator fun dec(): YearMonth = minus(1, DateTimeUnit.MONTH)
|
||||
|
||||
/**
|
||||
* Add specified amount of months to current date
|
||||
*/
|
||||
public fun plus(value: Int, unit: DateTimeUnit.MonthBased): YearMonth =
|
||||
plus(value.toLong(), unit)
|
||||
|
||||
/**
|
||||
* Subtract specified amount of months from current date
|
||||
*/
|
||||
public fun minus(value: Int, unit: DateTimeUnit.MonthBased): YearMonth =
|
||||
plus(-value.toLong(), unit)
|
||||
|
||||
public fun minus(value: Long, unit: DateTimeUnit.MonthBased): YearMonth =
|
||||
plus(-value, unit)
|
||||
|
||||
public fun plus(value: Long, unit: DateTimeUnit.MonthBased): YearMonth {
|
||||
val monthsToAdd = value * unit.months
|
||||
if (monthsToAdd == 0L) {
|
||||
return this
|
||||
}
|
||||
val monthCount = year * 12L + (month.value - 1)
|
||||
val calcMonths = monthCount + monthsToAdd
|
||||
|
||||
val newYear = YEAR.checkValidIntValue(calcMonths.floorDiv(12))
|
||||
val newMonth = calcMonths.floorMod(12) + 1
|
||||
return of(newYear, newMonth.toInt())
|
||||
}
|
||||
|
||||
override fun toString(): String = toJavaYearMonth().toString()
|
||||
|
||||
public companion object {
|
||||
public fun of(year: Int, month: Int): YearMonth = YearMonth(year, Month.of(month))
|
||||
|
||||
public fun of(year: Int, month: Month): YearMonth = YearMonth(year, month)
|
||||
|
||||
public fun parse(value: String): YearMonth =
|
||||
java.time.YearMonth.parse(value).toKotlinYearMonth()
|
||||
|
||||
public fun now(): YearMonth {
|
||||
val today = LocalDate.now()
|
||||
|
||||
return of(today.year, today.month.value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun Long.floorMod(other: Long): Long = when (other) {
|
||||
0L -> this
|
||||
else -> this - floorDiv(other) * other
|
||||
}
|
||||
+128
@@ -0,0 +1,128 @@
|
||||
package io.github.boguszpawlowski.composecalendar.kotlinxDateTime
|
||||
|
||||
import io.kotest.assertions.throwables.shouldThrow
|
||||
import io.kotest.core.spec.style.ShouldSpec
|
||||
import io.kotest.data.Headers3
|
||||
import io.kotest.data.Row3
|
||||
import io.kotest.data.forAll
|
||||
import io.kotest.data.table
|
||||
import io.kotest.matchers.shouldBe
|
||||
import kotlinx.datetime.DateTimeUnit
|
||||
import java.time.DateTimeException
|
||||
|
||||
internal class YearMonthTest : ShouldSpec({
|
||||
|
||||
context("Year Month") {
|
||||
should("overflow a year if adding months") {
|
||||
val yearMonth = YearMonth.of(2002, 11)
|
||||
val expectedResult = YearMonth.of(2003, 1)
|
||||
|
||||
val result = yearMonth.plus(2, DateTimeUnit.MONTH)
|
||||
|
||||
result shouldBe expectedResult
|
||||
}
|
||||
should("add years if adding months") {
|
||||
val yearMonth = YearMonth.of(2002, 11)
|
||||
val expectedResult = YearMonth.of(2202, 11)
|
||||
|
||||
val result = yearMonth.plus(2, DateTimeUnit.CENTURY)
|
||||
|
||||
result shouldBe expectedResult
|
||||
}
|
||||
should("subtract years if subtracting months") {
|
||||
val yearMonth = YearMonth.of(2002, 1)
|
||||
val expectedResult = YearMonth.of(2001, 11)
|
||||
|
||||
val result = yearMonth.minus(2, DateTimeUnit.MONTH)
|
||||
|
||||
result shouldBe expectedResult
|
||||
}
|
||||
should("subtract year if subtracting months") {
|
||||
val yearMonth = YearMonth.of(2002, 11)
|
||||
val expectedResult = YearMonth.of(2000, 11)
|
||||
|
||||
val result = yearMonth.minus(2, DateTimeUnit.YEAR)
|
||||
|
||||
result shouldBe expectedResult
|
||||
}
|
||||
should("throw an error if months greater than 12") {
|
||||
shouldThrow<DateTimeException> {
|
||||
YearMonth.of(1222, 13)
|
||||
}
|
||||
}
|
||||
should("correctly add months") {
|
||||
forAll(
|
||||
table(
|
||||
Headers3("starting month", "months to add", "expected month"),
|
||||
Row3(1, 2, 3),
|
||||
Row3(12, 24, 12),
|
||||
Row3(11, 2, 1),
|
||||
Row3(1, 14, 3),
|
||||
Row3(6, 7, 1),
|
||||
Row3(10, 20, 6),
|
||||
)
|
||||
) { startingMonth: Int, monthsToAdd: Int, expectedMonth: Int ->
|
||||
val yearMonth = YearMonth.of(2000, startingMonth)
|
||||
|
||||
val result = yearMonth.plus(monthsToAdd, DateTimeUnit.MONTH).month.value
|
||||
|
||||
result shouldBe expectedMonth
|
||||
}
|
||||
}
|
||||
should("correctly add years") {
|
||||
forAll(
|
||||
table(
|
||||
Headers3("starting month", "months to add", "expected year"),
|
||||
Row3(1, 2, 2000),
|
||||
Row3(12, 24, 2002),
|
||||
Row3(11, 2, 2001),
|
||||
Row3(1, 14, 2001),
|
||||
Row3(6, 7, 2001),
|
||||
Row3(10, 20, 2002),
|
||||
)
|
||||
) { startingMonth: Int, monthsToAdd: Int, expectedYear: Int ->
|
||||
val yearMonth = YearMonth.of(2000, startingMonth)
|
||||
|
||||
val result = yearMonth.plus(monthsToAdd, DateTimeUnit.MONTH).year
|
||||
|
||||
result shouldBe expectedYear
|
||||
}
|
||||
}
|
||||
context("when decrementing") {
|
||||
should("subtract one month") {
|
||||
val yearMonth = YearMonth.of(2002, 11)
|
||||
val expectedResult = YearMonth.of(2002, 10)
|
||||
|
||||
val result = yearMonth.dec()
|
||||
|
||||
result shouldBe expectedResult
|
||||
}
|
||||
should("overflow the year when january") {
|
||||
val yearMonth = YearMonth.of(2002, 1)
|
||||
val expectedResult = YearMonth.of(2001, 12)
|
||||
|
||||
val result = yearMonth.dec()
|
||||
|
||||
result shouldBe expectedResult
|
||||
}
|
||||
}
|
||||
context("when incrementing") {
|
||||
should("add one month") {
|
||||
val yearMonth = YearMonth.of(2002, 11)
|
||||
val expectedResult = YearMonth.of(2002, 12)
|
||||
|
||||
val result = yearMonth.inc()
|
||||
|
||||
result shouldBe expectedResult
|
||||
}
|
||||
should("overflow the year when december") {
|
||||
val yearMonth = YearMonth.of(2002, 12)
|
||||
val expectedResult = YearMonth.of(2003, 1)
|
||||
|
||||
val result = yearMonth.inc()
|
||||
|
||||
result shouldBe expectedResult
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user