format As Kotlin Builder Dsl
Produces Kotlin code that, when pasted into a Kotlin source file, creates a DateTimeFormat instance that behaves identically to format.
The typical use case for this is to create a DateTimeFormat instance using a non-idiomatic approach and then convert it to a builder DSL.
Samples
import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.test.*
fun main() {
//sampleStart
// Printing a given date-time format as a Kotlin code snippet that creates the same format
val customFormat = LocalDate.Format {
@OptIn(FormatStringsInDatetimeFormats::class)
byUnicodePattern("MM/dd yyyy")
}
val customFormatAsKotlinCode = DateTimeFormat.formatAsKotlinBuilderDsl(customFormat)
check(
customFormatAsKotlinCode.contains("""
monthNumber()
char('/')
dayOfMonth()
char(' ')
year()
""".trimIndent())
)
//sampleEnd
}