-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathLocalTimeFormat.kt
317 lines (268 loc) · 11.8 KB
/
LocalTimeFormat.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
* Copyright 2019-2023 JetBrains s.r.o. and contributors.
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
*/
package kotlinx.datetime.format
import kotlinx.datetime.LocalTime
import kotlinx.datetime.DateTimeFormatException
import kotlinx.datetime.internal.DecimalFraction
import kotlinx.datetime.internal.format.*
import kotlinx.datetime.internal.format.parser.Copyable
/**
* The AM/PM marker that indicates whether the hour in range `1..12` is before or after noon.
*/
public enum class AmPmMarker {
/** The time is before noon. */
AM,
/** The time is after noon. */
PM,
}
internal interface TimeFieldContainer {
var minute: Int?
var second: Int?
var nanosecond: Int?
var hour: Int?
var hourOfAmPm: Int?
var amPm: AmPmMarker?
var fractionOfSecond: DecimalFraction?
get() = nanosecond?.let { DecimalFraction(it, 9) }
set(value) {
nanosecond = value?.fractionalPartWithNDigits(9)
}
}
private object TimeFields {
val hour = UnsignedFieldSpec(PropertyAccessor(TimeFieldContainer::hour), minValue = 0, maxValue = 23)
val minute = UnsignedFieldSpec(PropertyAccessor(TimeFieldContainer::minute), minValue = 0, maxValue = 59)
val second =
UnsignedFieldSpec(PropertyAccessor(TimeFieldContainer::second), minValue = 0, maxValue = 59, defaultValue = 0)
val fractionOfSecond =
GenericFieldSpec(PropertyAccessor(TimeFieldContainer::fractionOfSecond, "nanosecond"), defaultValue = DecimalFraction(0, 9))
val amPm = GenericFieldSpec(PropertyAccessor(TimeFieldContainer::amPm))
val hourOfAmPm = UnsignedFieldSpec(PropertyAccessor(TimeFieldContainer::hourOfAmPm), minValue = 1, maxValue = 12)
}
internal class IncompleteLocalTime(
override var hour: Int? = null,
override var hourOfAmPm: Int? = null,
override var amPm: AmPmMarker? = null,
override var minute: Int? = null,
override var second: Int? = null,
override var nanosecond: Int? = null
) : TimeFieldContainer, Copyable<IncompleteLocalTime> {
fun toLocalTime(): LocalTime {
val hour: Int = hour?.let { hour ->
hourOfAmPm?.let {
require((hour + 11) % 12 + 1 == it) { "Inconsistent hour and hour-of-am-pm: hour is $hour, but hour-of-am-pm is $it" }
}
amPm?.let { amPm ->
require((amPm == AmPmMarker.PM) == (hour >= 12)) {
"Inconsistent hour and the AM/PM marker: hour is $hour, but the AM/PM marker is $amPm"
}
}
hour
} ?: hourOfAmPm?.let { hourOfAmPm ->
amPm?.let { amPm ->
hourOfAmPm.let { if (it == 12) 0 else it } + if (amPm == AmPmMarker.PM) 12 else 0
}
} ?: throw DateTimeFormatException("Incomplete time: missing hour")
return LocalTime(
hour,
requireParsedField(minute, "minute"),
second ?: 0,
nanosecond ?: 0,
)
}
fun populateFrom(localTime: LocalTime) {
hour = localTime.hour
hourOfAmPm = (localTime.hour + 11) % 12 + 1
amPm = if (localTime.hour >= 12) AmPmMarker.PM else AmPmMarker.AM
minute = localTime.minute
second = localTime.second
nanosecond = localTime.nanosecond
}
override fun copy(): IncompleteLocalTime = IncompleteLocalTime(hour, hourOfAmPm, amPm, minute, second, nanosecond)
override fun equals(other: Any?): Boolean =
other is IncompleteLocalTime && hour == other.hour && hourOfAmPm == other.hourOfAmPm && amPm == other.amPm &&
minute == other.minute && second == other.second && nanosecond == other.nanosecond
override fun hashCode(): Int =
(hour ?: 0) * 31 + (hourOfAmPm ?: 0) * 31 + (amPm?.hashCode() ?: 0) * 31 + (minute ?: 0) * 31 +
(second ?: 0) * 31 + (nanosecond ?: 0)
override fun toString(): String =
"${hour ?: "??"}:${minute ?: "??"}:${second ?: "??"}.${
nanosecond?.let { nanos ->
nanos.toString().let { it.padStart(9 - it.length, '0') }
} ?: "???"
}"
}
internal interface AbstractWithTimeBuilder : DateTimeFormatBuilder.WithTime {
fun addFormatStructureForTime(structure: FormatStructure<TimeFieldContainer>)
override fun hour(padding: Padding) = addFormatStructureForTime(BasicFormatStructure(HourDirective(padding)))
override fun amPmHour(padding: Padding) =
addFormatStructureForTime(BasicFormatStructure(AmPmHourDirective(padding)))
override fun amPmMarker(am: String, pm: String) =
addFormatStructureForTime(BasicFormatStructure(AmPmMarkerDirective(am, pm)))
override fun minute(padding: Padding) = addFormatStructureForTime(BasicFormatStructure(MinuteDirective(padding)))
override fun second(padding: Padding) = addFormatStructureForTime(BasicFormatStructure(SecondDirective(padding)))
override fun secondFraction(minLength: Int, maxLength: Int) =
addFormatStructureForTime(BasicFormatStructure(FractionalSecondDirective(minLength, maxLength)))
@Suppress("NO_ELSE_IN_WHEN")
override fun time(format: DateTimeFormat<LocalTime>) = when (format) {
is LocalTimeFormat -> addFormatStructureForTime(format.actualFormat)
}
}
private class HourDirective(private val padding: Padding) :
UnsignedIntFieldFormatDirective<TimeFieldContainer>(
TimeFields.hour,
minDigits = padding.minDigits(2),
spacePadding = padding.spaces(2)
) {
override val builderRepresentation: String
get() = when (padding) {
Padding.ZERO -> "${DateTimeFormatBuilder.WithTime::hour.name}()"
else -> "${DateTimeFormatBuilder.WithTime::hour.name}(${padding.toKotlinCode()})"
}
override fun equals(other: Any?): Boolean = other is HourDirective && padding == other.padding
override fun hashCode(): Int = padding.hashCode()
}
private class AmPmHourDirective(private val padding: Padding) :
UnsignedIntFieldFormatDirective<TimeFieldContainer>(
TimeFields.hourOfAmPm, minDigits = padding.minDigits(2),
spacePadding = padding.spaces(2)
) {
override val builderRepresentation: String
get() = when (padding) {
Padding.ZERO -> "${DateTimeFormatBuilder.WithTime::amPmHour.name}()"
else -> "${DateTimeFormatBuilder.WithTime::amPmHour.name}(${padding.toKotlinCode()})"
}
override fun equals(other: Any?): Boolean = other is AmPmHourDirective && padding == other.padding
override fun hashCode(): Int = padding.hashCode()
}
private class AmPmMarkerDirective(private val amString: String, private val pmString: String) :
NamedEnumIntFieldFormatDirective<TimeFieldContainer, AmPmMarker>(
TimeFields.amPm, mapOf(
AmPmMarker.AM to amString,
AmPmMarker.PM to pmString,
),
"AM/PM marker"
) {
override val builderRepresentation: String
get() =
"${DateTimeFormatBuilder.WithTime::amPmMarker.name}($amString, $pmString)"
override fun equals(other: Any?): Boolean =
other is AmPmMarkerDirective && amString == other.amString && pmString == other.pmString
override fun hashCode(): Int = 31 * amString.hashCode() + pmString.hashCode()
}
private class MinuteDirective(private val padding: Padding) :
UnsignedIntFieldFormatDirective<TimeFieldContainer>(
TimeFields.minute,
minDigits = padding.minDigits(2),
spacePadding = padding.spaces(2)
) {
override val builderRepresentation: String
get() = when (padding) {
Padding.ZERO -> "${DateTimeFormatBuilder.WithTime::minute.name}()"
else -> "${DateTimeFormatBuilder.WithTime::minute.name}(${padding.toKotlinCode()})"
}
override fun equals(other: Any?): Boolean = other is MinuteDirective && padding == other.padding
override fun hashCode(): Int = padding.hashCode()
}
private class SecondDirective(private val padding: Padding) :
UnsignedIntFieldFormatDirective<TimeFieldContainer>(
TimeFields.second,
minDigits = padding.minDigits(2),
spacePadding = padding.spaces(2)
) {
override val builderRepresentation: String
get() = when (padding) {
Padding.ZERO -> "${DateTimeFormatBuilder.WithTime::second.name}()"
else -> "${DateTimeFormatBuilder.WithTime::second.name}(${padding.toKotlinCode()})"
}
override fun equals(other: Any?): Boolean = other is SecondDirective && padding == other.padding
override fun hashCode(): Int = padding.hashCode()
}
internal class FractionalSecondDirective(
private val minDigits: Int,
private val maxDigits: Int,
zerosToAdd: List<Int> = NO_EXTRA_ZEROS,
) :
DecimalFractionFieldFormatDirective<TimeFieldContainer>(
TimeFields.fractionOfSecond,
minDigits,
maxDigits,
zerosToAdd
) {
override val builderRepresentation: String
get() {
val ref = "secondFraction" // can't directly reference `secondFraction` due to resolution ambiguity
// we ignore `grouping`, as it's not representable in the end users' code
return when {
minDigits == 1 && maxDigits == 9 -> "$ref()"
minDigits == 1 -> "$ref(maxLength = $maxDigits)"
maxDigits == 1 -> "$ref(minLength = $minDigits)"
maxDigits == minDigits -> "$ref($minDigits)"
else -> "$ref($minDigits, $maxDigits)"
}
}
override fun equals(other: Any?): Boolean =
other is FractionalSecondDirective && minDigits == other.minDigits && maxDigits == other.maxDigits
override fun hashCode(): Int = 31 * minDigits + maxDigits
companion object {
val NO_EXTRA_ZEROS = listOf(0, 0, 0, 0, 0, 0, 0, 0, 0)
val GROUP_BY_THREE = listOf(2, 1, 0, 2, 1, 0, 2, 1, 0)
}
}
internal class LocalTimeFormat(override val actualFormat: CachedFormatStructure<TimeFieldContainer>) :
AbstractDateTimeFormat<LocalTime, IncompleteLocalTime>() {
override fun intermediateFromValue(value: LocalTime): IncompleteLocalTime =
IncompleteLocalTime().apply { populateFrom(value) }
override fun valueFromIntermediate(intermediate: IncompleteLocalTime): LocalTime = intermediate.toLocalTime()
override val emptyIntermediate: IncompleteLocalTime get() = emptyIncompleteLocalTime
companion object {
fun build(block: DateTimeFormatBuilder.WithTime.() -> Unit): LocalTimeFormat {
val builder = Builder(AppendableFormatStructure())
builder.block()
return LocalTimeFormat(builder.build())
}
}
private class Builder(override val actualBuilder: AppendableFormatStructure<TimeFieldContainer>) :
AbstractDateTimeFormatBuilder<TimeFieldContainer, Builder>, AbstractWithTimeBuilder {
override fun addFormatStructureForTime(structure: FormatStructure<TimeFieldContainer>) {
actualBuilder.add(structure)
}
override fun createEmpty(): Builder = Builder(AppendableFormatStructure())
}
}
// these are constants so that the formats are not recreated every time they are used
internal val ISO_TIME_BASIC by lazy {
LocalTimeFormat.build {
char('T')
hour()
minute()
alternativeParsing({
// intentionally empty
}) {
second()
optional {
char('.')
secondFraction(1, 9)
}
}
}
}
internal val ISO_TIME by lazy {
LocalTimeFormat.build {
hour()
char(':')
minute()
alternativeParsing({
// intentionally empty
}) {
char(':')
second()
optional {
char('.')
secondFraction(1, 9)
}
}
}
}
private val emptyIncompleteLocalTime = IncompleteLocalTime()