https://kotlinlang.org logo
y

Yan Pujante

03/05/2021, 5:49 PM
I am using the
kotlin.time.Duration
concept in my code and see that I can convert to
String
pretty easily. I am not seeing a way to go the other way around, which makes it quite hard to use it as a field in a form or to save/retrieve in preferences... Am I missing something?
y

Youssef Shoaib [MOD]

03/05/2021, 6:03 PM
your best bet is to use something like
toLongMilliseconds()
, save that, and then when you retrieve it do a
.milliseconds
on that long value to retrieve it as a milliseconds value.
y

Yan Pujante

03/05/2021, 6:06 PM
that works to save/read.. but that is super user unfriendly (please convert 1h15m in milliseconds before entering the value...)
y

Youssef Shoaib [MOD]

03/05/2021, 6:30 PM
Oh so you mean like the pretty
toString
function? hmmm one second I'm gonna take a look at that source code and see what can be done
For user input you should probably use something like a DateTimePicker like the Material components one. I'm saying this because looking at the toString code it's probably too complicated and error-prone to use for user input, especially because user input usually just cannot be trusted.
y

Yan Pujante

03/05/2021, 6:36 PM
ok thank you. I will take a look at this
i

ilya.gorbunov

03/05/2021, 7:12 PM
We have
Duration.parse
function in mind, so I've opened https://youtrack.jetbrains.com/issue/KT-45325 to track this feature request.
👍 1
y

Yan Pujante

03/05/2021, 7:13 PM
Sounds great thank you
14 Views