Skip to content

Commit cff3fdd

Browse files
dkhalanskyjbdanil-pavlovqwwdfsadilya-g
authored
Improve the KDoc (#367)
Fixes #347 We methodically went through the complete KDoc-based documentation and reworked it, adding usage examples, clarifying the specifics of behaviors, proposing the expected usage patterns, etc. Additionally, we enabled Dokka's fail-on-warnings flag. --------- Co-authored-by: Danil.Pavlov <[email protected]> Co-authored-by: Vsevolod Tolstopyatov <[email protected]> Co-authored-by: ilya-g <[email protected]>
1 parent e721269 commit cff3fdd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+4580
-304
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ To convert back, use the companion object function `Instant.fromEpochMillisecond
175175
### Converting instant and local date/time to and from the ISO 8601 string
176176

177177
`Instant`, `LocalDateTime`, `LocalDate` and `LocalTime` provide shortcuts for
178-
parsing and formatting them using the extended ISO-8601 format.
178+
parsing and formatting them using the extended ISO 8601 format.
179179
The `toString()` function is used to convert the value to a string in that format, and
180180
the `parse` function in companion object is used to parse a string representation back.
181181

@@ -201,7 +201,7 @@ LocalTime.parse("12:0:03.999") // fails with an IllegalArgumentException
201201

202202
### Working with other string formats
203203

204-
When some data needs to be formatted in some format other than ISO-8601, one
204+
When some data needs to be formatted in some format other than ISO 8601, one
205205
can define their own format or use some of the predefined ones:
206206

207207
```kotlin

core/build.gradle.kts

+2
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,9 @@ val downloadWindowsZonesMapping by tasks.registering {
397397
tasks.withType<AbstractDokkaLeafTask>().configureEach {
398398
pluginsMapConfiguration.set(mapOf("org.jetbrains.dokka.base.DokkaBase" to """{ "templatesDir" : "${projectDir.toString().replace('\\', '/')}/dokka-templates" }"""))
399399

400+
failOnWarning.set(true)
400401
dokkaSourceSets.configureEach {
402+
kotlin.sourceSets.commonTest.get().kotlin.srcDirs.forEach { samples.from(it) }
401403
// reportUndocumented.set(true) // much noisy output about `hashCode` and serializer encoders, decoders etc
402404
skipDeprecated.set(true)
403405
// Enum members and undocumented toString()

core/common/src/Clock.kt

+39-1
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,71 @@ import kotlin.time.*
1111
* A source of [Instant] values.
1212
*
1313
* 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.
1419
*/
1520
public interface Clock {
1621
/**
1722
* 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.
1831
*/
1932
public fun now(): Instant
2033

2134
/**
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
2352
*/
2453
public object System : Clock {
2554
override fun now(): Instant = @Suppress("DEPRECATION_ERROR") Instant.now()
2655
}
2756

57+
/** A companion object used purely for namespacing. */
2858
public companion object {
2959

3060
}
3161
}
3262

3363
/**
3464
* 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
3569
*/
3670
public fun Clock.todayIn(timeZone: TimeZone): LocalDate =
3771
now().toLocalDateTime(timeZone).date
3872

3973
/**
4074
* 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.
4179
*/
4280
@ExperimentalTime
4381
public fun Clock.asTimeSource(): TimeSource.WithComparableMarks = object : TimeSource.WithComparableMarks {

0 commit comments

Comments
 (0)