@@ -12,30 +12,26 @@ package org.readium.r2.shared.extensions
12
12
import android.net.Uri
13
13
import java.net.URL
14
14
import java.security.MessageDigest
15
- import java.util.*
16
- import org.joda.time.DateTime
17
- import org.joda.time.DateTimeZone
15
+ import java.util.Date
16
+ import kotlinx.datetime.Instant
17
+ import kotlinx.datetime.LocalDate
18
+ import kotlinx.datetime.LocalDateTime
19
+ import kotlinx.datetime.TimeZone
20
+ import kotlinx.datetime.atStartOfDayIn
21
+ import kotlinx.datetime.toInstant
18
22
import org.json.JSONException
19
23
import org.json.JSONObject
20
24
import org.readium.r2.shared.InternalReadiumApi
21
25
22
26
@InternalReadiumApi
23
- public fun String.iso8601ToDate (): Date ? =
24
- try {
25
- // We assume that a date without a time zone component is in UTC. To handle this properly,
26
- // we need to set the default time zone of Joda to UTC, since by default it uses the local
27
- // time zone. This ensures that apps see exactly the same dates (e.g. published) no matter
28
- // where they are located.
29
- // For the same reason, the output Date will be in UTC. Apps should convert it to the local
30
- // time zone for display purposes, or keep it as UTC for storage.
31
- val defaultTZ = DateTimeZone .getDefault()
32
- DateTimeZone .setDefault(DateTimeZone .UTC )
33
- val date = DateTime (this ).toDateTime(DateTimeZone .UTC ).toDate()
34
- DateTimeZone .setDefault(defaultTZ)
35
- date
36
- } catch (e: Exception ) {
37
- null
38
- }
27
+ public fun String.iso8601ToDate (): Date ? {
28
+ val instant = tryOrNull { Instant .parse(this ) }
29
+ ? : tryOrNull { LocalDateTime .parse(this ).toInstant(TimeZone .UTC ) }
30
+ ? : tryOrNull { LocalDate .parse(this ).atStartOfDayIn(TimeZone .UTC ) }
31
+ ? : return null
32
+
33
+ return Date (instant.toEpochMilliseconds())
34
+ }
39
35
40
36
/* *
41
37
* If this string starts with the given [prefix], returns this string.
0 commit comments