Open dkhalanskyjb opened 4 months ago
I would like this. I display durations in console output, reports, and compose GUIs often in similar places to where I display dates, so it would be nice to have similar formatting options. I also could use this to make a custom parser for a duration input component in compose.
@mgroth0, thanks! Could you share some examples of the Duration
formats that you need?
Sure!
One example is for a stopwatch component, which displays the time an operation has been running. The string is guaranteed to be N characters long (I have N at 18).
"${inWholeMicroseconds}µs".padStart(N)
"${inWholeMilliseconds}ms.padStart(N)"
"${toDouble(SECONDS).toStringRoundedToDecimalWithPaddedZeros(3)}.padStart(N)"
"${minutesPart.toString().padStart(2)}m ${secondsPart.toString().padStart(2)}s ${ millisecondsPart.toString().padStart(3) }ms".padStart(N)
"${minutesPart.toString().padStart(2)}m ${secondsPart.toString().padStart(2)}s ${ millisecondsPart.toString().padStart(3) }ms".padStart(N)
It works but it also feels a little messy and I'm not sure if this is the best approach. One nice thing about having the library have first class support for this sort of thing is it helps me figure out the best way to do it.
And that would only work for formatting an existing value. Parsing would be a whole different animal and I guess it might not work with this sort of format.
Another format I have is very similar, but is designed for text to speech so it uses the full unit names instead of the abbreviations, and doesn't care about padding.
The other feature I have in mind is one that I haven't developed yet, but it would allow users to type an arbitrary duration into a text field and it would be parsed into a Duration value.
Hm. I don't think that DateTimeFormat
-style formats provided by this library are a good fit for your use case: even if we do provide an API to support the conditionals of this form, I'm not sure that the resulting code will look more concise/readable than building strings by hand.
What your code could certainly benefit from is some way to conveniently format numbers, so ${minutesPart.toString().padStart(2)}
would become something like ${minutesPart:02}
.
Even though it's not strictly part of this library, it could still benefit from
DateTimeFormat
formatting capabilities. Please share your use cases and suggestions here.