-
Notifications
You must be signed in to change notification settings - Fork 157
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
Round a Temporal.Duration to the nearest small unit #337
Comments
Should this be part of this proposal or part of DurationFormat? I wouldn't be opposed to adding this API but we could also consider adding const duration = Temporal.Duration.from({ seconds: 59, milliseconds: 999 })
Temporal.Duration.from(duration, { smallestUnit: 'seconds' }).toString() // => 'PT60S'
Temporal.Duration.from(duration, { smallestUnit: 'seconds', disambiguation: 'balance' }).toString() // => 'PT1M' |
It's certainly up for debate about whether this functionality belongs in Intl.DurationFormat or Temporal.Duration. I've made the case previously that it belongs in Temporal.Duration. Intl.DurationFormat should be a "pass-through" formatter; it should take what it's given and format it. Maybe it can mask fields, but it feels beyond the scope of Intl.DurationFormat to start balancing things between fields. Temporal.Duration puts the math all in one place. That being said, I can also see arguments that this is Intl.DurationFormat's responsibility. It would be good to have more of those arguments written out. |
Another use case: you want to render only the 2 most significant fields. For example, first you tick down hours and minutes, then minutes and seconds (when hours becomes 0), then seconds and milliseconds (when minutes becomes 0). |
I think this good for counters as @sffc said. But we need to hit a limit. For example, count down for a world cup, but once you hit second. only round for seconds. |
A more flexible option might be something more along the lines of:
Then the decision on exactly which fields to display can be done in user land. |
How about, const d = Temporal.Duration.from({ days: 10, hours: 23 });
const d1 = d.balance({ largestUnit: "weeks", calendar: "iso" }); // 1 week 3 days 23 hours
const d1 = d.balance({ smallestUnit: "days" }); // 11 days (or 10 days?) If doing math with weeks or higher, you just have to pass in which calendar you want. |
Agreed. Rounding is a business-logic issue. It doesn't only matter when formatting, but also when storing, calculating, etc. For example, some databases allow you to control the precision of date/time data... so the app needs to round before storing. Or sometimes it's important to know if two durations are "equal" but you'll only know that after rounding. |
I don't think Temporal should conflate balancing (which carries excess quantities into a largest unit, preserving total magnitude) with rounding (which replaces total magnitude with a nearby value that is an exact multiple of some increment). Even the inputs are different—balancing is dependent upon the choice of largest unit, while rounding is dependent upon the choice of increment¹ and mode². That said, rounding itself is clearly valuable, to both Intl.DurationFormat and to authors. It could certainly be built on top of a minimal Temporal.Duration, but user-space implementations are likely to miss the nuance. I support including it. ¹ which is not necessarily a unit; it could be e.g. "one hundredth of a second" or "6 minutes" |
Meeting, May 28: We decided not to add this API to the initially shipping polyfill at the last minute, because it needs more design, but there is consensus that we do need something like it. |
I agree with @gibson042 that balancing and rounding are fundamentally different operations. But I can also see value in combining both operations into a single method because the problem domains overlap. For example, Another advantage of co-location: it's a common use case to only want a single unit, e.g. "total days" or "total minutes". In cases where Invalid combinations (e.g. If we did decide to co-locate balancing and rounding, we'd want a single term that encompasses both, e.g. "normalize", "coerce", "adjust", "limit", "clamp", "trim", "condense", etc. Ironically, in #609 I made the opposite argument about assignment vs. addition/subtraction on non-Duration types. Currently we allow both operations in a single |
Actually, shall we just close this in favour of #856 instead. |
There is a need identified in https://github.com/younies/proposal-intl-duration-format/issues/2 to perform rounding of Temporal.Duration fields. For example, if you have a duration
{ seconds: 59, milliseconds: 999 }
, and you want it rounded to seconds, then you should get{ minutes: 1 }
.Could we add a
.roundTo(field)
method that takes a string field like"seconds"
(returning a new Temporal.Duration)? That method would probably need adisambiguation
parameter.CC @younies
Related: #324
The text was updated successfully, but these errors were encountered: