@@ -11,33 +11,71 @@ import kotlin.time.*
11
11
* A source of [Instant] values.
12
12
*
13
13
* See [Clock.System][Clock.System] for the clock instance that queries the operating system.
14
+ *
15
+ * It is not recommended to use [Clock.System] directly in the implementation. Instead, you can pass a
16
+ * [Clock] explicitly to the necessary functions or classes.
17
+ * This way, tests can be written deterministically by providing custom [Clock] implementations
18
+ * to the system under test.
14
19
*/
15
20
public interface Clock {
16
21
/* *
17
22
* Returns the [Instant] corresponding to the current time, according to this clock.
23
+ *
24
+ * It is not guaranteed that calling [now] later will return a larger [Instant].
25
+ * In particular, for [Clock.System] it is completely expected that the opposite will happen,
26
+ * and it must be taken into account.
27
+ * See the [System] documentation for details.
28
+ *
29
+ * Even though [Instant] is defined to be on the UTC-SLS time scale, which enforces a specific way of handling
30
+ * leap seconds, [now] is not guaranteed to handle leap seconds in any specific way.
18
31
*/
19
32
public fun now (): Instant
20
33
21
34
/* *
22
- * The [Clock] instance that queries the operating system as its source of knowledge of time.
35
+ * The [Clock] instance that queries the platform-specific system clock as its source of time knowledge.
36
+ *
37
+ * Successive calls to [now] will not necessarily return increasing [Instant] values, and when they do,
38
+ * these increases will not necessarily correspond to the elapsed time.
39
+ *
40
+ * For example, when using [Clock.System], the following could happen:
41
+ * - [now] returns `2023-01-02T22:35:01Z`.
42
+ * - The system queries the Internet and recognizes that its clock needs adjusting.
43
+ * - [now] returns `2023-01-02T22:32:05Z`.
44
+ *
45
+ * When you need predictable intervals between successive measurements, consider using [TimeSource.Monotonic].
46
+ *
47
+ * For improved testability, you should avoid using [Clock.System] directly in the implementation
48
+ * and pass a [Clock] explicitly instead. For example:
49
+ *
50
+ * @sample kotlinx.datetime.test.samples.ClockSamples.system
51
+ * @sample kotlinx.datetime.test.samples.ClockSamples.dependencyInjection
23
52
*/
24
53
public object System : Clock {
25
54
override fun now (): Instant = @Suppress(" DEPRECATION_ERROR" ) Instant .now()
26
55
}
27
56
57
+ /* * A companion object used purely for namespacing. */
28
58
public companion object {
29
59
30
60
}
31
61
}
32
62
33
63
/* *
34
64
* Returns the current date at the given [time zone][timeZone], according to [this Clock][this].
65
+ *
66
+ * The time zone is important because the current date is not the same in all time zones at the same instant.
67
+ *
68
+ * @sample kotlinx.datetime.test.samples.ClockSamples.todayIn
35
69
*/
36
70
public fun Clock.todayIn (timeZone : TimeZone ): LocalDate =
37
71
now().toLocalDateTime(timeZone).date
38
72
39
73
/* *
40
74
* Returns a [TimeSource] that uses this [Clock] to mark a time instant and to find the amount of time elapsed since that mark.
75
+ *
76
+ * **Pitfall**: using this function with [Clock.System] is error-prone
77
+ * because [Clock.System] is not well suited for measuring time intervals.
78
+ * Please only use this conversion function on the [Clock] instances that are fully controlled programmatically.
41
79
*/
42
80
@ExperimentalTime
43
81
public fun Clock.asTimeSource (): TimeSource .WithComparableMarks = object : TimeSource .WithComparableMarks {
0 commit comments