Skip to content

Commit ee220be

Browse files
committed
Add documentation for common methods
1 parent be1e529 commit ee220be

File tree

13 files changed

+443
-48
lines changed

13 files changed

+443
-48
lines changed

core/commonMain/src/Instant.kt

Lines changed: 170 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,145 +11,291 @@ import kotlin.time.ExperimentalTime
1111
@OptIn(ExperimentalTime::class)
1212
public expect class Instant : Comparable<Instant> {
1313

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+
*/
1522
public val epochSeconds: Long
1623

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+
*/
1831
public val nanosecondsOfSecond: Int
1932

2033
/**
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
2241
*/
2342
public fun toEpochMilliseconds(): Long
2443

2544
/**
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+
*
2650
* The return value is clamped to the platform-specific boundaries for [Instant] if the result exceeds them.
2751
*/
2852
@ExperimentalTime
2953
public operator fun plus(duration: Duration): Instant
3054

3155
/**
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+
*
3261
* The return value is clamped to the platform-specific boundaries for [Instant] if the result exceeds them.
3362
*/
3463
@ExperimentalTime
3564
public operator fun minus(duration: Duration): Instant
3665

3766
// 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+
*/
3976
@ExperimentalTime
4077
public operator fun minus(other: Instant): Duration
4178

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+
*/
4385
public override operator fun compareTo(other: Instant): Int
4486

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+
4595
companion object {
46-
/** */
4796
@Deprecated("Use Clock.System.now() instead", ReplaceWith("Clock.System.now()", "kotlinx.datetime.Clock"), level = DeprecationLevel.ERROR)
4897
fun now(): Instant
4998

5099
/**
100+
* Returns an [Instant] that is [epochMilliseconds] number of milliseconds from the epoch instant `1970-01-01T00:00:00Z`.
101+
*
51102
* The return value is clamped to the platform-specific boundaries for [Instant] if the result exceeds them.
52103
*/
53104
fun fromEpochMilliseconds(epochMilliseconds: Long): Instant
54105

55106
/**
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+
*
56110
* The return value is clamped to the platform-specific boundaries for [Instant] if the result exceeds them.
57111
*/
58112
fun fromEpochSeconds(epochSeconds: Long, nanosecondAdjustment: Long = 0): Instant
59113

60114
/**
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+
*
61123
* @throws DateTimeFormatException if the text cannot be parsed or the boundaries of [Instant] are exceeded.
62124
*/
63125
fun parse(isoString: String): Instant
64126

65-
// -100001-12-31T23:59:59.999999999Z
66-
val DISTANT_PAST: Instant
67127

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
70143

71144
internal val MIN: Instant
72145
internal val MAX: Instant
73146
}
74147
}
75148

149+
/** Returns true if the instant is not later than [Instant.DISTANT_PAST]. */
76150
public val Instant.isDistantPast
77151
get() = this <= Instant.DISTANT_PAST
78152

153+
/** Returns true if the instant is not earlier than [Instant.DISTANT_FUTURE]. */
79154
public val Instant.isDistantFuture
80155
get() = this >= Instant.DISTANT_FUTURE
81156

82157
/**
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+
*
83163
* @throws DateTimeFormatException if the text cannot be parsed or the boundaries of [Instant] are exceeded.
84164
*/
85165
public fun String.toInstant(): Instant = Instant.parse(this)
86166

87167
/**
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+
*
88171
* @throws DateTimeArithmeticException if this value or the results of intermediate computations are too large to fit in
89172
* [LocalDateTime].
90173
*/
91174
public expect fun Instant.plus(period: DateTimePeriod, timeZone: TimeZone): Instant
92175

93176
/**
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].
95187
*/
96188
public expect fun Instant.periodUntil(other: Instant, timeZone: TimeZone): DateTimePeriod
97189

98190
/**
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].
101193
*
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].
103202
*/
104203
public expect fun Instant.until(other: Instant, unit: DateTimeUnit, timeZone: TimeZone): Long
105204

106205
/**
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.
109209
*
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].
111212
*/
112213
public fun Instant.daysUntil(other: Instant, timeZone: TimeZone): Int =
113214
until(other, DateTimeUnit.DAY, timeZone).clampToInt()
114215

115216
/**
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].
118218
*
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].
120223
*/
121224
public fun Instant.monthsUntil(other: Instant, timeZone: TimeZone): Int =
122225
until(other, DateTimeUnit.MONTH, timeZone).clampToInt()
123226

124227
/**
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.
127231
*
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].
129234
*/
130235
public fun Instant.yearsUntil(other: Instant, timeZone: TimeZone): Int =
131236
until(other, DateTimeUnit.YEAR, timeZone).clampToInt()
132237

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+
*/
133251
public fun Instant.minus(other: Instant, timeZone: TimeZone): DateTimePeriod =
134252
other.periodUntil(this, timeZone)
135253

136254

137255
/**
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+
*
138261
* @throws DateTimeArithmeticException if this value or the result is too large to fit in [LocalDateTime].
139262
*/
140263
public expect fun Instant.plus(unit: DateTimeUnit, timeZone: TimeZone): Instant
141264

142265
/**
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+
*
143272
* @throws DateTimeArithmeticException if this value or the result is too large to fit in [LocalDateTime].
144273
*/
145274
public expect fun Instant.plus(value: Int, unit: DateTimeUnit, timeZone: TimeZone): Instant
146275

147276
/**
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+
*
148283
* @throws DateTimeArithmeticException if this value or the result is too large to fit in [LocalDateTime].
149284
*/
150285
public expect fun Instant.plus(value: Long, unit: DateTimeUnit, timeZone: TimeZone): Instant
151286

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+
*/
153299
public fun Instant.minus(other: Instant, unit: DateTimeUnit, timeZone: TimeZone): Long =
154300
other.until(this, unit, timeZone)
155301

0 commit comments

Comments
 (0)