Hi everyone :slightly_smiling_face: Playing with t...
# kotlinx-datetime
j
Hi everyone 🙂 Playing with the lib, trying some very basic formatting:
Copy code
val formattedArrival = arrival
    .toLocalDateTime(TimeZone.currentSystemDefault())
    .format(LocalDateTime.Format { byUnicodePattern("HH:mm") })
We're fine with hardcoded "HH:mm" for now. But there's a warning which I don't understand : `Using format strings is discouraged. If the format string is a constant, the corresponding builder-style Kotlin code can be obtained by calling
DateTimeFormat.formatAsKotlinBuilderDsl
on the resulting format.` They only way I've been able to use the mentionned DSL is that:
Copy code
val format = LocalDateTime.Format { byUnicodePattern("hh:mm") }
val test = DateTimeFormat.formatAsKotlinBuilderDsl(format)
But 1) the warning is still there 2) that doesn't really help me understand what the warning is supposed to mean. Any help? 🙂
d
The warning wants you to print
DateTimeFormat.formatAsKotlinBuilderDsl(format)
and copy-paste the result into your code.
Copy code
import kotlinx.datetime.*
import kotlinx.datetime.format.*

fun main() {
    val format = LocalDateTime.Format { byUnicodePattern("HH:mm") }
	println(DateTimeFormat.formatAsKotlinBuilderDsl(format))
}
prints
Copy code
hour()
char(':')
minute()
so your code becomes
Copy code
LocalDateTime.Format { hour(); char(':'); minute() }
j
oooooohhhhhhhhh Ok I get it, makes sense (even though I would never have guessed without help 😅) Thank you! 🙏
d
Do you maybe have some suggestions as to which wording would be less confusing?
j
I've been thinking about that but it's kinda hard now that I understand what the suggestion means haha 😅 Like "the wording's fine, wth was I having an issue with it?" 😅 Human brain... I guess having the sample code with the println somewhere in the documentation would have instantly solved it for me 🙂
🙏 1
o
This seems hard to express in abstract wording. So what about:
Copy code
Using format strings like "HH:mm" is discouraged. Please see <DOC REFERENCE>.
With
<DOC REFERENCE>
stating: Consider using the DSL instead of a format string like "HH:mm". The following example prints the corresponding DSL invocations for a given format string constant:
Copy code
import kotlinx.datetime.*
import kotlinx.datetime.format.*

fun main() {
    val format = LocalDateTime.Format { byUnicodePattern("HH:mm") }
	println(DateTimeFormat.formatAsKotlinBuilderDsl(format))
}
The output will be:
Copy code
hour()
char(':')
minute()
You can use this to replace
byUnicodePattern("HH:mm")
.