5
5
6
6
package kotlinx.datetime
7
7
8
+ import kotlinx.datetime.format.*
8
9
import kotlinx.datetime.serializers.LocalDateIso8601Serializer
9
10
import kotlinx.serialization.Serializable
10
11
@@ -23,14 +24,18 @@ import kotlinx.serialization.Serializable
23
24
public expect class LocalDate : Comparable <LocalDate > {
24
25
public companion object {
25
26
/* *
26
- * Parses a string that represents a date in ISO-8601 format
27
- * and returns the parsed [LocalDate] value.
27
+ * A shortcut for calling [DateTimeFormat.parse].
28
28
*
29
- * An example of a local date in ISO-8601 format: `2020-08-30`.
29
+ * Parses a string that represents a date and returns the parsed [LocalDate] value.
30
+ *
31
+ * If [format] is not specified, [Formats.ISO] is used.
30
32
*
31
33
* @throws IllegalArgumentException if the text cannot be parsed or the boundaries of [LocalDate] are exceeded.
34
+ *
35
+ * @see LocalDate.toString for formatting using the default format.
36
+ * @see LocalDate.format for formatting using a custom format.
32
37
*/
33
- public fun parse (isoString : String ): LocalDate
38
+ public fun parse (input : CharSequence , format : DateTimeFormat < LocalDate > = getIsoDateFormat() ): LocalDate
34
39
35
40
/* *
36
41
* Returns a [LocalDate] that is [epochDays] number of days from the epoch day `1970-01-01`.
@@ -41,10 +46,67 @@ public expect class LocalDate : Comparable<LocalDate> {
41
46
*/
42
47
public fun fromEpochDays (epochDays : Int ): LocalDate
43
48
49
+ /* *
50
+ * Creates a new format for parsing and formatting [LocalDate] values.
51
+ *
52
+ * Example:
53
+ * ```
54
+ * // 2020 Jan 05
55
+ * LocalDate.Format {
56
+ * year()
57
+ * char(' ')
58
+ * monthName(MonthNames.ENGLISH_ABBREVIATED)
59
+ * char(' ')
60
+ * dayOfMonth()
61
+ * }
62
+ * ```
63
+ *
64
+ * Only parsing and formatting of well-formed values is supported. If the input does not fit the boundaries
65
+ * (for example, [dayOfMonth] is 31 for February), consider using [DateTimeComponents.Format] instead.
66
+ *
67
+ * There is a collection of predefined formats in [LocalDate.Formats].
68
+ */
69
+ @Suppress(" FunctionName" )
70
+ public fun Format (block : DateTimeFormatBuilder .WithDate .() -> Unit ): DateTimeFormat <LocalDate >
71
+
44
72
internal val MIN : LocalDate
45
73
internal val MAX : LocalDate
46
74
}
47
75
76
+ /* *
77
+ * A collection of predefined formats for parsing and formatting [LocalDate] values.
78
+ *
79
+ * See [LocalDate.Formats.ISO] and [LocalDate.Formats.ISO_BASIC] for popular predefined formats.
80
+ * [LocalDate.parse] and [LocalDate.toString] can be used as convenient shortcuts for the
81
+ * [LocalDate.Formats.ISO] format.
82
+ *
83
+ * If predefined formats are not sufficient, use [LocalDate.Format] to create a custom
84
+ * [kotlinx.datetime.format.DateTimeFormat] for [LocalDate] values.
85
+ */
86
+ public object Formats {
87
+ /* *
88
+ * ISO 8601 extended format, which is the format used by [LocalDate.toString] and [LocalDate.parse].
89
+ *
90
+ * Examples of dates in ISO 8601 format:
91
+ * - `2020-08-30`
92
+ * - `+12020-08-30`
93
+ * - `0000-08-30`
94
+ * - `-0001-08-30`
95
+ */
96
+ public val ISO : DateTimeFormat <LocalDate >
97
+
98
+ /* *
99
+ * ISO 8601 basic format.
100
+ *
101
+ * Examples of dates in ISO 8601 basic format:
102
+ * - `20200830`
103
+ * - `+120200830`
104
+ * - `00000830`
105
+ * - `-00010830`
106
+ */
107
+ public val ISO_BASIC : DateTimeFormat <LocalDate >
108
+ }
109
+
48
110
/* *
49
111
* Constructs a [LocalDate] instance from the given date components.
50
112
*
@@ -77,14 +139,19 @@ public expect class LocalDate : Comparable<LocalDate> {
77
139
78
140
/* * Returns the year component of the date. */
79
141
public val year: Int
142
+
80
143
/* * Returns the number-of-month (1..12) component of the date. */
81
144
public val monthNumber: Int
145
+
82
146
/* * Returns the month ([Month]) component of the date. */
83
147
public val month: Month
148
+
84
149
/* * Returns the day-of-month component of the date. */
85
150
public val dayOfMonth: Int
151
+
86
152
/* * Returns the day-of-week component of the date. */
87
153
public val dayOfWeek: DayOfWeek
154
+
88
155
/* * Returns the day-of-year component of the date. */
89
156
public val dayOfYear: Int
90
157
@@ -106,13 +173,21 @@ public expect class LocalDate : Comparable<LocalDate> {
106
173
public override fun compareTo (other : LocalDate ): Int
107
174
108
175
/* *
109
- * Converts this date to the ISO-8601 string representation.
176
+ * Converts this date to the extended ISO-8601 string representation.
110
177
*
111
- * @see LocalDate.parse
178
+ * @see Formats.ISO for the format details.
179
+ * @see parse for the dual operation: obtaining [LocalDate] from a string.
180
+ * @see LocalDate.format for formatting using a custom format.
112
181
*/
113
182
public override fun toString (): String
114
183
}
115
184
185
+ /* *
186
+ * Formats this value using the given [format].
187
+ * Equivalent to calling [DateTimeFormat.format] on [format] with `this`.
188
+ */
189
+ public fun LocalDate.format (format : DateTimeFormat <LocalDate >): String = format.format(this )
190
+
116
191
/* *
117
192
* @suppress
118
193
*/
@@ -159,9 +234,8 @@ public operator fun LocalDate.minus(period: DatePeriod): LocalDate =
159
234
if (period.days != Int .MIN_VALUE && period.months != Int .MIN_VALUE ) {
160
235
plus(with (period) { DatePeriod (- years, - months, - days) })
161
236
} else {
162
- minus(period.years, DateTimeUnit .YEAR ).
163
- minus(period.months, DateTimeUnit .MONTH ).
164
- minus(period.days, DateTimeUnit .DAY )
237
+ minus(period.years, DateTimeUnit .YEAR ).minus(period.months, DateTimeUnit .MONTH )
238
+ .minus(period.days, DateTimeUnit .DAY )
165
239
}
166
240
167
241
/* *
@@ -299,3 +373,6 @@ public expect fun LocalDate.plus(value: Long, unit: DateTimeUnit.DateBased): Loc
299
373
* @throws DateTimeArithmeticException if the result exceeds the boundaries of [LocalDate].
300
374
*/
301
375
public fun LocalDate.minus (value : Long , unit : DateTimeUnit .DateBased ): LocalDate = plus(- value, unit)
376
+
377
+ // workaround for https://youtrack.jetbrains.com/issue/KT-65484
378
+ internal fun getIsoDateFormat () = LocalDate .Formats .ISO
0 commit comments