fix: Another date format thing.
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/promote/production Build is passing

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 oldVersionDate = "2013-05-07 13:46:00"
private val oldVersionDateVariant = "2021-03-21 10:32:00.000000" private val oldVersionDateVariant = "2021-03-21 10:32:00.000000"
private val bugVersionDate = "2023-12-19T10:30:53-05:00"
@Test @Test
fun new_version_date_should_be_parsed() { fun new_version_date_should_be_parsed() {
val date = DateUtils.parseDate(newVersionDate) val date = DateUtils.parseDate(newVersionDate)
@ -52,4 +54,14 @@ class DatesTest {
assertEquals(expected, date) 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 package bou.amine.apps.readerforselfossv2.utils
import android.text.format.DateUtils import android.text.format.DateUtils
import io.github.aakira.napier.Napier
import kotlinx.datetime.* import kotlinx.datetime.*
actual class DateUtils { 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 // For now, we handle this in a hacky way, because kotlin only accepts iso formats
actual fun parseDate(dateString: String): Long { actual fun parseDate(dateString: String): Long {
var isoDateString: String = var isoDateString: String =
if (dateString.matches(oldVersionFormat)) { try {
dateString.replace(" ", "T") if (dateString.matches(oldVersionFormat)) {
} else if (dateString.matches(newVersionFormat)) { dateString.replace(" ", "T")
dateString.split("+")[0] } else if (dateString.matches(newVersionFormat)) {
} else { dateString.split("+")[0]
throw Exception("Unrecognized format for $dateString") } 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() return LocalDateTime.parse(isoDateString).toInstant(TimeZone.currentSystemDefault()).toEpochMilliseconds()