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

Commit

Permalink
Rename String.toIso8601Date to String.iso8601ToDate
Browse files Browse the repository at this point in the history
  • Loading branch information
mickael-menu committed Feb 12, 2020
1 parent 045fcae commit 06b39d8
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
package org.readium.r2.shared.extensions

import org.joda.time.DateTime
import org.joda.time.DateTimeZone
import java.util.*

fun Date.toIso8601String(): String =
DateTime(this, DateTimeZone.UTC).toString()
DateTime(this).toString()
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@
package org.readium.r2.shared.extensions

import org.joda.time.DateTime
import org.joda.time.DateTimeZone
import java.util.*

fun String.toIso8601Date(): Date? {
fun String.iso8601ToDate(): Date? =
try {
return DateTime(this, DateTimeZone.UTC).toDate()
DateTime(this).toDate()
} catch (e: Exception) {
return null
null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import org.json.JSONObject
import org.readium.r2.shared.JSONable
import org.readium.r2.shared.util.logging.WarningLogger
import org.readium.r2.shared.extensions.optNullableString
import org.readium.r2.shared.extensions.toIso8601Date
import org.readium.r2.shared.extensions.iso8601ToDate
import org.readium.r2.shared.extensions.toIso8601String
import org.readium.r2.shared.util.KeyMapper
import org.readium.r2.shared.util.logging.JsonWarning
Expand Down Expand Up @@ -71,8 +71,8 @@ data class Availability(

return Availability(
state = state,
since = json?.optNullableString("since")?.toIso8601Date(),
until = json?.optNullableString("until")?.toIso8601Date()
since = json?.optNullableString("since")?.iso8601ToDate(),
until = json?.optNullableString("until")?.iso8601ToDate()
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ data class Metadata(
val identifier = json.remove("identifier") as? String
val type = json.remove("@type") as? String
val localizedSubtitle = LocalizedString.fromJSON(json.remove("subtitle"), warnings)
val modified = (json.remove("modified") as? String)?.toIso8601Date()
val published = (json.remove("published") as? String)?.toIso8601Date()
val modified = (json.remove("modified") as? String)?.iso8601ToDate()
val published = (json.remove("published") as? String)?.iso8601ToDate()
val languages = json.optStringsFromArrayOrSingle("language", remove = true)
val sortAs = json.remove("sortAs") as? String
val subjects = Subject.fromJSONArray(json.remove("subject"), normalizeHref, warnings)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import org.json.JSONObject
import org.junit.Assert.*
import org.junit.Test
import org.readium.r2.shared.assertJSONEquals
import org.readium.r2.shared.extensions.toIso8601Date
import org.readium.r2.shared.extensions.iso8601ToDate

class AvailabilityTest {

Expand Down Expand Up @@ -35,8 +35,8 @@ class AvailabilityTest {
assertEquals(
Availability(
state = Availability.State.AVAILABLE,
since = "2001-01-01T12:36:27.000Z".toIso8601Date(),
until = "2001-02-01T12:36:27.000Z".toIso8601Date()
since = "2001-01-01T12:36:27.000Z".iso8601ToDate(),
until = "2001-02-01T12:36:27.000Z".iso8601ToDate()
),
Availability.fromJSON(JSONObject("""{
'state': 'available',
Expand Down Expand Up @@ -70,8 +70,8 @@ class AvailabilityTest {
}"""),
Availability(
state = Availability.State.AVAILABLE,
since = "2001-01-01T12:36:27.000Z".toIso8601Date(),
until = "2001-02-01T12:36:27.000Z".toIso8601Date()
since = "2001-01-01T12:36:27.000Z".iso8601ToDate(),
until = "2001-02-01T12:36:27.000Z".iso8601ToDate()
).toJSON()
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import org.json.JSONObject
import org.junit.Assert.*
import org.junit.Test
import org.readium.r2.shared.assertJSONEquals
import org.readium.r2.shared.extensions.toIso8601Date
import org.readium.r2.shared.extensions.iso8601ToDate

class MetadataTest {

Expand All @@ -37,8 +37,8 @@ class MetadataTest {
"en" to "Subtitle",
"fr" to "Sous-titre"
)),
modified = "2001-01-01T12:36:27.000Z".toIso8601Date(),
published = "2001-01-02T12:36:27.000Z".toIso8601Date(),
modified = "2001-01-01T12:36:27.000Z".iso8601ToDate(),
published = "2001-01-02T12:36:27.000Z".iso8601ToDate(),
languages = listOf("en", "fr"),
sortAs = "sort key",
subjects = listOf(Subject(name = "Science Fiction"), Subject(name = "Fantasy")),
Expand Down Expand Up @@ -198,8 +198,8 @@ class MetadataTest {
"en" to "Subtitle",
"fr" to "Sous-titre"
)),
modified = "2001-01-01T12:36:27.000Z".toIso8601Date(),
published = "2001-01-02T12:36:27.000Z".toIso8601Date(),
modified = "2001-01-01T12:36:27.000Z".iso8601ToDate(),
published = "2001-01-02T12:36:27.000Z".iso8601ToDate(),
languages = listOf("en", "fr"),
sortAs = "sort key",
subjects = listOf(Subject(name = "Science Fiction"), Subject(name = "Fantasy")),
Expand Down

0 comments on commit 06b39d8

Please sign in to comment.