-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathInstant.kt
186 lines (154 loc) · 8.42 KB
/
Instant.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* Copyright 2019-2020 JetBrains s.r.o.
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
*/
@file:JvmName("InstantJvmKt")
package kotlinx.datetime
import kotlinx.datetime.serializers.InstantIso8601Serializer
import kotlinx.serialization.Serializable
import java.time.DateTimeException
import java.time.format.DateTimeParseException
import java.time.temporal.ChronoUnit
import kotlin.time.*
import java.time.Instant as jtInstant
import java.time.OffsetDateTime as jtOffsetDateTime
import java.time.Clock as jtClock
@Serializable(with = InstantIso8601Serializer::class)
@OptIn(ExperimentalTime::class)
public actual class Instant internal constructor(internal val value: jtInstant) : Comparable<Instant> {
public actual val epochSeconds: Long
get() = value.epochSecond
public actual val nanosecondsOfSecond: Int
get() = value.nano
public actual fun toEpochMilliseconds(): Long = try {
value.toEpochMilli()
} catch (e: ArithmeticException) {
if (value.isAfter(java.time.Instant.EPOCH)) Long.MAX_VALUE else Long.MIN_VALUE
}
public actual operator fun plus(duration: Duration): Instant = duration.toComponents { seconds, nanoseconds ->
try {
Instant(value.plusSeconds(seconds).plusNanos(nanoseconds.toLong()))
} catch (e: java.lang.Exception) {
if (e !is ArithmeticException && e !is DateTimeException) throw e
if (duration.isPositive()) MAX else MIN
}
}
public actual operator fun minus(duration: Duration): Instant = plus(-duration)
public actual operator fun minus(other: Instant): Duration =
Duration.seconds(this.value.epochSecond - other.value.epochSecond) + // won't overflow given the instant bounds
Duration.nanoseconds(this.value.nano - other.value.nano)
public actual override operator fun compareTo(other: Instant): Int = this.value.compareTo(other.value)
override fun equals(other: Any?): Boolean =
(this === other) || (other is Instant && this.value == other.value)
override fun hashCode(): Int = value.hashCode()
actual override fun toString(): String = value.toString()
public actual companion object {
@Deprecated("Use Clock.System.now() instead", ReplaceWith("Clock.System.now()", "kotlinx.datetime.Clock"), level = DeprecationLevel.ERROR)
public actual fun now(): Instant =
Instant(jtClock.systemUTC().instant())
public actual fun fromEpochMilliseconds(epochMilliseconds: Long): Instant =
Instant(jtInstant.ofEpochMilli(epochMilliseconds))
public actual fun parse(isoString: String): Instant = try {
Instant(jtOffsetDateTime.parse(fixOffsetRepresentation(isoString)).toInstant())
} catch (e: DateTimeParseException) {
throw DateTimeFormatException(e)
}
/** A workaround for a quirk of the JDKs older than 11 where the string representations of Instant that have an
* offset of the form "+XX" are not recognized by [jtOffsetDateTime.parse], while "+XX:XX" work fine. */
private fun fixOffsetRepresentation(isoString: String): String {
val time = isoString.indexOf('T', ignoreCase = true)
if (time == -1) return isoString // the string is malformed
val offset = isoString.indexOfLast { c -> c == '+' || c == '-' }
if (offset < time) return isoString // the offset is 'Z' and not +/- something else
val separator = isoString.indexOf(':', offset) // if there is a ':' in the offset, no changes needed
return if (separator != -1) isoString else "$isoString:00"
}
public actual fun fromEpochSeconds(epochSeconds: Long, nanosecondAdjustment: Long): Instant = try {
Instant(jtInstant.ofEpochSecond(epochSeconds, nanosecondAdjustment))
} catch (e: Exception) {
if (e !is ArithmeticException && e !is DateTimeException) throw e
if (epochSeconds > 0) MAX else MIN
}
public actual fun fromEpochSeconds(epochSeconds: Long, nanosecondAdjustment: Int): Instant =
fromEpochSeconds(epochSeconds, nanosecondAdjustment.toLong())
public actual val DISTANT_PAST: Instant = Instant(jtInstant.ofEpochSecond(DISTANT_PAST_SECONDS, 999_999_999))
public actual val DISTANT_FUTURE: Instant = Instant(jtInstant.ofEpochSecond(DISTANT_FUTURE_SECONDS, 0))
internal actual val MIN: Instant = Instant(jtInstant.MIN)
internal actual val MAX: Instant = Instant(jtInstant.MAX)
}
}
private fun Instant.atZone(zone: TimeZone): java.time.ZonedDateTime = try {
value.atZone(zone.zoneId)
} catch (e: DateTimeException) {
throw DateTimeArithmeticException(e)
}
public actual fun Instant.plus(period: DateTimePeriod, timeZone: TimeZone): Instant {
try {
val thisZdt = atZone(timeZone)
return with(period) {
thisZdt
.run { if (totalMonths != 0) plusMonths(totalMonths.toLong()) else this }
.run { if (days != 0) plusDays(days.toLong()) else this }
.run { if (totalNanoseconds != 0L) plusNanos(totalNanoseconds) else this }
}.toInstant().let(::Instant)
} catch (e: DateTimeException) {
throw DateTimeArithmeticException(e)
}
}
public actual fun Instant.plus(unit: DateTimeUnit, timeZone: TimeZone): Instant =
plus(1L, unit, timeZone)
public actual fun Instant.plus(value: Int, unit: DateTimeUnit, timeZone: TimeZone): Instant =
plus(value.toLong(), unit, timeZone)
public actual fun Instant.minus(value: Int, unit: DateTimeUnit, timeZone: TimeZone): Instant =
plus(-value.toLong(), unit, timeZone)
public actual fun Instant.plus(value: Long, unit: DateTimeUnit, timeZone: TimeZone): Instant =
try {
val thisZdt = atZone(timeZone)
when (unit) {
is DateTimeUnit.TimeBased ->
plus(value, unit).value.also { it.atZone(timeZone.zoneId) }
is DateTimeUnit.DateBased.DayBased ->
thisZdt.plusDays(safeMultiply(value, unit.days.toLong())).toInstant()
is DateTimeUnit.DateBased.MonthBased ->
thisZdt.plusMonths(safeMultiply(value, unit.months.toLong())).toInstant()
}.let(::Instant)
} catch (e: Exception) {
if (e !is DateTimeException && e !is ArithmeticException) throw e
throw DateTimeArithmeticException("Instant $this cannot be represented as local date when adding $value $unit to it", e)
}
public actual fun Instant.plus(value: Long, unit: DateTimeUnit.TimeBased): Instant =
try {
multiplyAndDivide(value, unit.nanoseconds, NANOS_PER_ONE.toLong()).let { (d, r) ->
Instant(this.value.plusSeconds(d).plusNanos(r))
}
} catch (e: Exception) {
if (e !is DateTimeException && e !is ArithmeticException) throw e
if (value > 0) Instant.MAX else Instant.MIN
}
@OptIn(ExperimentalTime::class)
public actual fun Instant.periodUntil(other: Instant, timeZone: TimeZone): DateTimePeriod {
var thisZdt = this.atZone(timeZone)
val otherZdt = other.atZone(timeZone)
val months = thisZdt.until(otherZdt, ChronoUnit.MONTHS); thisZdt = thisZdt.plusMonths(months)
val days = thisZdt.until(otherZdt, ChronoUnit.DAYS); thisZdt = thisZdt.plusDays(days)
val nanoseconds = thisZdt.until(otherZdt, ChronoUnit.NANOS)
if (months > Int.MAX_VALUE || months < Int.MIN_VALUE) {
throw DateTimeArithmeticException("The number of months between $this and $other does not fit in an Int")
}
return buildDateTimePeriod(months.toInt(), days.toInt(), nanoseconds)
}
public actual fun Instant.until(other: Instant, unit: DateTimeUnit, timeZone: TimeZone): Long = try {
val thisZdt = this.atZone(timeZone)
val otherZdt = other.atZone(timeZone)
when(unit) {
is DateTimeUnit.TimeBased -> until(other, unit)
is DateTimeUnit.DateBased.DayBased -> thisZdt.until(otherZdt, ChronoUnit.DAYS) / unit.days
is DateTimeUnit.DateBased.MonthBased -> thisZdt.until(otherZdt, ChronoUnit.MONTHS) / unit.months
}
} catch (e: DateTimeException) {
throw DateTimeArithmeticException(e)
} catch (e: ArithmeticException) {
if (this.value < other.value) Long.MAX_VALUE else Long.MIN_VALUE
}
internal actual fun Instant.toStringWithOffset(offset: UtcOffset): String =
jtOffsetDateTime.ofInstant(this.value, offset.zoneOffset).toString()