@@ -18,26 +18,26 @@ import kotlin.reflect.*
18
18
*
19
19
* Its main purpose is to provide support for complex date-time formats that don't correspond to any of the standard
20
20
* entities in the library. For example, a format that includes only the month and the day of the month, but not the
21
- * year, can not be represented and parsed as a [LocalDate], but it is valid for a [ValueBag ].
21
+ * year, can not be represented and parsed as a [LocalDate], but it is valid for a [DateTimeComponents ].
22
22
*
23
23
* Example:
24
24
* ```
25
25
* val input = "2020-03-16T23:59:59.999999999+03:00"
26
- * val bag = ValueBag .Format.ISO_INSTANT.parse(input)
26
+ * val bag = DateTimeComponents .Format.ISO_INSTANT.parse(input)
27
27
* val localDateTime = bag.toLocalDateTime() // LocalDateTime(2020, 3, 16, 23, 59, 59, 999_999_999)
28
28
* val instant = bag.toInstantUsingUtcOffset() // Instant.parse("2020-03-16T20:59:59.999999999Z")
29
29
* val offset = bag.toUtcOffset() // UtcOffset(hours = 3)
30
30
* ```
31
31
*
32
32
* Another purpose is to support parsing and formatting data with out-of-bounds values. For example, parsing
33
- * `23:59:60` as a [LocalTime] is not possible, but it is possible to parse it as a [ValueBag ], adjust the value by
33
+ * `23:59:60` as a [LocalTime] is not possible, but it is possible to parse it as a [DateTimeComponents ], adjust the value by
34
34
* setting [second] to `59`, and then convert it to a [LocalTime] via [toLocalTime].
35
35
*
36
36
* Example:
37
37
* ```
38
38
* val input = "23:59:60"
39
39
* val extraDay: Boolean
40
- * val time = ValueBag .Format {
40
+ * val time = DateTimeComponents .Format {
41
41
* appendTime(LocalTime.Format.ISO)
42
42
* }.parse(input).apply {
43
43
* if (hour == 23 && minute == 59 && second == 60) {
@@ -49,12 +49,12 @@ import kotlin.reflect.*
49
49
* ```
50
50
*
51
51
* Because this class has limited applications, constructing it directly is not possible.
52
- * For formatting, use the [format] overload that accepts a lambda with a [ValueBag ] receiver.
52
+ * For formatting, use the [format] overload that accepts a lambda with a [DateTimeComponents ] receiver.
53
53
*
54
54
* Example:
55
55
* ```
56
56
* // Mon, 16 Mar 2020 23:59:59 +0300
57
- * ValueBag .Format.RFC_1123.format {
57
+ * DateTimeComponents .Format.RFC_1123.format {
58
58
* populateFrom(LocalDateTime(2020, 3, 16, 23, 59, 59, 999_999_999))
59
59
* populateFrom(UtcOffset(hours = 3))
60
60
* }
@@ -63,26 +63,26 @@ import kotlin.reflect.*
63
63
* Accessing the fields of this class is not thread-safe.
64
64
* Make sure to apply proper synchronization if you are using a single instance from multiple threads.
65
65
*/
66
- public class ValueBag internal constructor(internal val contents : ValueBagContents = ValueBagContents ()) {
66
+ public class DateTimeComponents internal constructor(internal val contents : DateTimeComponentsContents = DateTimeComponentsContents ()) {
67
67
public companion object {
68
68
/* *
69
- * Creates a [DateTimeFormat] for [ValueBag ] values using [DateTimeFormatBuilder.WithDateTimeComponents].
69
+ * Creates a [DateTimeFormat] for [DateTimeComponents ] values using [DateTimeFormatBuilder.WithDateTimeComponents].
70
70
*
71
- * There is a collection of predefined formats in [ValueBag .Formats].
71
+ * There is a collection of predefined formats in [DateTimeComponents .Formats].
72
72
*/
73
73
@Suppress(" FunctionName" )
74
- public fun Format (block : DateTimeFormatBuilder .WithDateTimeComponents .() -> Unit ): DateTimeFormat <ValueBag > {
75
- val builder = ValueBagFormat .Builder (AppendableFormatStructure ())
74
+ public fun Format (block : DateTimeFormatBuilder .WithDateTimeComponents .() -> Unit ): DateTimeFormat <DateTimeComponents > {
75
+ val builder = DateTimeComponentsFormat .Builder (AppendableFormatStructure ())
76
76
block(builder)
77
- return ValueBagFormat (builder.build())
77
+ return DateTimeComponentsFormat (builder.build())
78
78
}
79
79
}
80
80
81
81
/* *
82
- * The entry point for parsing and formatting [ValueBag ] values.
82
+ * The entry point for parsing and formatting [DateTimeComponents ] values.
83
83
*
84
- * If predefined formats are not sufficient, use [ValueBag .Format] to create a custom
85
- * [kotlinx.datetime.format.DateTimeFormat] for [ValueBag ] values.
84
+ * If predefined formats are not sufficient, use [DateTimeComponents .Format] to create a custom
85
+ * [kotlinx.datetime.format.DateTimeFormat] for [DateTimeComponents ] values.
86
86
*/
87
87
public object Formats {
88
88
@@ -94,11 +94,11 @@ public class ValueBag internal constructor(internal val contents: ValueBagConten
94
94
* * `2020-01-01T23:59:59+01`
95
95
* * `2020-01-01T23:59:59Z`
96
96
*
97
- * This format uses the local date, local time, and UTC offset fields of [ValueBag ].
97
+ * This format uses the local date, local time, and UTC offset fields of [DateTimeComponents ].
98
98
*
99
99
* See ISO-8601-1:2019, 5.4.2.1b), excluding the format without the offset.
100
100
*/
101
- public val ISO_DATE_TIME_OFFSET : DateTimeFormat <ValueBag > = Format {
101
+ public val ISO_DATE_TIME_OFFSET : DateTimeFormat <DateTimeComponents > = Format {
102
102
appendDate(ISO_DATE )
103
103
alternativeParsing({
104
104
char(' t' )
@@ -132,7 +132,7 @@ public class ValueBag internal constructor(internal val contents: ValueBagConten
132
132
*
133
133
* North American and military time zone abbreviations are not supported.
134
134
*/
135
- public val RFC_1123 : DateTimeFormat <ValueBag > = Format {
135
+ public val RFC_1123 : DateTimeFormat <DateTimeComponents > = Format {
136
136
alternativeParsing({
137
137
// the day of week may be missing
138
138
}) {
@@ -166,23 +166,23 @@ public class ValueBag internal constructor(internal val contents: ValueBagConten
166
166
}
167
167
168
168
/* *
169
- * Writes the contents of the specified [localTime] to this [ValueBag ].
169
+ * Writes the contents of the specified [localTime] to this [DateTimeComponents ].
170
170
* The [localTime] is written to the [hour], [minute], [second] and [nanosecond] fields.
171
171
*
172
172
* If any of the fields are already set, they will be overwritten.
173
173
*/
174
174
public fun populateFrom (localTime : LocalTime ) { contents.time.populateFrom(localTime) }
175
175
176
176
/* *
177
- * Writes the contents of the specified [localDate] to this [ValueBag ].
177
+ * Writes the contents of the specified [localDate] to this [DateTimeComponents ].
178
178
* The [localDate] is written to the [year], [monthNumber] and [dayOfMonth] fields.
179
179
*
180
180
* If any of the fields are already set, they will be overwritten.
181
181
*/
182
182
public fun populateFrom (localDate : LocalDate ) { contents.date.populateFrom(localDate) }
183
183
184
184
/* *
185
- * Writes the contents of the specified [localDateTime] to this [ValueBag ].
185
+ * Writes the contents of the specified [localDateTime] to this [DateTimeComponents ].
186
186
* The [localDateTime] is written to the
187
187
* [year], [monthNumber], [dayOfMonth], [hour], [minute], [second] and [nanosecond] fields.
188
188
*
@@ -194,15 +194,15 @@ public class ValueBag internal constructor(internal val contents: ValueBagConten
194
194
}
195
195
196
196
/* *
197
- * Writes the contents of the specified [utcOffset] to this [ValueBag ].
197
+ * Writes the contents of the specified [utcOffset] to this [DateTimeComponents ].
198
198
* The [utcOffset] is written to the [offsetTotalHours], [offsetMinutesOfHour] and [offsetSecondsOfMinute] fields.
199
199
*
200
200
* If any of the fields are already set, they will be overwritten.
201
201
*/
202
202
public fun populateFrom (utcOffset : UtcOffset ) { contents.offset.populateFrom(utcOffset) }
203
203
204
204
/* *
205
- * Writes the contents of the specified [instant] to this [ValueBag ].
205
+ * Writes the contents of the specified [instant] to this [DateTimeComponents ].
206
206
*
207
207
* This method is almost always equivalent to the following code:
208
208
* ```
@@ -288,7 +288,7 @@ public class ValueBag internal constructor(internal val contents: ValueBagConten
288
288
public var timeZoneId: String? by contents::timeZoneId
289
289
290
290
/* *
291
- * Builds a [UtcOffset] from the fields in this [ValueBag ].
291
+ * Builds a [UtcOffset] from the fields in this [DateTimeComponents ].
292
292
*
293
293
* This method uses the following fields:
294
294
* * [offsetTotalHours] (default value is 0)
@@ -300,7 +300,7 @@ public class ValueBag internal constructor(internal val contents: ValueBagConten
300
300
public fun toUtcOffset (): UtcOffset = contents.offset.toUtcOffset()
301
301
302
302
/* *
303
- * Builds a [LocalDate] from the fields in this [ValueBag ].
303
+ * Builds a [LocalDate] from the fields in this [DateTimeComponents ].
304
304
*
305
305
* This method uses the following fields:
306
306
* * [year]
@@ -314,7 +314,7 @@ public class ValueBag internal constructor(internal val contents: ValueBagConten
314
314
public fun toLocalDate (): LocalDate = contents.date.toLocalDate()
315
315
316
316
/* *
317
- * Builds a [LocalTime] from the fields in this [ValueBag ].
317
+ * Builds a [LocalTime] from the fields in this [DateTimeComponents ].
318
318
*
319
319
* This method uses the following fields:
320
320
* * [hour]
@@ -327,7 +327,7 @@ public class ValueBag internal constructor(internal val contents: ValueBagConten
327
327
public fun toLocalTime (): LocalTime = contents.time.toLocalTime()
328
328
329
329
/* *
330
- * Builds a [LocalDateTime] from the fields in this [ValueBag ].
330
+ * Builds a [LocalDateTime] from the fields in this [DateTimeComponents ].
331
331
*
332
332
* This method uses the following fields:
333
333
* * [year]
@@ -349,7 +349,7 @@ public class ValueBag internal constructor(internal val contents: ValueBagConten
349
349
public fun toLocalDateTime (): LocalDateTime = toLocalDate().atTime(toLocalTime())
350
350
351
351
/* *
352
- * Builds an [Instant] from the fields in this [ValueBag ].
352
+ * Builds an [Instant] from the fields in this [DateTimeComponents ].
353
353
*
354
354
* Uses the fields required for [toLocalDateTime] and [toUtcOffset].
355
355
*
@@ -383,69 +383,69 @@ public class ValueBag internal constructor(internal val contents: ValueBagConten
383
383
}
384
384
385
385
/* *
386
- * Uses this format to format an unstructured [ValueBag ].
386
+ * Uses this format to format an unstructured [DateTimeComponents ].
387
387
*
388
- * [block] is called on an empty [ValueBag ] before formatting.
388
+ * [block] is called on an empty [DateTimeComponents ] before formatting.
389
389
*
390
390
* Example:
391
391
* ```
392
392
* // Mon, 16 Mar 2020 23:59:59 +0300
393
- * ValueBag .Format.RFC_1123.format {
393
+ * DateTimeComponents .Format.RFC_1123.format {
394
394
* populateFrom(LocalDateTime(2020, 3, 16, 23, 59, 59, 999_999_999))
395
395
* populateFrom(UtcOffset(hours = 3))
396
396
* }
397
397
* ```
398
398
*/
399
- public fun DateTimeFormat<ValueBag >.format (block : ValueBag .() -> Unit ): String = format(ValueBag ().apply { block() })
399
+ public fun DateTimeFormat<DateTimeComponents >.format (block : DateTimeComponents .() -> Unit ): String = format(DateTimeComponents ().apply { block() })
400
400
401
401
/* *
402
- * Parses a [ValueBag ] from [input] using the given format.
402
+ * Parses a [DateTimeComponents ] from [input] using the given format.
403
403
* Equivalent to calling [DateTimeFormat.parse] on [format] with [input].
404
404
*
405
- * [ValueBag ] does not perform any validation, so even invalid values may be parsed successfully if the string pattern
405
+ * [DateTimeComponents ] does not perform any validation, so even invalid values may be parsed successfully if the string pattern
406
406
* matches.
407
407
*
408
408
* @throws IllegalArgumentException if the text does not match the format.
409
409
*/
410
- public fun ValueBag .Companion.parse (input : CharSequence , format : DateTimeFormat <ValueBag >): ValueBag =
410
+ public fun DateTimeComponents .Companion.parse (input : CharSequence , format : DateTimeFormat <DateTimeComponents >): DateTimeComponents =
411
411
format.parse(input)
412
412
413
- internal class ValueBagContents internal constructor(
413
+ internal class DateTimeComponentsContents internal constructor(
414
414
val date : IncompleteLocalDate = IncompleteLocalDate (),
415
415
val time : IncompleteLocalTime = IncompleteLocalTime (),
416
416
val offset : IncompleteUtcOffset = IncompleteUtcOffset (),
417
417
var timeZoneId : String? = null ,
418
418
) : DateFieldContainer by date, TimeFieldContainer by time, UtcOffsetFieldContainer by offset,
419
- DateTimeFieldContainer , Copyable <ValueBagContents > {
420
- override fun copy (): ValueBagContents = ValueBagContents (date.copy(), time.copy(), offset.copy(), timeZoneId)
419
+ DateTimeFieldContainer , Copyable <DateTimeComponentsContents > {
420
+ override fun copy (): DateTimeComponentsContents = DateTimeComponentsContents (date.copy(), time.copy(), offset.copy(), timeZoneId)
421
421
422
422
override fun equals (other : Any? ): Boolean =
423
- other is ValueBagContents && other.date == date && other.time == time &&
423
+ other is DateTimeComponentsContents && other.date == date && other.time == time &&
424
424
other.offset == offset && other.timeZoneId == timeZoneId
425
425
426
426
override fun hashCode (): Int =
427
427
date.hashCode() xor time.hashCode() xor offset.hashCode() xor (timeZoneId?.hashCode() ? : 0 )
428
428
}
429
429
430
430
@SharedImmutable
431
- internal val timeZoneField = GenericFieldSpec (ValueBagContents ::timeZoneId)
431
+ internal val timeZoneField = GenericFieldSpec (DateTimeComponentsContents ::timeZoneId)
432
432
433
433
internal class TimeZoneIdDirective (knownZones : Set <String >) :
434
- StringFieldFormatDirective <ValueBagContents >(timeZoneField, knownZones) {
434
+ StringFieldFormatDirective <DateTimeComponentsContents >(timeZoneField, knownZones) {
435
435
436
436
override val builderRepresentation: String = " ${DateTimeFormatBuilder .WithDateTimeComponents ::appendTimeZoneId.name} ()"
437
437
}
438
438
439
- internal class ValueBagFormat (val actualFormat : StringFormat <ValueBagContents >) :
440
- AbstractDateTimeFormat <ValueBag , ValueBagContents >(actualFormat) {
441
- override fun intermediateFromValue (value : ValueBag ): ValueBagContents = value.contents
439
+ internal class DateTimeComponentsFormat (val actualFormat : StringFormat <DateTimeComponentsContents >) :
440
+ AbstractDateTimeFormat <DateTimeComponents , DateTimeComponentsContents >(actualFormat) {
441
+ override fun intermediateFromValue (value : DateTimeComponents ): DateTimeComponentsContents = value.contents
442
442
443
- override fun valueFromIntermediate (intermediate : ValueBagContents ): ValueBag = ValueBag (intermediate)
443
+ override fun valueFromIntermediate (intermediate : DateTimeComponentsContents ): DateTimeComponents = DateTimeComponents (intermediate)
444
444
445
- override fun newIntermediate (): ValueBagContents = ValueBagContents ()
445
+ override fun newIntermediate (): DateTimeComponentsContents = DateTimeComponentsContents ()
446
446
447
- class Builder (override val actualBuilder : AppendableFormatStructure <ValueBagContents >) :
448
- AbstractDateTimeFormatBuilder <ValueBagContents , Builder >, DateTimeFormatBuilder .WithDateTimeComponents {
447
+ class Builder (override val actualBuilder : AppendableFormatStructure <DateTimeComponentsContents >) :
448
+ AbstractDateTimeFormatBuilder <DateTimeComponentsContents , Builder >, DateTimeFormatBuilder .WithDateTimeComponents {
449
449
override fun appendYear (padding : Padding ) =
450
450
actualBuilder.add(BasicFormatStructure (YearDirective (padding)))
451
451
@@ -512,8 +512,8 @@ internal class ValueBagFormat(val actualFormat: StringFormat<ValueBagContents>)
512
512
}
513
513
514
514
@Suppress(" NO_ELSE_IN_WHEN" )
515
- override fun appendValueBag (format : DateTimeFormat <ValueBag >) = when (format) {
516
- is ValueBagFormat -> actualBuilder.add(format.actualFormat.directives)
515
+ override fun appendDateTimeComponents (format : DateTimeFormat <DateTimeComponents >) = when (format) {
516
+ is DateTimeComponentsFormat -> actualBuilder.add(format.actualFormat.directives)
517
517
}
518
518
519
519
override fun createEmpty (): Builder = Builder (AppendableFormatStructure ())
0 commit comments