Skip to content

Duplicate monthNumber in format factory after migrate to 0.7.1 #566

@TakanashiHitomi

Description

@TakanashiHitomi

Hello,
After I migrate lib from 0.6.2 to 0.7.1, I found some crash in my application.
Seems there are 2 monthNumber in my format, but there should be only one.
And this crash is not shown every time, just several Android 11 and 8 devices.

I test these codes in emulator for several times, but none of them show this bug.
Please notify me if anything wrong in my code.

Thanks!

Fatal Exception: java.lang.IllegalArgumentException: At most one variable-length numeric field in a row is allowed, but got several: [monthNumber, monthNumber]. Parsing is undefined: for example, with variable-length month number and variable-length day of month, '111' can be parsed as Jan 11th or Nov 1st.
       at kotlinx.datetime.internal.format.parser.NumberSpanParserOperation.<init>(ParserOperation.kt:56)
       at kotlinx.datetime.internal.format.parser.ParserKt.concat$simplify(Parser.kt:96)
       at kotlinx.datetime.internal.format.parser.ParserKt.concat(Parser.kt:125)
       at kotlinx.datetime.internal.format.ConcatenatedFormatStructure.parser(FormatStructure.kt:233)
       at kotlinx.datetime.internal.format.CachedFormatStructure.<init>(FormatStructure.kt:248)
       at kotlinx.datetime.format.AbstractDateTimeFormatBuilder.build(DateTimeFormatBuilder.kt:476)
       at kotlinx.datetime.format.DateTimeComponents$Companion.Format(DateTimeComponents.kt:59)
       at [my.package.name].TimeFormatKt.dateTimeFormat(TimeFormat.kt:50)
// TimeFormat.kt
fun Instant.dateTimeFormat(): String {
    val targetDateTime = this.toLocalDateTime(TimeZone.currentSystemDefault())
    val currentDateTime = Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault())

    val isSameYear = targetDateTime.year == currentDateTime.year
    val isSameDate = isSameYear &&
            targetDateTime.monthNumber == currentDateTime.monthNumber &&
            targetDateTime.dayOfMonth == currentDateTime.dayOfMonth

    // crash report here
    val format = DateTimeComponents.Format {
        if (isSameDate) {
            char(' ')
            hour(); char(':'); minute()
        } else {
            monthNumber(); char('/'); dayOfMonth(); if (isSameYear.not()) {
                char('/'); year()
            }
        }
    }

    return format(format)
}

// Display.kt
Text(
    text = item.lastMessageDate?.dateTimeFormat() ?: "",
    style = MaterialTheme.typography.bodySmall,
    color = MaterialTheme.colorScheme.descriptionText,
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions