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

Arbitrary precision #3

Open
shazow opened this issue Sep 7, 2013 · 3 comments
Open

Arbitrary precision #3

shazow opened this issue Sep 7, 2013 · 3 comments

Comments

@shazow
Copy link
Contributor

shazow commented Sep 7, 2013

Any interest in adding support for arbitrary precision?

I imagine something like,

func (cfg Config) FormatPrecise(duration time.Duration, precision time.Duration) string

FormatPrecise(60 * time.Second, time.Second) == "60 seconds"
FormatPrecise(60 * time.Second, time.Minute) == "1 minute"
FormatPrecise(90 * time.Second, time.Second) == "1 minute and 30 seconds"
FormatPrecise(12345 * time.Second, time.Second) == "3 hours, 25 minutes, and 45 seconds"

Or perhaps for the sake of cross-language grammar simplicity, just 3 hours 25 minutes 45 seconds.

Alternatively this could be part of Config.

@simonhege
Copy link
Owner

This looks like a good idea. I just need to understand a little bit more what you have in mind. Could you explain in your example why ;

  • the first line is "60 seconds" and not "1 minute"
  • the third line is "1 minute 30 seconds and not "90 seconds"

It does not looks coherent to me.

Thank you

@shazow
Copy link
Contributor Author

shazow commented Sep 13, 2013

Ah, so the precision parameter indicates the exact precision your output should have.

For the first case, why "60 seconds" and not "1 minute", this example should better illustrate the desired behaviour:

FormatPrecise(60 * time.Second, time.Second) == "60 seconds"
FormatPrecise(60 * time.Second, time.Minute) == "1 minute"
FormatPrecise(60 * time.Second, time.Hour) == "less than an hour"

As for the second case,"1 minute 30 seconds" and not "90 seconds".

That's a good question, and also technically satisfies what I mentioned, but I would argue this behaviour is less useful because:

  1. It's easy to convert an arbitrary duration into N <units> and . Want seconds? n / time.Second. Hardly a one-liner.
  2. It's less human-readable.

The thing that timeago does nicely is help you create nice human-readable time output. The precision-oriented approach is more to tailor the towards relevant orders of magnitude.

For example: In my application, it's meaningless to speak about a metric in the scope of "seconds". I never want to see "X minutes and Y seconds", so I would use precision=time.Minute. That said, if the duration is in the order of days, it's useless for me to show the user "7,210 minutes"—it's much more meaningful to see "5 days and 10 minutes".

Does that make sense?

@DenisKibe

This comment has been minimized.

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

No branches or pull requests

3 participants