1
- @file:OptIn(InternalReadiumApi ::class )
1
+ @file:OptIn(InternalReadiumApi ::class , kotlin.time. ExperimentalTime :: class )
2
2
3
3
package org.readium.r2.shared.util
4
4
5
5
import android.os.Parcel
6
6
import android.os.Parcelable
7
- import kotlinx.datetime .Clock
7
+ import kotlin.time .Clock
8
8
import kotlinx.datetime.LocalDate
9
9
import kotlinx.datetime.LocalDateTime
10
10
import kotlinx.datetime.TimeZone
@@ -25,14 +25,16 @@ import kotlinx.serialization.encoding.Encoder
25
25
import org.readium.r2.shared.InternalReadiumApi
26
26
import org.readium.r2.shared.extensions.tryOrNull
27
27
28
+ private typealias KotlinInstant = kotlin.time.Instant
29
+
28
30
/* *
29
31
* A moment in time.
30
32
*/
31
33
@Parcelize
32
- @TypeParceler< kotlinx.datetime. Instant , InstantParceler > ()
34
+ @TypeParceler<KotlinInstant , InstantParceler >()
33
35
@Serializable(with = InstantSerializer ::class )
34
36
public class Instant private constructor(
35
- private val value : kotlinx.datetime. Instant ,
37
+ private val value : KotlinInstant ,
36
38
) : Parcelable, Comparable<Instant> {
37
39
public companion object {
38
40
/* *
@@ -41,28 +43,28 @@ public class Instant private constructor(
41
43
* Returns null if it can't be parsed.
42
44
*/
43
45
public fun parse (input : String ): Instant ? {
44
- val instant = tryOrNull { kotlinx.datetime. Instant .parse(input) }
46
+ val instant = tryOrNull { KotlinInstant .parse(input) }
45
47
? : tryOrNull { LocalDateTime .parse(input).toInstant(TimeZone .UTC ) }
46
48
? : tryOrNull { LocalDate .parse(input).atStartOfDayIn(TimeZone .UTC ) }
47
49
48
50
return instant?.let { Instant (it) }
49
51
}
50
52
51
- public fun fromKotlinInstant (value : kotlinx.datetime. Instant ): Instant = Instant (value)
53
+ public fun fromKotlinInstant (value : KotlinInstant ): Instant = Instant (value)
52
54
53
55
public fun fromJavaDate (date : java.util.Date ): Instant =
54
- Instant (kotlinx.datetime. Instant .fromEpochMilliseconds(date.time))
56
+ Instant (KotlinInstant .fromEpochMilliseconds(date.time))
55
57
56
58
public fun fromEpochMilliseconds (milliseconds : Long ): Instant =
57
- Instant (kotlinx.datetime. Instant .fromEpochMilliseconds(milliseconds))
59
+ Instant (KotlinInstant .fromEpochMilliseconds(milliseconds))
58
60
59
61
/* *
60
62
* Returns an [Instant] representing the current moment in time.
61
63
*/
62
64
public fun now (): Instant = Instant (Clock .System .now())
63
65
}
64
66
65
- public fun toKotlinInstant (): kotlinx.datetime. Instant = value
67
+ public fun toKotlinInstant (): KotlinInstant = value
66
68
67
69
public fun toJavaDate (): java.util.Date = java.util.Date (value.toEpochMilliseconds())
68
70
@@ -83,12 +85,12 @@ public class Instant private constructor(
83
85
}
84
86
85
87
@InternalReadiumApi
86
- private object InstantParceler : Parceler<kotlinx.datetime.Instant > {
88
+ private object InstantParceler : Parceler<KotlinInstant > {
87
89
88
- override fun create (parcel : Parcel ): kotlinx.datetime. Instant =
89
- kotlinx.datetime. Instant .fromEpochMilliseconds(parcel.readLong())
90
+ override fun create (parcel : Parcel ): KotlinInstant =
91
+ KotlinInstant .fromEpochMilliseconds(parcel.readLong())
90
92
91
- override fun kotlinx.datetime.Instant .write (parcel : Parcel , flags : Int ) {
93
+ override fun KotlinInstant .write (parcel : Parcel , flags : Int ) {
92
94
parcel.writeLong(toEpochMilliseconds())
93
95
}
94
96
}
0 commit comments