generated from boguszpawlowski/AndroidTemplate
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add module with a support for kotlinx-datetime (#61)
- Loading branch information
1 parent
3c56ff1
commit 0768d84
Showing
21 changed files
with
369 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import com.vanniktech.maven.publish.SonatypeHost | ||
|
||
plugins { | ||
kotlin(Kotlin.JvmPluginId) | ||
id(MavenPublish.PluginId) | ||
} | ||
|
||
kotlin { | ||
explicitApi() | ||
} | ||
|
||
dependencies { | ||
api(Kotlin.DateTime) | ||
implementation(Kotlin.StdLib) | ||
|
||
testImplementation(Kotest.Assertions) | ||
testImplementation(Kotest.RunnerJunit5) | ||
testImplementation(Kotlin.Reflect) | ||
} | ||
|
||
plugins.withId("com.vanniktech.maven.publish") { | ||
mavenPublish { | ||
sonatypeHost = SonatypeHost.S01 | ||
releaseSigningEnabled = true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
POM_ARTIFACT_ID=kotlinx-datetime | ||
POM_DESCRIPTION=A set of utilities supporting usage of kotlin datetime instead of java.time. | ||
POM_PACKAGING=aar |
5 changes: 5 additions & 0 deletions
5
...ime/src/main/java/io/github/boguszpawlowski/composecalendar/kotlinxDateTime/Converters.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 changes: 15 additions & 0 deletions
15
...main/java/io/github/boguszpawlowski/composecalendar/kotlinxDateTime/DateTimeExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
87 changes: 87 additions & 0 deletions
87
...time/src/main/java/io/github/boguszpawlowski/composecalendar/kotlinxDateTime/YearMonth.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package io.github.boguszpawlowski.composecalendar.kotlinxDateTime | ||
|
||
import kotlinx.datetime.DateTimeUnit | ||
import kotlinx.datetime.LocalDate | ||
import kotlinx.datetime.Month | ||
import java.time.Month.DECEMBER | ||
import java.time.Month.JANUARY | ||
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 = when (month.value) { | ||
in 1..11 -> YearMonth(year, month) | ||
else -> YearMonth(year + 1, JANUARY) | ||
} | ||
|
||
/** | ||
* Decrement by one month | ||
*/ | ||
public operator fun dec(): YearMonth = when (month.value) { | ||
in 2..12 -> YearMonth(year, month) | ||
else -> YearMonth(year - 1, DECEMBER) | ||
} | ||
|
||
/** | ||
* 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 | ||
} |
92 changes: 92 additions & 0 deletions
92
.../src/test/java/io/github/boguszpawlowski/composecalendar/kotlinxDateTime/YearMonthTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
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 | ||
} | ||
} | ||
} | ||
}) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,3 @@ | ||
POM_ARTIFACT_ID=composecalendar | ||
POM_NAME=Compose Calendar | ||
POM_PACKAGING=aar | ||
|
||
GROUP=io.github.boguszpawlowski.composecalendar | ||
POM_DESCRIPTION=Library for handling the Calendar view in Jetpack Compose. | ||
|
||
POM_URL=https://github.com/boguszpawlowski/ComposeCalendar | ||
POM_SCM_URL=https://github.com/boguszpawlowski/ComposeCalendar | ||
POM_SCM_CONNECTION=git@github.com:boguszpawlowski/ComposeCalendar.git | ||
POM_SCM_DEV_CONNECTION=git@github.com:boguszpawlowski/ComposeCalendar.git | ||
|
||
POM_LICENCE_NAME=The Apache Software License, Version 2.0 | ||
POM_LICENCE_URL=https://www.apache.org/licenses/LICENSE-2.0.txt | ||
|
||
POM_LICENCE_DIST=repo | ||
POM_DEVELOPER_ID=boguszp | ||
POM_DEVELOPER_NAME=Bogusz Pawlowski | ||
POM_PACKAGING=aar |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.