Skip to content
This repository has been archived by the owner on Jul 8, 2022. It is now read-only.

Commit

Permalink
Parse ISO8601 format by default. Fixes #123 (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
RezMike authored Jul 1, 2020
1 parent 53790af commit 84eca60
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 2 additions & 1 deletion klock/src/commonMain/kotlin/com/soywiz/klock/DateFormat.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ interface DateFormat {
companion object {
val DEFAULT_FORMAT = DateFormat("EEE, dd MMM yyyy HH:mm:ss z")
val FORMAT1 = DateFormat("yyyy-MM-dd'T'HH:mm:ssXXX")
val FORMAT2 = DateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
val FORMAT_DATE = DateFormat("yyyy-MM-dd")

val FORMATS = listOf(DEFAULT_FORMAT, FORMAT1)
val FORMATS = listOf(DEFAULT_FORMAT, FORMAT1, FORMAT2, FORMAT_DATE)

fun parse(date: String): DateTimeTz {
var lastError: Throwable? = null
Expand Down
8 changes: 8 additions & 0 deletions klock/src/commonTest/kotlin/com/soywiz/klock/DateTimeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -410,4 +410,12 @@ class DateTimeTest {
assertEquals("Sat, 11 Aug -0027 08:00:00 UTC", DateTime.fromUnix(-63000000000000L).toString())
assertEquals("Sun, 31 Dec 0000 23:59:59 UTC", DateTime.fromUnix(-62135596800000L - 1L).toString())
}

@Test
fun testBug123() {
val str1 = "1989-01-01T10:00:00Z"
val str2 = "1989-01-01T10:00:00.000Z"
assertEquals(str1, DateTime.parse(str1).format(DateFormat.FORMAT1))
assertEquals(str2, DateTime.parse(str2).format(DateFormat.FORMAT2))
}
}

0 comments on commit 84eca60

Please sign in to comment.