Git Product home page Git Product logo

Comments (10)

dmitshur avatar dmitshur commented on June 26, 2024

How would you specify it?

Unless you have a good proposal to do this, it seems like it's be shorter to simply implement what you want yourself in your project, custom for your needs. As an example, I wanted something like humanize.Time except to have it always say "less than a minute ago" for time spans of less than a minute. I just wrote it, the func was 4 lines of code.

from go-humanize.

dmitshur avatar dmitshur commented on June 26, 2024

Also, have you seen humanize.RelTime? It lets you specify custom labels for times that earlier and later.

from go-humanize.

hurf avatar hurf commented on June 26, 2024

@shurcooL - Thanks! Accutally I want something like '12s ago' or '21 days'(I don't want to use week) . I've wrote the code and will submit a PR for it.

from go-humanize.

tj avatar tj commented on June 26, 2024

I'd like this as well, seems to be a common mistake that libs like this append "ago".

EDIT: just saw the RelTime comment haha. That works. Wouldn't mind having humanize.Duration(n) with no "ago", then you can easily have Resolved in %s -> Resolved in 5 minutes etc, letting the dev append whatever is appropriate for their domain.

from go-humanize.

dmitshur avatar dmitshur commented on June 26, 2024

@tj, humanize.Duration(n) sounds reasonable to me at a surface glance. But please run that proposal by @dustin. It's really hard to control feature-creep in the API of this library. We still haven't even merged #26 after all this time.

from go-humanize.

tj avatar tj commented on June 26, 2024

Hmm actually on second thought I do think it would be nice, I just got bit by it adding the opinionated "now" for tiny deltas.

from go-humanize.

dustin avatar dustin commented on June 26, 2024

I'll send up a PR in a few minutes.

On Wed, Apr 20, 2016 at 5:21 PM TJ Holowaychuk [email protected]
wrote:

Hmm actually on second thought I do think it would be nice, I just got bit
by it adding the opinionated "now" for tiny deltas


You are receiving this because you were mentioned.

Reply to this email directly or view it on GitHub
#25 (comment)

from go-humanize.

dustin avatar dustin commented on June 26, 2024

Sorry, took me a bit more than a couple of minutes to get off the bus, but I think this may do what you want.

In particular:

  1. Arbitrary ranges.
  2. Nanosecond precision.

The first one was I think what was being asked for in particular.

The second is required because the way it was implemented rounded everything to seconds. The general vision of RelTime was to show time for humans on a page where things are meant to be presented in human scales. time.Duration.String() seems well suited for the rest of these cases, but there are plenty of reasons to allow people to specify their own arbitrary boundaries.

from go-humanize.

tj avatar tj commented on June 26, 2024

Understandable. I was thinking it could be something like:

func Time(t time.Time) string {
    if t.Before(time.Now()) {
        return Duration(time.Now().Sub(t)) + " ago"
    } else {
        return Duration(t.Sub(time.Now())) + " from now"
    }
}

Then if you have a different domain like mine where you'd just like to say "Resolved in X minutes" or similar it's no problem, no strings.Trim() required with RelTime.

from go-humanize.

tj avatar tj commented on June 26, 2024

I guess for my case rounding up or down entire hours etc is not precise enough anyway haha, but hey 😄

from go-humanize.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.