Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider adding parsing and formatting support for kotlin.time.Duration #348

Open
dkhalanskyjb opened this issue Feb 29, 2024 · 5 comments
Labels
formatters Related to parsing and formatting

Comments

@dkhalanskyjb
Copy link
Collaborator

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.

@dkhalanskyjb dkhalanskyjb added the formatters Related to parsing and formatting label Feb 29, 2024
@mgroth0
Copy link

mgroth0 commented Jul 3, 2024

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.

@dkhalanskyjb
Copy link
Collaborator Author

@mgroth0, thanks! Could you share some examples of the Duration formats that you need?

@mgroth0
Copy link

mgroth0 commented Jul 8, 2024

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).

  1. I disallow negative durations
  2. For zero, I return "0".padStart(N)
  3. Next, I dermine the largest unit in which the value is at least 1
  4. Based on the largest unit, I branch out into different logics
    • MICROSECONDS: "${inWholeMicroseconds}µs".padStart(N)
    • MILLISECONDS: "${inWholeMilliseconds}ms.padStart(N)"
    • SECONDS: "${toDouble(SECONDS).toStringRoundedToDecimalWithPaddedZeros(3)}.padStart(N)"
    • MINUTES: "${minutesPart.toString().padStart(2)}m ${secondsPart.toString().padStart(2)}s ${ millisecondsPart.toString().padStart(3) }ms".padStart(N)
    • HOURS: "${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.

@mgroth0
Copy link

mgroth0 commented Jul 8, 2024

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.

@dkhalanskyjb
Copy link
Collaborator Author

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}.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
formatters Related to parsing and formatting
Projects
None yet
Development

No branches or pull requests

2 participants