@@ -182,7 +182,7 @@ internal class IncompleteLocalDate(
182
182
" ${year ? : " ??" } -${monthNumber ? : " ??" } -${dayOfMonth ? : " ??" } (day of week is ${isoDayOfWeek ? : " ??" } )"
183
183
}
184
184
185
- private class YearDirective (private val padding : Padding ) :
185
+ private class YearDirective (private val padding : Padding , private val isYearOfEra : Boolean = false ) :
186
186
SignedIntFieldFormatDirective <DateFieldContainer >(
187
187
DateFields .year,
188
188
minDigits = padding.minDigits(4 ),
@@ -193,22 +193,62 @@ private class YearDirective(private val padding: Padding) :
193
193
override val builderRepresentation: String get() = when (padding) {
194
194
Padding .ZERO -> " ${DateTimeFormatBuilder .WithDate ::year.name} ()"
195
195
else -> " ${DateTimeFormatBuilder .WithDate ::year.name} (${padding.toKotlinCode()} )"
196
+ }.let {
197
+ if (isYearOfEra) { it + YEAR_OF_ERA_COMMENT } else it
196
198
}
197
199
198
- override fun equals (other : Any? ): Boolean = other is YearDirective && padding == other.padding
199
- override fun hashCode (): Int = padding.hashCode()
200
+ override fun equals (other : Any? ): Boolean =
201
+ other is YearDirective && padding == other.padding && isYearOfEra == other.isYearOfEra
202
+ override fun hashCode (): Int = padding.hashCode() * 31 + isYearOfEra.hashCode()
200
203
}
201
204
202
- private class ReducedYearDirective (val base : Int ) :
205
+ private class ReducedYearDirective (val base : Int , private val isYearOfEra : Boolean = false ) :
203
206
ReducedIntFieldDirective <DateFieldContainer >(
204
207
DateFields .year,
205
208
digits = 2 ,
206
209
base = base,
207
210
) {
208
- override val builderRepresentation: String get() = " ${DateTimeFormatBuilder .WithDate ::yearTwoDigits.name} ($base )"
211
+ override val builderRepresentation: String get() =
212
+ " ${DateTimeFormatBuilder .WithDate ::yearTwoDigits.name} ($base )" .let {
213
+ if (isYearOfEra) { it + YEAR_OF_ERA_COMMENT } else it
214
+ }
215
+
216
+ override fun equals (other : Any? ): Boolean =
217
+ other is ReducedYearDirective && base == other.base && isYearOfEra == other.isYearOfEra
218
+ override fun hashCode (): Int = base.hashCode() * 31 + isYearOfEra.hashCode()
219
+ }
220
+
221
+ private const val YEAR_OF_ERA_COMMENT =
222
+ " /** TODO: the original format had an `y` directive, so the behavior is different on years earlier than 1 AD. See the [kotlinx.datetime.format.byUnicodePattern] documentation for details. */"
223
+
224
+ /* *
225
+ * A special directive for year-of-era that behaves equivalently to [DateTimeFormatBuilder.WithDate.year].
226
+ * This is the result of calling [byUnicodePattern] on a pattern that uses the ubiquitous "y" symbol.
227
+ * We need a separate directive so that, when using [DateTimeFormat.formatAsKotlinBuilderDsl], we can print an
228
+ * additional comment and explain that the behavior was not preserved exactly.
229
+ */
230
+ internal fun DateTimeFormatBuilder.WithDate.yearOfEra (padding : Padding ) {
231
+ @Suppress(" NO_ELSE_IN_WHEN" )
232
+ when (this ) {
233
+ is AbstractWithDateBuilder -> addFormatStructureForDate(
234
+ BasicFormatStructure (YearDirective (padding, isYearOfEra = true ))
235
+ )
236
+ }
237
+ }
209
238
210
- override fun equals (other : Any? ): Boolean = other is ReducedYearDirective && base == other.base
211
- override fun hashCode (): Int = base.hashCode()
239
+ /* *
240
+ * A special directive for year-of-era that behaves equivalently to [DateTimeFormatBuilder.WithDate.year].
241
+ * This is the result of calling [byUnicodePattern] on a pattern that uses the ubiquitous "y" symbol.
242
+ * We need a separate directive so that, when using [DateTimeFormat.formatAsKotlinBuilderDsl], we can print an
243
+ * additional comment and explain that the behavior was not preserved exactly.
244
+ */
245
+ internal fun DateTimeFormatBuilder.WithDate.yearOfEraTwoDigits (baseYear : Int ) {
246
+ @Suppress(" NO_ELSE_IN_WHEN" )
247
+ when (this ) {
248
+ is AbstractWithDateBuilder -> addFormatStructureForDate(
249
+ BasicFormatStructure (ReducedYearDirective (baseYear, isYearOfEra = true ))
250
+ )
251
+ }
212
252
}
213
253
214
254
private class MonthDirective (private val padding : Padding ) :
0 commit comments