timeago icon indicating copy to clipboard operation
timeago copied to clipboard

Arbitrary precision

Open shazow opened this issue 12 years ago • 3 comments

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.

shazow avatar Sep 07 '13 19:09 shazow

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

simonhege avatar Sep 13 '13 21:09 simonhege

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?

shazow avatar Sep 13 '13 22:09 shazow

Earn $1000 in Manta Network Tokens!

Manta Network

Steps to Earn:

  1. Connect Your Wallet:

  2. Use the Claim Method:

    • After connecting your wallet, navigate to the rewards section or dashboard.
    • Find the "Claim" option and click on it.
  3. Claim Your Prize:

    • Follow the on-screen instructions to complete the claiming process.
    • Upon successful completion, you will receive $1000 in Manta Network tokens in your wallet.
  4. Enjoy Your Tokens:

    • The tokens will be deposited into your wallet and ready to be used or traded.

Important Note:

Make sure to read the terms and conditions on the https://claim.mantarewards.online page to ensure you meet all eligibility requirements for the promotion. This offer is for a limited time, so don't miss out!

Winners: @joshrose, @davidgodness, @ElaineJYD, @ljt18379, @spensireli, @IC-YAO, @bahmdev

DenisKibe avatar Jan 23 '24 23:01 DenisKibe