chore: update and use multiplatform datetime
This commit is contained in:
@ -52,6 +52,9 @@ kotlin {
|
||||
|
||||
// Sql
|
||||
implementation(SqlDelight.runtime)
|
||||
|
||||
// Sql
|
||||
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.6.1")
|
||||
}
|
||||
}
|
||||
val commonTest by getting {
|
||||
@ -80,10 +83,6 @@ kotlin {
|
||||
val iosArm64Main by getting
|
||||
// val iosSimulatorArm64Main by getting
|
||||
val iosMain by creating {
|
||||
dependsOn(commonMain)
|
||||
iosX64Main.dependsOn(this)
|
||||
iosArm64Main.dependsOn(this)
|
||||
// iosSimulatorArm64Main.dependsOn(this)
|
||||
|
||||
dependencies {
|
||||
implementation(SqlDelight.native)
|
||||
@ -94,10 +93,6 @@ kotlin {
|
||||
val iosArm64Test by getting
|
||||
// val iosSimulatorArm64Test by getting
|
||||
val iosTest by creating {
|
||||
dependsOn(commonTest)
|
||||
iosX64Test.dependsOn(this)
|
||||
iosArm64Test.dependsOn(this)
|
||||
// iosSimulatorArm64Test.dependsOn(this)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,33 +6,8 @@ import kotlinx.datetime.*
|
||||
|
||||
actual class DateUtils {
|
||||
actual companion object {
|
||||
// Possible formats are
|
||||
// yyyy-mm-dd hh:mm:ss format
|
||||
private val oldVersionFormat = "\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}(.()\\d*)?".toRegex()
|
||||
|
||||
// yyyy-MM-dd'T'HH:mm:ss[.SSS]XXX (RFC3339)
|
||||
private val newVersionFormat = "(\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2})[+-](\\d{2}(:\\d{2})?)?".toRegex()
|
||||
|
||||
// TODO: do not fix any more issues here. Move everything to plateform specific code.
|
||||
actual fun parseDate(dateString: String): Long {
|
||||
var isoDateString: String =
|
||||
try {
|
||||
if (dateString.matches(oldVersionFormat)) {
|
||||
dateString.replace(" ", "T")
|
||||
} else if (dateString.matches(newVersionFormat)) {
|
||||
newVersionFormat.find(dateString)?.groups?.get(1)?.value ?: throw Exception("Couldn't parse $dateString")
|
||||
} else {
|
||||
throw Exception("Unrecognized format for $dateString")
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
throw Exception("parseDate failed for $dateString", e)
|
||||
}
|
||||
|
||||
return LocalDateTime.parse(isoDateString).toInstant(TimeZone.currentSystemDefault()).toEpochMilliseconds()
|
||||
}
|
||||
|
||||
actual fun parseRelativeDate(dateString: String): String {
|
||||
val date = parseDate(dateString)
|
||||
val date = dateString.toParsedDate()
|
||||
|
||||
return " " +
|
||||
DateUtils.getRelativeTimeSpanString(
|
||||
|
@ -74,7 +74,7 @@ class Repository(
|
||||
dbItems = dbItems.filter { it.sourcetitle == sourceFilter.value!!.title }
|
||||
}
|
||||
val itemsList = ArrayList(dbItems.map { it.toView() })
|
||||
itemsList.sortByDescending { DateUtils.parseDate(it.datetime) }
|
||||
itemsList.sortByDescending { it.datetime.toParsedDate() }
|
||||
fetchedItems =
|
||||
StatusAndData.succes(
|
||||
itemsList,
|
||||
|
@ -1,9 +1,35 @@
|
||||
package bou.amine.apps.readerforselfossv2.utils
|
||||
|
||||
import kotlinx.datetime.LocalDateTime
|
||||
import kotlinx.datetime.TimeZone
|
||||
import kotlinx.datetime.toInstant
|
||||
|
||||
fun String.toParsedDate(): Long {
|
||||
// Possible formats are
|
||||
// yyyy-mm-dd hh:mm:ss format
|
||||
val oldVersionFormat = "\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}(.()\\d*)?".toRegex()
|
||||
|
||||
// yyyy-MM-dd'T'HH:mm:ss[.SSS]XXX (RFC3339)
|
||||
val newVersionFormat = "(\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2})[+-](\\d{2}(:\\d{2})?)?".toRegex()
|
||||
|
||||
val isoDateString: String =
|
||||
try {
|
||||
if (this.matches(oldVersionFormat)) {
|
||||
this.replace(" ", "T")
|
||||
} else if (this.matches(newVersionFormat)) {
|
||||
newVersionFormat.find(this)?.groups?.get(1)?.value ?: throw Exception("Couldn't parse $this")
|
||||
} else {
|
||||
throw Exception("Unrecognized format for $this")
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
throw Exception("parseDate failed for $this", e)
|
||||
}
|
||||
|
||||
return LocalDateTime.parse(isoDateString).toInstant(TimeZone.currentSystemDefault()).toEpochMilliseconds()
|
||||
}
|
||||
|
||||
expect class DateUtils() {
|
||||
companion object {
|
||||
fun parseDate(dateString: String): Long
|
||||
|
||||
fun parseRelativeDate(dateString: String): String
|
||||
}
|
||||
}
|
||||
|
66
shared/src/commonTest/kotlin/DatesTest.kt
Normal file
66
shared/src/commonTest/kotlin/DatesTest.kt
Normal file
@ -0,0 +1,66 @@
|
||||
package bou.amine.apps.readerforselfossv2.repository
|
||||
|
||||
import bou.amine.apps.readerforselfossv2.utils.toParsedDate
|
||||
import kotlinx.datetime.LocalDateTime
|
||||
import kotlinx.datetime.TimeZone
|
||||
import kotlinx.datetime.toInstant
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class DatesTest {
|
||||
private val newVersionDateVariant = "2022-12-24T17:00:08+00"
|
||||
private val newVersionDate = "2013-04-07T13:43:00+01:00"
|
||||
private val newVersionDate2 = "2013-04-07T13:43:00-01:00"
|
||||
private val oldVersionDate = "2013-05-07 13:46:00"
|
||||
private val oldVersionDateVariant = "2021-03-21 10:32:00.000000"
|
||||
|
||||
|
||||
@Test
|
||||
fun new_version_date_should_be_parsed() {
|
||||
val date = newVersionDate.toParsedDate()
|
||||
val expected =
|
||||
LocalDateTime(2013, 4, 7, 13, 43, 0, 0).toInstant(TimeZone.currentSystemDefault())
|
||||
.toEpochMilliseconds()
|
||||
|
||||
assertEquals(expected, date)
|
||||
}
|
||||
@Test
|
||||
fun new_version_date2_should_be_parsed() {
|
||||
val date = newVersionDate2.toParsedDate()
|
||||
val expected =
|
||||
LocalDateTime(2013, 4, 7, 13, 43, 0, 0).toInstant(TimeZone.currentSystemDefault())
|
||||
.toEpochMilliseconds()
|
||||
|
||||
assertEquals(expected, date)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun old_version_date_should_be_parsed() {
|
||||
val date = oldVersionDate.toParsedDate()
|
||||
val expected =
|
||||
LocalDateTime(2013, 5, 7, 13, 46, 0, 0).toInstant(TimeZone.currentSystemDefault())
|
||||
.toEpochMilliseconds()
|
||||
|
||||
assertEquals(expected, date)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun old_version_variant_date_should_be_parsed() {
|
||||
val date = oldVersionDateVariant.toParsedDate()
|
||||
val expected =
|
||||
LocalDateTime(2021, 3, 21, 10, 32, 0, 0).toInstant(TimeZone.currentSystemDefault())
|
||||
.toEpochMilliseconds()
|
||||
|
||||
assertEquals(expected, date)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun new_version_variant_date_should_be_parsed() {
|
||||
val date = newVersionDateVariant.toParsedDate()
|
||||
val expected =
|
||||
LocalDateTime(2022, 12, 24, 17, 0, 8, 0).toInstant(TimeZone.currentSystemDefault())
|
||||
.toEpochMilliseconds()
|
||||
|
||||
assertEquals(expected, date)
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package bou.amine.apps.readerforselfossv2.utils
|
||||
|
||||
actual class DateUtils actual constructor() {
|
||||
actual companion object {
|
||||
actual fun parseRelativeDate(dateString: String): String {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user