Skip to content

Intl.DurationFormat #47

Open
Open
@zbraniecki

Description

Since there's little progress with deciding on what to do with durations, and ICU's API doesn't seem too defined about it, I'd like to start a discussion about possible choices. It seems to me that we have four:

  1. We can add it to NumberFormat
  2. We can add it to UnitFormat
  3. We can create a separate DurationFormat
  4. We can kickstart some RuleBasedNumberFormat

The common use cases are:

  1. Timer "05:12:21"
  2. Stopwatch "12:21.05"
  3. Video/Music player remaining/elapsed time "-04:13"

The formatting differs from time formatter, because the UI will define the range of units to be displayed, like h:m:s, m:s.S and the number of digits per unit (m:ss vs mm:ss or mm:ss.SSS vs mm:ss.SS)

NumberFormat would be convenient for that reason - it would benefit from similar API of defining minimumIntegerDigits and maximumIntegerDigits for each component, and the value itself may be just a number of milliseconds.

Fitting it into UnitFormat seems to be natural fit for CLDR Unit Elements [0] together with compound units, unit sequences and coordinate units, but may be hard and requiring us to bend over backward to accommodate for the options that will be required.

Custom DurationFormat seems to be the easiest choice from the API design perspective, but it increases the number of objects we specify. It's been the choice I made so far for Firefox OS patterns, but I'll be happy to migrate our code once we reach a consensus here.

RuleBasedNumberFormat is what ICU seems to be using now for duration patterns, but it's a pretty big API most similar to Mozilla's proprietary Date.prototype.toLocaleFormat and I'm not sure if we want to go there.

The API I use in DurationFormat looks like this:

var f = Intl.DurationFormat(locales, {
  minUnit: 'second', // millisecond | second | minute | hour
  maxUnit: 'hour' // millisecond | second | minute | hour
});
f.format(i); // 52:34:51

Thougths?

[0] http://www.unicode.org/reports/tr35/tr35-general.html#Unit_Elements

Metadata

Assignees

Labels

ProposalLarger change requiring a proposalc: datetimeComponent: dates, times, timezoness: in progressStatus: the issue has an active proposal

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions