@@ -11,145 +11,291 @@ import kotlin.time.ExperimentalTime
11
11
@OptIn(ExperimentalTime ::class )
12
12
public expect class Instant : Comparable <Instant > {
13
13
14
- /* * */
14
+ /* *
15
+ * The number of seconds from the epoch instant `1970-01-01T00:00:00Z` rounded down to a [Long] number.
16
+ *
17
+ * The difference between the rounded number of seconds and the actual number of seconds
18
+ * is returned by [nanosecondsOfSecond] property expressed in nanoseconds.
19
+ *
20
+ * @see Instant.fromEpochSeconds
21
+ */
15
22
public val epochSeconds: Long
16
23
17
- /* * */
24
+ /* *
25
+ * The number of nanoseconds by which this instant is later than [epochSeconds] from the epoch instant.
26
+ *
27
+ * The value is always positive and lies in the range `0..999_999_999`.
28
+ *
29
+ * @see Instant.fromEpochSeconds
30
+ */
18
31
public val nanosecondsOfSecond: Int
19
32
20
33
/* *
21
- * The return value is clamped to [Long.MAX_VALUE] or [Long.MIN_VALUE] if the result does not fit in [Long].
34
+ * Returns the number of milliseconds from the epoch instant `1970-01-01T00:00:00Z`.
35
+ *
36
+ * Any fractional part of millisecond is rounded down to the whole number of milliseconds.
37
+ *
38
+ * If the result does not fit in [Long], returns [Long.MAX_VALUE] for a positive result or [Long.MIN_VALUE] for a negative result.
39
+ *
40
+ * @see Instant.fromEpochMilliseconds
22
41
*/
23
42
public fun toEpochMilliseconds (): Long
24
43
25
44
/* *
45
+ * Returns an instant that is the result of adding the specified [duration] to this instant.
46
+ *
47
+ * If the [duration] is positive, the returned instant is later than this instant.
48
+ * If the [duration] is negative, the returned instant is earlier than this instant.
49
+ *
26
50
* The return value is clamped to the platform-specific boundaries for [Instant] if the result exceeds them.
27
51
*/
28
52
@ExperimentalTime
29
53
public operator fun plus (duration : Duration ): Instant
30
54
31
55
/* *
56
+ * Returns an instant that is the result of subtracting the specified [duration] from this instant.
57
+ *
58
+ * If the [duration] is positive, the returned instant is earlier than this instant.
59
+ * If the [duration] is negative, the returned instant is later than this instant.
60
+ *
32
61
* The return value is clamped to the platform-specific boundaries for [Instant] if the result exceeds them.
33
62
*/
34
63
@ExperimentalTime
35
64
public operator fun minus (duration : Duration ): Instant
36
65
37
66
// questionable
38
- /* * */
67
+ /* *
68
+ * Returns the [Duration] between two instants: [other] and `this`.
69
+ *
70
+ * The duration returned is positive if this instant is later than the other,
71
+ * and negative if this instant is earlier than the other.
72
+ *
73
+ * The result is never clamped, but note that for instants that are far apart,
74
+ * the value returned may represent the duration between them inexactly due to the loss of precision.
75
+ */
39
76
@ExperimentalTime
40
77
public operator fun minus (other : Instant ): Duration
41
78
42
- /* * */
79
+ /* *
80
+ * Compares `this` instant with the [other] instant.
81
+ * Returns zero if this instant represent the same moment as the other (i.e. equal to other),
82
+ * a negative number if this instant is earlier than the other,
83
+ * and a positive number if this instant is later than the other.
84
+ */
43
85
public override operator fun compareTo (other : Instant ): Int
44
86
87
+ /* *
88
+ * Converts this instant to the ISO-8601 string representation.
89
+ *
90
+ * @see Instant.parse
91
+ */
92
+ public override fun toString (): String
93
+
94
+
45
95
companion object {
46
- /* * */
47
96
@Deprecated(" Use Clock.System.now() instead" , ReplaceWith (" Clock.System.now()" , " kotlinx.datetime.Clock" ), level = DeprecationLevel .ERROR )
48
97
fun now (): Instant
49
98
50
99
/* *
100
+ * Returns an [Instant] that is [epochMilliseconds] number of milliseconds from the epoch instant `1970-01-01T00:00:00Z`.
101
+ *
51
102
* The return value is clamped to the platform-specific boundaries for [Instant] if the result exceeds them.
52
103
*/
53
104
fun fromEpochMilliseconds (epochMilliseconds : Long ): Instant
54
105
55
106
/* *
107
+ * Returns an [Instant] that is the [epochSeconds] number of seconds from the epoch instant `1970-01-01T00:00:00Z`
108
+ * and the [nanosecondAdjustment] number of nanoseconds from the whole second.
109
+ *
56
110
* The return value is clamped to the platform-specific boundaries for [Instant] if the result exceeds them.
57
111
*/
58
112
fun fromEpochSeconds (epochSeconds : Long , nanosecondAdjustment : Long = 0): Instant
59
113
60
114
/* *
115
+ * Parses a string that represents an instant in ISO-8601 format including date and time components and
116
+ * the mandatory `Z` designator of the UTC+0 time zone and returns the parsed [Instant] value.
117
+ *
118
+ * Examples of instants in ISO-8601 format:
119
+ * - `2020-08-30T18:43:00Z`
120
+ * - `2020-08-30T18:43:00.500Z`
121
+ * - `2020-08-30T18:43:00.123456789Z`
122
+ *
61
123
* @throws DateTimeFormatException if the text cannot be parsed or the boundaries of [Instant] are exceeded.
62
124
*/
63
125
fun parse (isoString : String ): Instant
64
126
65
- // -100001-12-31T23:59:59.999999999Z
66
- val DISTANT_PAST : Instant
67
127
68
- // +100000-01-01T00:00:00Z
69
- val DISTANT_FUTURE : Instant
128
+ /* *
129
+ * An instant value that is far in the past.
130
+ *
131
+ * All instants in the range `DISTANT_PAST..DISTANT_FUTURE` can be converted to [LocalDateTime][Instant.toLocalDateTime]
132
+ * without exceptions on all supported platforms.
133
+ */
134
+ val DISTANT_PAST : Instant // -100001-12-31T23:59:59.999999999Z
135
+
136
+ /* *
137
+ * An instant value that is far in the future.
138
+ *
139
+ * All instants in the range `DISTANT_PAST..DISTANT_FUTURE` can be converted to [LocalDateTime][Instant.toLocalDateTime]
140
+ * without exceptions on all supported platforms.
141
+ */
142
+ val DISTANT_FUTURE : Instant // +100000-01-01T00:00:00Z
70
143
71
144
internal val MIN : Instant
72
145
internal val MAX : Instant
73
146
}
74
147
}
75
148
149
+ /* * Returns true if the instant is not later than [Instant.DISTANT_PAST]. */
76
150
public val Instant .isDistantPast
77
151
get() = this <= Instant .DISTANT_PAST
78
152
153
+ /* * Returns true if the instant is not earlier than [Instant.DISTANT_FUTURE]. */
79
154
public val Instant .isDistantFuture
80
155
get() = this >= Instant .DISTANT_FUTURE
81
156
82
157
/* *
158
+ * Converts this string representing an instant in ISO-8601 format including date and time components and
159
+ * the mandatory `Z` designator of the UTC+0 time zone to an [Instant] value.
160
+ *
161
+ * See [Instant.parse] for examples of instant string representations.
162
+ *
83
163
* @throws DateTimeFormatException if the text cannot be parsed or the boundaries of [Instant] are exceeded.
84
164
*/
85
165
public fun String.toInstant (): Instant = Instant .parse(this )
86
166
87
167
/* *
168
+ * Returns an instant that is the result of adding components of [DateTimePeriod] to this instant. The components are
169
+ * added in the order from the largest units to the smallest, i.e. from years to nanoseconds.
170
+ *
88
171
* @throws DateTimeArithmeticException if this value or the results of intermediate computations are too large to fit in
89
172
* [LocalDateTime].
90
173
*/
91
174
public expect fun Instant.plus (period : DateTimePeriod , timeZone : TimeZone ): Instant
92
175
93
176
/* *
94
- * @throws DateTimeArithmeticException if this [Instant] or [other] is too large to fit in [LocalDateTime].
177
+ * Returns a [DateTimePeriod] representing the difference between `this` and [other] instants.
178
+ *
179
+ * The components of [DateTimePeriod] are calculated so that adding it to `this` instant results in the [other] instant.
180
+ *
181
+ * All components of the [DateTimePeriod] returned are:
182
+ * - positive or zero if this instant is earlier than the other,
183
+ * - negative or zero if this instant is later than the other,
184
+ * - exactly zero if this instant is equal to the other.
185
+ *
186
+ * @throws DateTimeArithmeticException if `this` or [other] instant is too large to fit in [LocalDateTime].
95
187
*/
96
188
public expect fun Instant.periodUntil (other : Instant , timeZone : TimeZone ): DateTimePeriod
97
189
98
190
/* *
99
- * The return value is clamped to [Long.MAX_VALUE] or [Long.MIN_VALUE] if [unit] is more granular than
100
- * [DateTimeUnit.DAY] and the result is too large .
191
+ * Returns the whole number of the specified date or time [units] [unit] between `this` and [other] instants
192
+ * in the specified [timeZone] .
101
193
*
102
- * @throws DateTimeArithmeticException if this [Instant] or [other] is too large to fit in [LocalDateTime].
194
+ * The value returned is:
195
+ * - positive or zero if this instant is earlier than the other,
196
+ * - negative or zero if this instant is later than the other,
197
+ * - zero if this instant is equal to the other.
198
+
199
+ * If the result does not fit in [Long], returns [Long.MAX_VALUE] for a positive result or [Long.MIN_VALUE] for a negative result.
200
+ *
201
+ * @throws DateTimeArithmeticException if `this` or [other] instant is too large to fit in [LocalDateTime].
103
202
*/
104
203
public expect fun Instant.until (other : Instant , unit : DateTimeUnit , timeZone : TimeZone ): Long
105
204
106
205
/* *
107
- * The return value is clamped to [Int.MAX_VALUE] or [Int.MIN_VALUE] if the result would otherwise cause an arithmetic
108
- * overflow.
206
+ * Returns the number of whole days between two instants in the specified [timeZone].
207
+ *
208
+ * If the result does not fit in [Int], returns [Int.MAX_VALUE] for a positive result or [Int.MIN_VALUE] for a negative result.
109
209
*
110
- * @throws DateTimeArithmeticException if this [Instant] or [other] is too large to fit in [LocalDateTime].
210
+ * @see Instant.until
211
+ * @throws DateTimeArithmeticException if `this` or [other] instant is too large to fit in [LocalDateTime].
111
212
*/
112
213
public fun Instant.daysUntil (other : Instant , timeZone : TimeZone ): Int =
113
214
until(other, DateTimeUnit .DAY , timeZone).clampToInt()
114
215
115
216
/* *
116
- * The return value is clamped to [Int.MAX_VALUE] or [Int.MIN_VALUE] if the result would otherwise cause an arithmetic
117
- * overflow.
217
+ * Returns the number of whole months between two instants in the specified [timeZone].
118
218
*
119
- * @throws DateTimeArithmeticException if this [Instant] or [other] is too large to fit in [LocalDateTime].
219
+ * If the result does not fit in [Int], returns [Int.MAX_VALUE] for a positive result or [Int.MIN_VALUE] for a negative result.
220
+ *
221
+ * @see Instant.until
222
+ * @throws DateTimeArithmeticException if `this` or [other] instant is too large to fit in [LocalDateTime].
120
223
*/
121
224
public fun Instant.monthsUntil (other : Instant , timeZone : TimeZone ): Int =
122
225
until(other, DateTimeUnit .MONTH , timeZone).clampToInt()
123
226
124
227
/* *
125
- * The return value is clamped to [Int.MAX_VALUE] or [Int.MIN_VALUE] if the result would otherwise cause an arithmetic
126
- * overflow.
228
+ * Returns the number of whole years between two instants in the specified [timeZone].
229
+ *
230
+ * If the result does not fit in [Int], returns [Int.MAX_VALUE] for a positive result or [Int.MIN_VALUE] for a negative result.
127
231
*
128
- * @throws DateTimeArithmeticException if this [Instant] or [other] is too large to fit in [LocalDateTime].
232
+ * @see Instant.until
233
+ * @throws DateTimeArithmeticException if `this` or [other] instant is too large to fit in [LocalDateTime].
129
234
*/
130
235
public fun Instant.yearsUntil (other : Instant , timeZone : TimeZone ): Int =
131
236
until(other, DateTimeUnit .YEAR , timeZone).clampToInt()
132
237
238
+ /* *
239
+ * Returns a [DateTimePeriod] representing the difference between [other] and `this` instants.
240
+ *
241
+ * The components of [DateTimePeriod] are calculated so that adding it back to the `other` instant results in this instant.
242
+ *
243
+ * All components of the [DateTimePeriod] returned are:
244
+ * - negative or zero if this instant is earlier than the other,
245
+ * - positive or zero if this instant is later than the other,
246
+ * - exactly zero if this instant is equal to the other.
247
+ *
248
+ * @throws DateTimeArithmeticException if `this` or [other] instant is too large to fit in [LocalDateTime].
249
+ * @see Instant.periodUntil
250
+ */
133
251
public fun Instant.minus (other : Instant , timeZone : TimeZone ): DateTimePeriod =
134
252
other.periodUntil(this , timeZone)
135
253
136
254
137
255
/* *
256
+ * Returns an instant that is the result of adding one [unit] to this instant
257
+ * in the specified [timeZone].
258
+ *
259
+ * The returned instant is later than this instant.
260
+ *
138
261
* @throws DateTimeArithmeticException if this value or the result is too large to fit in [LocalDateTime].
139
262
*/
140
263
public expect fun Instant.plus (unit : DateTimeUnit , timeZone : TimeZone ): Instant
141
264
142
265
/* *
266
+ * Returns an instant that is the result of adding the [value] number of the specified [unit] to this instant
267
+ * in the specified [timeZone].
268
+ *
269
+ * If the [value] is positive, the returned instant is later than this instant.
270
+ * If the [value] is negative, the returned instant is earlier than this instant.
271
+ *
143
272
* @throws DateTimeArithmeticException if this value or the result is too large to fit in [LocalDateTime].
144
273
*/
145
274
public expect fun Instant.plus (value : Int , unit : DateTimeUnit , timeZone : TimeZone ): Instant
146
275
147
276
/* *
277
+ * Returns an instant that is the result of adding the [value] number of the specified [unit] to this instant
278
+ * in the specified [timeZone].
279
+ *
280
+ * If the [value] is positive, the returned instant is later than this instant.
281
+ * If the [value] is negative, the returned instant is earlier than this instant.
282
+ *
148
283
* @throws DateTimeArithmeticException if this value or the result is too large to fit in [LocalDateTime].
149
284
*/
150
285
public expect fun Instant.plus (value : Long , unit : DateTimeUnit , timeZone : TimeZone ): Instant
151
286
152
-
287
+ /* *
288
+ * Returns the whole number of the specified date or time [units][unit] between [other] and `this` instants
289
+ * in the specified [timeZone].
290
+ *
291
+ * The value returned is negative or zero if this instant is earlier than the other,
292
+ * and positive or zero if this instant is later than the other.
293
+ *
294
+ * If the result does not fit in [Long], returns [Long.MAX_VALUE] for a positive result or [Long.MIN_VALUE] for a negative result.
295
+ *
296
+ * @throws DateTimeArithmeticException if `this` or [other] instant is too large to fit in [LocalDateTime].
297
+ * @see Instant.until
298
+ */
153
299
public fun Instant.minus (other : Instant , unit : DateTimeUnit , timeZone : TimeZone ): Long =
154
300
other.until(this , unit, timeZone)
155
301
0 commit comments