1
+ /*
2
+ * Copyright 2019-2022 JetBrains s.r.o.
3
+ * Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
4
+ */
5
+
6
+ package kotlinx.datetime
7
+
8
+ import kotlinx.datetime.serializers.LocalTimeIso8601Serializer
9
+ import kotlinx.serialization.Serializable
10
+
11
+ /* *
12
+ * The time part of [LocalDateTime].
13
+ *
14
+ * This class represents time-of-day without a referencing a specific date.
15
+ * To reconstruct a full [LocalDateTime], representing civil date and time, [LocalTime] needs to be
16
+ * combined with [LocalDate] via [LocalDate.atTime] or [LocalTime.atDate].
17
+ *
18
+ * Also, [LocalTime] does not reference a particular time zone.
19
+ * Therefore, even on the same date, [LocalTime] denotes different moments of time.
20
+ * For example, `18:43` happens at different moments in Berlin and in Tokyo.
21
+ *
22
+ * The arithmetic on [LocalTime] values is not provided, since without accounting for the time zone
23
+ * transitions it may give misleading results.
24
+ */
25
+ @Serializable(LocalTimeIso8601Serializer ::class )
26
+ public expect class LocalTime : Comparable <LocalTime > {
27
+ public companion object {
28
+
29
+ /* *
30
+ * Parses a string that represents a time value in ISO-8601 and returns the parsed [LocalTime] value.
31
+ *
32
+ * Examples of time in ISO-8601 format:
33
+ * - `18:43`
34
+ * - `18:43:00`
35
+ * - `18:43:00.500`
36
+ * - `18:43:00.123456789`
37
+ *
38
+ * @throws IllegalArgumentException if the text cannot be parsed or the boundaries of [LocalTime] are
39
+ * exceeded.
40
+ */
41
+ public fun parse (isoString : String ): LocalTime
42
+
43
+ internal val MIN : LocalTime
44
+ internal val MAX : LocalTime
45
+ }
46
+
47
+ /* *
48
+ * Constructs a [LocalTime] instance from the given time components.
49
+ *
50
+ * The supported ranges of components:
51
+ * - [hour] `0..23`
52
+ * - [minute] `0..59`
53
+ * - [second] `0..59`
54
+ * - [nanosecond] `0..999_999_999`
55
+ *
56
+ * @throws IllegalArgumentException if any parameter is out of range.
57
+ */
58
+ public constructor (hour: Int , minute: Int , second: Int = 0 , nanosecond: Int = 0 )
59
+
60
+ /* * Returns the hour-of-day time component of this time value. */
61
+ public val hour: Int
62
+ /* * Returns the minute-of-hour time component of this time value. */
63
+ public val minute: Int
64
+ /* * Returns the second-of-minute time component of this time value. */
65
+ public val second: Int
66
+ /* * Returns the nanosecond-of-second time component of this time value. */
67
+ public val nanosecond: Int
68
+
69
+ /* *
70
+ * Compares `this` time value with the [other] time value.
71
+ * Returns zero if this value is equal to the other, a negative number if this value occurs earlier
72
+ * in the course of a typical day than the other, and a positive number if this value occurs
73
+ * later in the course of a typical day than the other.
74
+ *
75
+ * Note that, on days when there is a time overlap (for example, due to the daylight saving time
76
+ * transitions in autumn), a "lesser" wall-clock reading can, in fact, happen later than the
77
+ * "greater" one.
78
+ */
79
+ public override operator fun compareTo (other : LocalTime ): Int
80
+
81
+ /* *
82
+ * Converts this time value to the ISO-8601 string representation.
83
+ *
84
+ * @see LocalDateTime.parse
85
+ */
86
+ public override fun toString (): String
87
+ }
88
+
89
+ /* *
90
+ * Converts this string representing a time value in ISO-8601 format to a [LocalTime] value.
91
+ *
92
+ * See [LocalTime.parse] for examples of time string representations.
93
+ *
94
+ * @throws IllegalArgumentException if the text cannot be parsed or the boundaries of [LocalTime] are exceeded.
95
+ */
96
+ public fun String.toLocalTime (): LocalTime = LocalTime .parse(this )
97
+
98
+ /* *
99
+ * Combines this time's components with the specified date components into a [LocalDateTime] value.
100
+ */
101
+ public fun LocalTime.atDate (year : Int , monthNumber : Int , dayOfMonth : Int = 0): LocalDateTime =
102
+ LocalDateTime (year, monthNumber, dayOfMonth, hour, minute, second, nanosecond)
103
+
104
+ /* *
105
+ * Combines this time's components with the specified date components into a [LocalDateTime] value.
106
+ */
107
+ public fun LocalTime.atDate (year : Int , month : Month , dayOfMonth : Int = 0): LocalDateTime =
108
+ LocalDateTime (year, month, dayOfMonth, hour, minute, second, nanosecond)
109
+
110
+ /* *
111
+ * Combines this time's components with the specified [LocalDate] components into a [LocalDateTime] value.
112
+ */
113
+ public fun LocalTime.atDate (date : LocalDate ): LocalDateTime = atDate(date.year, date.monthNumber, date.dayOfMonth)
0 commit comments