fix: Fixing date formats.
This commit is contained in:
parent
33cca86383
commit
29d33687b4
@ -9,38 +9,49 @@ import org.junit.Test
|
|||||||
|
|
||||||
class DatesTest {
|
class DatesTest {
|
||||||
|
|
||||||
private val v3Date = "2013-04-07T13:43:00+01:00"
|
private val newVersionDateVariant = "2022-12-24T17:00:08+00"
|
||||||
private val v4Date = "2013-04-07 13:43:00"
|
private val newVersionDate = "2013-04-07T13:43:00+01:00"
|
||||||
private val bug1Date = "2022-12-24T17:00:08+00"
|
private val oldVersionDate = "2013-05-07 13:46:00"
|
||||||
|
private val oldVersionDateVariant = "2021-03-21 10:32:00.000000"
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun v3_date_should_be_parsed() {
|
fun new_version_date_should_be_parsed() {
|
||||||
val date = DateUtils.parseDate(v3Date)
|
val date = DateUtils.parseDate(newVersionDate)
|
||||||
val expected =
|
|
||||||
LocalDateTime(2013, 4, 7, 14, 43, 0, 0).toInstant(TimeZone.currentSystemDefault())
|
|
||||||
.toEpochMilliseconds()
|
|
||||||
|
|
||||||
assertEquals(date, expected)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun v4_date_should_be_parsed() {
|
|
||||||
val date = DateUtils.parseDate(v4Date)
|
|
||||||
val expected =
|
val expected =
|
||||||
LocalDateTime(2013, 4, 7, 13, 43, 0, 0).toInstant(TimeZone.currentSystemDefault())
|
LocalDateTime(2013, 4, 7, 13, 43, 0, 0).toInstant(TimeZone.currentSystemDefault())
|
||||||
.toEpochMilliseconds()
|
.toEpochMilliseconds()
|
||||||
|
|
||||||
assertEquals(date, expected)
|
assertEquals(expected, date)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun bug1_date_should_be_parsed() {
|
fun old_version_date_should_be_parsed() {
|
||||||
val date = DateUtils.parseDate(bug1Date)
|
val date = DateUtils.parseDate(oldVersionDate)
|
||||||
val expected =
|
val expected =
|
||||||
LocalDateTime(2022, 12, 24, 18, 0, 8, 0).toInstant(TimeZone.currentSystemDefault())
|
LocalDateTime(2013, 5, 7, 13, 46, 0, 0).toInstant(TimeZone.currentSystemDefault())
|
||||||
.toEpochMilliseconds()
|
.toEpochMilliseconds()
|
||||||
|
|
||||||
assertEquals(date, expected)
|
assertEquals(expected, date)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun old_version_variant_date_should_be_parsed() {
|
||||||
|
val date = DateUtils.parseDate(oldVersionDateVariant)
|
||||||
|
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 = DateUtils.parseDate(newVersionDateVariant)
|
||||||
|
val expected =
|
||||||
|
LocalDateTime(2022, 12, 24, 17, 0, 8, 0).toInstant(TimeZone.currentSystemDefault())
|
||||||
|
.toEpochMilliseconds()
|
||||||
|
|
||||||
|
assertEquals(expected, date)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -6,16 +6,25 @@ import kotlinx.datetime.*
|
|||||||
|
|
||||||
actual class DateUtils {
|
actual class DateUtils {
|
||||||
actual companion object {
|
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()
|
||||||
|
|
||||||
|
// We may need to consider moving the formatting to platform specific code, even if the tests are doubled
|
||||||
|
// 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 {
|
||||||
return try {
|
var isoDateString: String = if (dateString.matches(oldVersionFormat)) {
|
||||||
Instant.parse(dateString).toEpochMilliseconds()
|
dateString.replace(" ", "T")
|
||||||
} catch (e: Exception) {
|
} else if (dateString.matches(newVersionFormat)) {
|
||||||
var str = dateString.replace(" ", "T")
|
dateString.split("+")[0]
|
||||||
if (str.matches("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\+\\d{2}".toRegex())) {
|
} else {
|
||||||
str = str.split("+")[0]
|
throw Exception("Unrecognized format for $dateString")
|
||||||
}
|
|
||||||
LocalDateTime.parse(str).toInstant(TimeZone.currentSystemDefault()).toEpochMilliseconds()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return LocalDateTime.parse(isoDateString).toInstant(TimeZone.currentSystemDefault()).toEpochMilliseconds()
|
||||||
}
|
}
|
||||||
|
|
||||||
actual fun parseRelativeDate(dateString: String): String {
|
actual fun parseRelativeDate(dateString: String): String {
|
||||||
|
Loading…
Reference in New Issue
Block a user