-
Notifications
You must be signed in to change notification settings - Fork 41
Feature request: Allow a custom value for now() #26
Comments
@jbaudanza I understand, but the problem is the CLDR data that we use, which only account for relative to now. Think about this, it is not the same saying |
I should clarify. I'm not suggesting that the format function should describe time duration relative to anything other than "now". I just want to change the mechanics of how the library gets the value of "now". The semantics of what "now" means should remain the same. The CLDR output should remain the same. Here are a couple contrived examples to illustrate my point:
Let me know if this makes more sense. |
ok, I understand now. For 1), I don't think As for 2), that's a genuine request, we will look at it, but it is a hard to sell IMO. |
There aren't any real perf reasons. That was a contrived scenario to help clarify my request. My original motivation was to keep my react components pure functions. |
Good, that's what I thought. To clarify more on why is this going to be a problem, is that we plan to add relative time support into ECMA-402, specifically in |
We had a similar issue to the error report example when writing functional tests for the FormatJS website. The hack we used to address this is override As for the pure functions, I see the point, but relative time is in fact temporal. A wall clock UI implemented in React would have a similar issue at some point within the component hierarchy. Could you elaborate more on your pure function motivation? |
I don't have any motivation other than the standard motivations for writing pure functions. They are generally easier to reason about and are the idiomatic way to write React components. React also has some optimization tricks that are only available if your component's render function is pure. I understand this seems like kind of an edge case. But, it does seem that the library is unnecessarily coupling a pure function that calculates string with a system call to fetch the current time. Your monkey-patching test is another great example of why these two pieces of functionality shouldn't be tightly coupled. As is the case with libraries, I suspect there will be other use cases down the road that run into friction due to this coupling. For what it's worth, twitter seemed to go in the pure direction with their relative time module. This is from the twitter-cldr-js README: var fmt = new TwitterCldr.TimespanFormatter();
var then = Math.round(new Date(2012, 1, 1, 12, 0, 0).getTime() / 1000);
var now = Math.round(Date.now() / 1000);
fmt.format(then - now); // "6 months ago" I imagine a React wall clock component would periodically fetch the current time and store it in a |
So accepting a diff would be the way to do this for sure. So we can look into if/how we might do this. It's likely to be a backwards incompatible change at this level, but we could provide both ways of providing data at the React level. |
I'm leaning towards a modified version of this suggesting. I think it will be confusing as to which order these arguments go in, so I think adding an var relativeFormat = new IntlRelativeFormat('en');
relativeFormat.format(post.date, {now: Date.now()});
The reason I think this is better than adding a constructor option is that the |
Yup, I agree. I'll throw together a PR. |
Closing now that #27 was merged. |
formatjs/formatjs#94 adds a |
Here are some possible solutions:
format(date, now)
.now
would be optional.now
option to the constructor. This could be either a Date/number constant or a function reference.My motivation for this request is to use intl-relativeformat from within a React component. I can use this from within React now, but idiomatic React components are supposed to be pure functions of
this.props
andthis.state
. Having my render function callDate.now
is somewhat of an antipattern.Let me know what you think. I'd be happy to put together a PR.
The text was updated successfully, but these errors were encountered: