@@ -55,6 +55,12 @@ public class MonthNames(
5555 }
5656}
5757
58+ internal fun MonthNames.toKotlinCode (): String = when (this .names) {
59+ MonthNames .ENGLISH_FULL .names -> " MonthNames.${DayOfWeekNames .Companion ::ENGLISH_FULL .name} "
60+ MonthNames .ENGLISH_ABBREVIATED .names -> " MonthNames.${DayOfWeekNames .Companion ::ENGLISH_ABBREVIATED .name} "
61+ else -> names.joinToString(" , " , " MonthNames(" , " )" , transform = String ::toKotlinCode)
62+ }
63+
5864/* *
5965 * A description of how day of week names are formatted.
6066 */
@@ -103,6 +109,12 @@ public class DayOfWeekNames(
103109 }
104110}
105111
112+ internal fun DayOfWeekNames.toKotlinCode (): String = when (this .names) {
113+ DayOfWeekNames .ENGLISH_FULL .names -> " DayOfWeekNames.${DayOfWeekNames .Companion ::ENGLISH_FULL .name} "
114+ DayOfWeekNames .ENGLISH_ABBREVIATED .names -> " DayOfWeekNames.${DayOfWeekNames .Companion ::ENGLISH_ABBREVIATED .name} "
115+ else -> names.joinToString(" , " , " DayOfWeekNames(" , " )" , transform = String ::toKotlinCode)
116+ }
117+
106118internal fun <T > requireParsedField (field : T ? , name : String ): T {
107119 if (field == null ) {
108120 throw DateTimeFormatException (" Can not create a $name from the given input: the field $name is missing" )
@@ -178,6 +190,16 @@ private class YearDirective(private val padding: Padding, private val isYearOfEr
178190 spacePadding = padding.spaces(4 ),
179191 outputPlusOnExceededWidth = 4 ,
180192 ) {
193+ override val builderRepresentation: String
194+ get() = when (padding) {
195+ Padding .ZERO -> " ${DateTimeFormatBuilder .WithDate ::year.name} ()"
196+ else -> " ${DateTimeFormatBuilder .WithDate ::year.name} (${padding.toKotlinCode()} )"
197+ }.let {
198+ if (isYearOfEra) {
199+ it + YEAR_OF_ERA_COMMENT
200+ } else it
201+ }
202+
181203 override fun equals (other : Any? ): Boolean =
182204 other is YearDirective && padding == other.padding && isYearOfEra == other.isYearOfEra
183205
@@ -190,12 +212,23 @@ private class ReducedYearDirective(val base: Int, private val isYearOfEra: Boole
190212 digits = 2 ,
191213 base = base,
192214 ) {
215+ override val builderRepresentation: String
216+ get() =
217+ " ${DateTimeFormatBuilder .WithDate ::yearTwoDigits.name} ($base )" .let {
218+ if (isYearOfEra) {
219+ it + YEAR_OF_ERA_COMMENT
220+ } else it
221+ }
222+
193223 override fun equals (other : Any? ): Boolean =
194224 other is ReducedYearDirective && base == other.base && isYearOfEra == other.isYearOfEra
195225
196226 override fun hashCode (): Int = base.hashCode() * 31 + isYearOfEra.hashCode()
197227}
198228
229+ private const val YEAR_OF_ERA_COMMENT =
230+ " /** 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. */"
231+
199232/* *
200233 * A special directive for year-of-era that behaves equivalently to [DateTimeFormatBuilder.WithDate.year].
201234 * This is the result of calling [byUnicodePattern] on a pattern that uses the ubiquitous "y" symbol.
@@ -232,12 +265,22 @@ private class MonthDirective(private val padding: Padding) :
232265 minDigits = padding.minDigits(2 ),
233266 spacePadding = padding.spaces(2 ),
234267 ) {
268+ override val builderRepresentation: String
269+ get() = when (padding) {
270+ Padding .ZERO -> " ${DateTimeFormatBuilder .WithDate ::monthNumber.name} ()"
271+ else -> " ${DateTimeFormatBuilder .WithDate ::monthNumber.name} (${padding.toKotlinCode()} )"
272+ }
273+
235274 override fun equals (other : Any? ): Boolean = other is MonthDirective && padding == other.padding
236275 override fun hashCode (): Int = padding.hashCode()
237276}
238277
239278private class MonthNameDirective (private val names : MonthNames ) :
240279 NamedUnsignedIntFieldFormatDirective <DateFieldContainer >(DateFields .month, names.names, " monthName" ) {
280+ override val builderRepresentation: String
281+ get() =
282+ " ${DateTimeFormatBuilder .WithDate ::monthName.name} (${names.toKotlinCode()} )"
283+
241284 override fun equals (other : Any? ): Boolean = other is MonthNameDirective && names.names == other.names.names
242285 override fun hashCode (): Int = names.names.hashCode()
243286}
@@ -248,12 +291,23 @@ private class DayDirective(private val padding: Padding) :
248291 minDigits = padding.minDigits(2 ),
249292 spacePadding = padding.spaces(2 ),
250293 ) {
294+ override val builderRepresentation: String
295+ get() = when (padding) {
296+ Padding .ZERO -> " ${DateTimeFormatBuilder .WithDate ::dayOfMonth.name} ()"
297+ else -> " ${DateTimeFormatBuilder .WithDate ::dayOfMonth.name} (${padding.toKotlinCode()} )"
298+ }
299+
251300 override fun equals (other : Any? ): Boolean = other is DayDirective && padding == other.padding
252301 override fun hashCode (): Int = padding.hashCode()
253302}
254303
255304private class DayOfWeekDirective (private val names : DayOfWeekNames ) :
256305 NamedUnsignedIntFieldFormatDirective <DateFieldContainer >(DateFields .isoDayOfWeek, names.names, " dayOfWeekName" ) {
306+
307+ override val builderRepresentation: String
308+ get() =
309+ " ${DateTimeFormatBuilder .WithDate ::dayOfWeek.name} (${names.toKotlinCode()} )"
310+
257311 override fun equals (other : Any? ): Boolean = other is DayOfWeekDirective && names.names == other.names.names
258312 override fun hashCode (): Int = names.names.hashCode()
259313}
0 commit comments