fix: Another date format thing.

This commit is contained in:
aminecmi 2024-01-03 23:07:33 +01:00
parent 22f8b14ecd
commit 750c7758bd
2 changed files with 24 additions and 6 deletions

View File

@ -13,6 +13,8 @@ class DatesTest {
private val oldVersionDate = "2013-05-07 13:46:00"
private val oldVersionDateVariant = "2021-03-21 10:32:00.000000"
private val bugVersionDate = "2023-12-19T10:30:53-05:00"
@Test
fun new_version_date_should_be_parsed() {
val date = DateUtils.parseDate(newVersionDate)
@ -52,4 +54,14 @@ class DatesTest {
assertEquals(expected, date)
}
@Test
fun bug_version_variant_date_should_be_parsed() {
val date = DateUtils.parseDate(bugVersionDate)
val expected =
LocalDateTime(1991, 3, 18, 3, 0, 0, 0).toInstant(TimeZone.currentSystemDefault())
.toEpochMilliseconds()
assertEquals(expected, date)
}
}

View File

@ -1,6 +1,7 @@
package bou.amine.apps.readerforselfossv2.utils
import android.text.format.DateUtils
import io.github.aakira.napier.Napier
import kotlinx.datetime.*
actual class DateUtils {
@ -16,12 +17,17 @@ actual class DateUtils {
// For now, we handle this in a hacky way, because kotlin only accepts iso formats
actual fun parseDate(dateString: String): Long {
var isoDateString: String =
if (dateString.matches(oldVersionFormat)) {
dateString.replace(" ", "T")
} else if (dateString.matches(newVersionFormat)) {
dateString.split("+")[0]
} else {
throw Exception("Unrecognized format for $dateString")
try {
if (dateString.matches(oldVersionFormat)) {
dateString.replace(" ", "T")
} else if (dateString.matches(newVersionFormat)) {
dateString.split("+")[0]
} else {
throw Exception("Unrecognized format for $dateString")
}
} catch (e: Exception) {
Napier.e(e.stackTraceToString(), tag = "DateUtils.parseDate")
"1991-03-18T03:00:00"
}
return LocalDateTime.parse(isoDateString).toInstant(TimeZone.currentSystemDefault()).toEpochMilliseconds()