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
Should 'second'
be the smallest unit accepted?
#64
Comments
Hmm. I think this makes sense. If seconds are enabled, what should be the default behavior when the incoming duration has milliseconds, microseconds, or nanoseconds? My initial thought would be that we should display as many digits as necessary without trailing zeros. So, for example, new Intl.DurationFormat("en").format({ seconds: 1, milliseconds: 120 });
// "1.12 seconds" And if new Intl.DurationFormat("en", { fractionalSecondDigits: 2 }).format({ seconds: 1, milliseconds: 101 });
// "1.10 seconds" |
Does this correspond to the locale data? I don't think so, I thought the locale data did: new Intl.DurationFormat("en").format({ seconds: 1, milliseconds: 120 });
// "1 seconds and 120 milliseconds" |
Oh, good point. Yeah, both forms (fraction digits and separate fields) are supported by locale data, since this is just a matter of interpolating units in a list. Perhaps we should just make this a toggle switch of some sort in the options bag. Thanks @justingrant and @ryzokuken for raising this issue! It had flown under my radar. |
I'm not sure there's a strong use case for spelling out ms/μs/ns units. Seems like we could simply use decimal seconds in all cases. |
Lots of use cases.
Just look on the Wikipedia pages for milliseconds, microseconds, and nanoseconds. |
Ahh, you're right. We should support single-unit cases like |
How about if subsecond digits are appended to the smallest unit on the constructor list. For example, new Intl.DurationFormat("en", {
units: ["second"]
}).format({
second: 5,
millisecond: 120
});
// 5.12 seconds
new Intl.DurationFormat("en", {
units: ["second", "millisecond"]
}).format({
second: 5,
millisecond: 120
});
// 5 seconds, 120 milliseconds
new Intl.DurationFormat("en", {
units: ["millisecond"]
}).format({
millisecond: 5,
microsecond: 120
});
// 5.12 milliseconds
This is an overly broad statement because (1) I can't speak for what all users in all languages expect in their own languages, and (2) even in English I can think of some cases where one may want the expanded form, such as when milliseconds are the "main" unit but one must display overflow to higher order units. |
OK, sounds like you're proposing this:
Is this correct? Revise if not! Issues / Questions:
|
Yep, that sounds right! Agreed about this being dependent on #32. I would truncate the extra digits, only because rounding gets complicated. How would you round 59.999 seconds to 2 fraction digits? We don't want to get into the messiness of shifting data around between fields; that should be done in client code before Intl.DurationFormat gets the duration.
Another question:
|
Hmm, >999 is a tough problem! Some possible solutions: (probably incomplete list)
This is also dependent on #32, but in the other direction than my previous comment. In this case, the "exception" resolution to #32 is the easiest way to solve the >999 problem because unexpected values cause exceptions in all cases. If choice is "best effort" then >999 behavior is not obvious. |
If we #32 gets resolved as "best effort" then I'd probably say to go with the 2nd option listed there (carry over to larger fields, stopping at seconds). This makes sense since we would already be equating seconds and smaller as things where digits can be transferred. So, the algorithm would basically be,
I don't care too much about this case since it is an edge case; we just need to make sure we have well-defined behavior. |
However, I would not throw an exception if we happen to exceed 60 seconds. For example, I think the following is totally a valid thing to do. const durf = new Intl.DurationFormat("en", { units: ["second"] });
durf.format({ milliseconds: 123456 });
// "123.456 seconds" Even if it weren't valid, if we resolve to best effort on #32, I would like to also do a best effort here and not throw the exception. |
The single-unit case is easy. The multiple-unit case is more interesting. Do you mean that you'd expect the following output if we chose "best effort" ? const durf = new Intl.DurationFormat("en", { units: ["second"] });
durf.format({ minutes: 30, milliseconds: 123456 });
// "30 minutes and 123.456 seconds" |
Yes, I think that output is fine. The idea with best effort is that you always present something that makes sense to the user. In this case, garbage in, garbage out. |
I'd tentatively support what you're recommending above. This feels complicated enough that there may be edge cases we haven't thought of, so probably needs a few more eyeballs to review to make sure we're not missing anything important. |
With the new decisions in #32, the decisions here mostly stand, with the following changes:
Examples:
Some edge cases might include:
|
I thought of another way to express this. Instead of using Display For example, |
If we go with this suggestion, what are the ambient default styles if no Also, what would be the difference in behavior between |
The ambient default styles for [milli/micro/nano]seconds would always be "numeric" even if the
hmm. I think "always" paired with "numeric" does not make sense for these units. Perhaps we can throw an exception if display "always" is specified with an incompatible style option. |
One extremely minor edge case: {
seconds: "short",
milliseconds: "numeric",
microseconds: "short",
nanoseconds: "numeric",
fractionalDigits: 2,
} What happens when you give this
I lean toward 1. |
Good catch. IMHO, the most likely expected output would be
Another possibility is that |
Good question. Worth discussing. In general I think we should try not to allow garbage input when it's easy to determine when there is garbage input. I think for the "numeric" style, we can impose these rules:
I think those rules cover all our cases? If any rule fails, then we throw a TypeError in the constructor.
I thought about this, but I'm not a fan, because we should be converging around |
Second is not the smallest unit accepted, but we now allow displaying all sub-second units as fractional seconds, with the added benefit of being able to control the exact number of fractional digits. @justingrant can this be closed? |
Closing |
There is a whole lot of discussion in this thread which appears to be not reflected in the spec. Please make sure that the open ends are tied up regarding the interaction of fractionalSecondDigits and millisecond/microsecond/nanosecond options. |
Clarification, once you go The API has changed quite a bit since this was discussed, |
Maybe some additional tests for this would be necessary. cc @ben-allen |
What needs to be done here:
|
New update: since we no longer allow display "always" on subsecond fields (#155), there is no action on the first comment linked above. On the second comment, we should just make sure there is test coverage that long/short/narrow cannot follow any numeric, 2-digit, or fractional style field. |
Should
'second'
be the smallest unit accepted? If someone wants to hide fractions, thenfractionalSecondDigits
can be used.From #32 (comment)
The text was updated successfully, but these errors were encountered: