closure-library icon indicating copy to clipboard operation
closure-library copied to clipboard

Allow goog.date.relative.format to format relative dates with second precision

Open danielcompton opened this issue 9 years ago • 3 comments
trafficstars

In our application it would be helpful to get a relative date format (edit: from goog.date.relative.format) with second precision, e.g. "in 5 seconds", "35 seconds ago", e.t.c.

Is it likely that a PR for this functionality would be accepted? I'm happy to put it together if the idea is acceptable. One thing I'm not too sure about is backwards compatibility. Is it acceptable for the return value of times less than a minute from now to change from (for example) "1 minute ago" to "37 seconds ago"?

danielcompton avatar Sep 08 '16 05:09 danielcompton

@fabalbon Don't we already have something like this?

joeltine avatar Sep 21 '16 22:09 joeltine

@joeltine We have closure/goog/date/relative.js, but it has minute precision. If I'm not mistaken, what @danielcompton is proposing is to add second precision to this API. Regarding backwards compatibility I think it might be a problem because users might be expecting minute precision so I wouldn't change the default behavior.

fabalbon avatar Sep 22 '16 18:09 fabalbon

If I'm not mistaken, what @danielcompton is proposing is to add second precision to this API.

Yep, that's correct.

Regarding backwards compatibility I think it might be a problem because users might be expecting minute precision so I wouldn't change the default behavior.

I can see two possibilities to preserve backwards compatibility:

  1. Provide an extra parameter to goog.date.relative{format, formatDay, formatPast, ...}, as to be consistent, all of the relative formatters should be able to return the same format.
  2. Make setTimeDeltaFormatter a publicly usable function and let users override it. However this doesn't feel like a very good approach.
  3. Add another function to toggle if relative date formats include seconds.

I'm happy to put together a PR with some guidance on which option (or another) would be preferred. Not sure if I'm in love with any of the options. Instantiating and using a formatter feels like a better option. There's a comment from 2013 suggesting that this be moved to goog.i18n.DateTimeFormat once a new version of CLDR comes out.

danielcompton avatar Sep 22 '16 23:09 danielcompton