Skip to content
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

Update README.md #106

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 108 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,20 @@

## Quick Start

Use the `format` getter function for formatting using `long` and `short` style options.

```javascript
new Intl.DurationFormat("fr-FR", { style: "long" }).format({
hours: 1,
minutes: 46,
seconds: 40,
});
// => "1 heure, 46 minutes et 40 secondes"
const duration = {
hours: 1,
minutes: 46,
seconds: 40,
};

new Intl.DurationFormat("fr-FR", { style: "long" }).format(duration);
// → '1 heure, 46 minutes et 40 secondes'

new Intl.DurationFormat("en", { style: "short" }).format(duration);
// → '1 hr, 46 min and 40 sec'
```

## Motivation
Expand All @@ -65,8 +72,9 @@ In this section, we are going to illustrate each user needs (requirements) and a

#### Design

* Input value will be an object of type `Temporal.Duration`
* Example: `new DurationFormat().format(Temporal.Duration.from({hours: 3, minutes: 4});`
* Input value will be an object, possible values include:
"`months`", " `weeks`", "`days`", "`hours`", "`minutes`", " `seconds`", "`milliseconds`", "`microseconds`", "`nanoseconds`".
* Example: `new DurationFormat().format({hours: 3, minutes: 4 });`

### Formatting width

Expand Down Expand Up @@ -182,20 +190,68 @@ We allow users to specify a `fractionalDigits` option that will display the smal
#### Example

```javascript
new Intl.DurationFormat('en', { fractionalDigits: 2 }).format('PT12.3456S'); // => 12.34 sec
new Intl.DurationFormat('en', { milliseconds: 'narrow', fractionalDigits: 2 }).format('PT12.3456S'); // => 12s 345.60ms
const duration = {
seconds: 12,
milliseconds: 345,
microseconds: 600,
};

// Example using fractionalDigits
new Intl.DurationFormat("en", { fractionalDigits: 2 }).format(duration);
// => 12 sec, 345 ms, 600 μs
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to reduce the surprise with this behavior, a solution would be to set the default style for milliseconds, microseconds, and nanoseconds to be "numeric", even in non-digital styles.

Either way, please include an example with fractionalDigits and digital format.


// Example using fractionalDigits and milliseconds set to `long`
new Intl.DurationFormat("en", {
milliseconds: "long",
fractionalDigits: 2,
}).format(duration);
// => 12 sec, 345 milliseconds, 600 μs
```

## API design

``` javascript
new Intl.DurationFormat('en').format(Temporal.Duration.from('PT2H46M40S')); // => 2 hr 46 min 40 sec
new Intl.DurationFormat('en', {
hours: 'numeric',
seconds: 'numeric',
}).format(Temporal.Duration.from('PT2H40S')); // => 2:00:40
const duration = {
hours: 2,
minutes: 46,
seconds: 40,
};

new Intl.DurationFormat("en").format(duration);
// => 2 hr 46 min 40 sec
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 hours, 46 minutes, and 40 seconds according to the current draft text.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/tc39/proposal-intl-duration-format/pull/127/files guess this modifies the behaviour you explain


new Intl.DurationFormat("en", {
hours: "numeric",
seconds: "numeric",
}).format(duration);
// => 2:46:40
```

```javascript
const duration = {
years: 1,
months: 2,
weeks: 3,
days: 3,
hours: 4,
minutes: 5,
seconds: 6,
milliseconds: 7,
microseconds: 8,
nanoseconds: 9,
};

new Intl.DurationFormat("en").format(duration);
// => '1 yr, 2 mths, 3 wks, 3 days, 4 hr, 5 min, 6 sec, 7 ms, 8 μs, 9 ns'
new Intl.DurationFormat("en", { style: "short" }).format(duration);
// => '1 yr, 2 mths, 3 wks, 3 days, 4 hr, 5 min, 6 sec, 7 ms, 8 μs, 9 ns'
new Intl.DurationFormat("en", { style: "narrow" }).format(duration);
// => '1y 2mo 3w 3d 4h 5m 6s 7ms 8μs 9ns'
new Intl.DurationFormat("en", { style: "digital" }).format(duration);
// = '1y, 2mo, 3w, 3d, 4:05:06'
```


### Constructor

#### Syntax
Expand Down Expand Up @@ -240,7 +296,7 @@ new Intl.DurationFormat(locales, options)

##### Default values

* The per-unit style options default to the value of `style` for all styles except `"digital"`, for which units years till days default to `"narrow"` and hours till nanoseconds default to `"numeric"`.
* The per-unit style options default to the value of `style` for all styles except `"digital"`, for which units years till days default to `"short"` and hours till nanoseconds default to `"numeric"`.
* The per-unit display options default to `"auto"` if the corresponding style option is `undefined` and `"always"` otherwise.

#### Notes
Expand Down Expand Up @@ -269,7 +325,42 @@ A `string` containing the formatted duration.
#### Syntax

```javascript
new Intl.DurationFormat('en').formatToParts(duration)
const duration = {
hours: 7,
minutes: 8,
seconds: 9,
milliseconds: 123,
microseconds: 456,
nanoseconds: 789,
};

new Intl.DurationFormat('en').formatToParts(duration);

// => [
// { type: "integer", value: "7", unit: "hour" },
// { type: "literal", value: " ", unit: "hour" },
// { type: "unit", value: "hr", unit: "hour" },
// { type: "literal", value: ", " },
// { type: "integer", value: "8", unit: "minute" },
// { type: "literal", value: " ", unit: "minute" },
// { type: "unit", value: "min", unit: "minute" },
// { type: "literal", value: ", " },
// { type: "integer", value: "9", unit: "second" },
// { type: "literal", value: " ", unit: "second" },
// { type: "unit", value: "sec", unit: "second" },
// { type: "literal", value: ", " },
// { type: "integer", value: "123", unit: "millisecond" },
// { type: "literal", value: " ", unit: "millisecond" },
// { type: "unit", value: "msec", unit: "millisecond" },
// { type: "literal", value: ", " },
// { type: "integer", value: "456", unit: "microsecond" },
// { type: "literal", value: " ", unit: "microsecond" },
// { type: "unit", value: "μsec", unit: "microsecond" },
// { type: "literal", value: " and " },
// { type: "integer", value: "789", unit: "nanosecond" },
// { type: "literal", value: " ", unit: "nanosecond" },
// { type: "unit", value: "nsec", unit: "nanosecond" },
// ]
```

#### Parameters
Expand Down
4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2411,7 +2411,7 @@
</ul></div><div id="menu-toggle"><svg xmlns="http://www.w3.org/2000/svg" style="width:100%; height:100%; stroke:currentColor" viewBox="0 0 120 120">
<title>Menu</title>
<path stroke-width="10" stroke-linecap="round" d="M30,60 h60 M30,30 m0,5 h60 M30,90 m0,-5 h60"></path>
</svg></div><div id="menu-spacer"></div><div id="menu"><div id="menu-search"><input type="text" id="menu-search-box" placeholder="Search..."><div id="menu-search-results" class="inactive"></div></div><div id="menu-pins"><div class="menu-pane-header">Pins</div><ul id="menu-pins-list"></ul></div><div class="menu-pane-header">Table of Contents</div><div id="menu-toc"><ol class="toc"><li><span class="item-toggle">◢</span><a href="#durationformat-objects" title="DurationFormat Objects"><span class="secnum">1</span> DurationFormat Objects</a><ol class="toc"><li><span class="item-toggle">◢</span><a href="#sec-intl-durationformat-abstracts" title="Abstract Operations for DurationFormat Objects"><span class="secnum">1.1</span> Abstract Operations for DurationFormat Objects</a><ol class="toc"><li><span class="item-toggle-none"></span><a href="#sec-duration-records" title="Duration Records"><span class="secnum">1.1.1</span> Duration Records</a></li><li><span class="item-toggle-none"></span><a href="#sec-tointegerwithoutrounding" title="ToIntegerWithoutRounding ( argument )"><span class="secnum">1.1.2</span> ToIntegerWithoutRounding ( <var>argument</var> )</a></li><li><span class="item-toggle-none"></span><a href="#sec-todurationrecord" title="ToDurationRecord ( input )"><span class="secnum">1.1.3</span> ToDurationRecord ( <var>input</var> )</a></li><li><span class="item-toggle-none"></span><a href="#sec-durationsign" title="DurationSign ( record )"><span class="secnum">1.1.4</span> DurationSign ( <var>record</var> )</a></li><li><span class="item-toggle-none"></span><a href="#sec-isvaliddurationrecord" title="IsValidDurationRecord ( record )"><span class="secnum">1.1.5</span> IsValidDurationRecord ( <var>record</var> )</a></li><li><span class="item-toggle-none"></span><a href="#sec-getdurationunitoptions" title="GetDurationUnitOptions ( unit, options, baseStyle, stylesList, digitalBase, prevStyle )"><span class="secnum">1.1.6</span> GetDurationUnitOptions ( <var>unit</var>, <var>options</var>, <var>baseStyle</var>, <var>stylesList</var>, <var>digitalBase</var>, <var>prevStyle</var> )</a></li><li><span class="item-toggle-none"></span><a href="#sec-partitiondurationformatpattern" title="PartitionDurationFormatPattern ( durationFormat, duration )"><span class="secnum">1.1.7</span> PartitionDurationFormatPattern ( <var>durationFormat</var>, <var>duration</var> )</a></li></ol></li><li><span class="item-toggle">◢</span><a href="#sec-intl-durationformat-constructor" title="The Intl.DurationFormat Constructor"><span class="secnum">1.2</span> The Intl.DurationFormat Constructor</a><ol class="toc"><li><span class="item-toggle-none"></span><a href="#sec-Intl.DurationFormat" title="Intl.DurationFormat ( [ locales [ , options ] ] )"><span class="secnum">1.2.1</span> Intl.DurationFormat ( [ <var>locales</var> [ , <var>options</var> ] ] )</a></li></ol></li><li><span class="item-toggle">◢</span><a href="#sec-properties-of-intl-durationformat-constructor" title="Properties of the Intl.DurationFormat Constructor"><span class="secnum">1.3</span> Properties of the Intl.DurationFormat Constructor</a><ol class="toc"><li><span class="item-toggle-none"></span><a href="#sec-Intl.DurationFormat.prototype" title="Intl.DurationFormat.prototype"><span class="secnum">1.3.1</span> Intl.DurationFormat.prototype</a></li><li><span class="item-toggle-none"></span><a href="#sec-Intl.DurationFormat.supportedLocalesOf" title="Intl.DurationFormat.supportedLocalesOf ( locales [ , options ] )"><span class="secnum">1.3.2</span> Intl.DurationFormat.supportedLocalesOf ( <var>locales</var> [ , <var>options</var> ] )</a></li><li><span class="item-toggle-none"></span><a href="#sec-Intl.DurationFormat-internal-slots" title="Internal slots"><span class="secnum">1.3.3</span> Internal slots</a></li></ol></li><li><span class="item-toggle">◢</span><a href="#sec-properties-of-intl-durationformat-prototype-object" title="Properties of the Intl.DurationFormat Prototype Object"><span class="secnum">1.4</span> Properties of the Intl.DurationFormat Prototype Object</a><ol class="toc"><li><span class="item-toggle-none"></span><a href="#sec-Intl.DurationFormat.prototype.constructor" title="Intl.DurationFormat.prototype.constructor"><span class="secnum">1.4.1</span> Intl.DurationFormat.prototype.constructor</a></li><li><span class="item-toggle-none"></span><a href="#sec-Intl.DurationFormat.prototype-@@tostringtag" title="Intl.DurationFormat.prototype [ @@toStringTag ]"><span class="secnum">1.4.2</span> Intl.DurationFormat.prototype [ @@toStringTag ]</a></li><li><span class="item-toggle-none"></span><a href="#sec-Intl.DurationFormat.prototype.format" title="Intl.DurationFormat.prototype.format ( duration )"><span class="secnum">1.4.3</span> Intl.DurationFormat.prototype.format ( <var>duration</var> )</a></li><li><span class="item-toggle-none"></span><a href="#sec-Intl.DurationFormat.prototype.formatToParts" title="Intl.DurationFormat.prototype.formatToParts ( duration )"><span class="secnum">1.4.4</span> Intl.DurationFormat.prototype.formatToParts ( <var>duration</var> )</a></li><li><span class="item-toggle-none"></span><a href="#sec-Intl.DurationFormat.prototype.resolvedOptions" title="Intl.DurationFormat.prototype.resolvedOptions ( )"><span class="secnum">1.4.5</span> Intl.DurationFormat.prototype.resolvedOptions ( )</a></li></ol></li><li><span class="item-toggle-none"></span><a href="#sec-properties-of-intl-durationformat-instances" title="Properties of Intl.DurationFormat Instances"><span class="secnum">1.5</span> Properties of Intl.DurationFormat Instances</a></li></ol></li><li><span class="item-toggle-none"></span><a href="#sec-copyright-and-software-license" title="Copyright &amp; Software License"><span class="secnum">A</span> Copyright &amp; Software License</a></li></ol></div></div><div id="spec-container"><h1 class="version">Stage 3 Draft / July 2, 2022</h1><h1 class="title">Intl.DurationFormat</h1>
</svg></div><div id="menu-spacer"></div><div id="menu"><div id="menu-search"><input type="text" id="menu-search-box" placeholder="Search..."><div id="menu-search-results" class="inactive"></div></div><div id="menu-pins"><div class="menu-pane-header">Pins</div><ul id="menu-pins-list"></ul></div><div class="menu-pane-header">Table of Contents</div><div id="menu-toc"><ol class="toc"><li><span class="item-toggle">◢</span><a href="#durationformat-objects" title="DurationFormat Objects"><span class="secnum">1</span> DurationFormat Objects</a><ol class="toc"><li><span class="item-toggle">◢</span><a href="#sec-intl-durationformat-abstracts" title="Abstract Operations for DurationFormat Objects"><span class="secnum">1.1</span> Abstract Operations for DurationFormat Objects</a><ol class="toc"><li><span class="item-toggle-none"></span><a href="#sec-duration-records" title="Duration Records"><span class="secnum">1.1.1</span> Duration Records</a></li><li><span class="item-toggle-none"></span><a href="#sec-tointegerwithoutrounding" title="ToIntegerWithoutRounding ( argument )"><span class="secnum">1.1.2</span> ToIntegerWithoutRounding ( <var>argument</var> )</a></li><li><span class="item-toggle-none"></span><a href="#sec-todurationrecord" title="ToDurationRecord ( input )"><span class="secnum">1.1.3</span> ToDurationRecord ( <var>input</var> )</a></li><li><span class="item-toggle-none"></span><a href="#sec-durationsign" title="DurationSign ( record )"><span class="secnum">1.1.4</span> DurationSign ( <var>record</var> )</a></li><li><span class="item-toggle-none"></span><a href="#sec-isvaliddurationrecord" title="IsValidDurationRecord ( record )"><span class="secnum">1.1.5</span> IsValidDurationRecord ( <var>record</var> )</a></li><li><span class="item-toggle-none"></span><a href="#sec-getdurationunitoptions" title="GetDurationUnitOptions ( unit, options, baseStyle, stylesList, digitalBase, prevStyle )"><span class="secnum">1.1.6</span> GetDurationUnitOptions ( <var>unit</var>, <var>options</var>, <var>baseStyle</var>, <var>stylesList</var>, <var>digitalBase</var>, <var>prevStyle</var> )</a></li><li><span class="item-toggle-none"></span><a href="#sec-partitiondurationformatpattern" title="PartitionDurationFormatPattern ( durationFormat, duration )"><span class="secnum">1.1.7</span> PartitionDurationFormatPattern ( <var>durationFormat</var>, <var>duration</var> )</a></li></ol></li><li><span class="item-toggle">◢</span><a href="#sec-intl-durationformat-constructor" title="The Intl.DurationFormat Constructor"><span class="secnum">1.2</span> The Intl.DurationFormat Constructor</a><ol class="toc"><li><span class="item-toggle-none"></span><a href="#sec-Intl.DurationFormat" title="Intl.DurationFormat ( [ locales [ , options ] ] )"><span class="secnum">1.2.1</span> Intl.DurationFormat ( [ <var>locales</var> [ , <var>options</var> ] ] )</a></li></ol></li><li><span class="item-toggle">◢</span><a href="#sec-properties-of-intl-durationformat-constructor" title="Properties of the Intl.DurationFormat Constructor"><span class="secnum">1.3</span> Properties of the Intl.DurationFormat Constructor</a><ol class="toc"><li><span class="item-toggle-none"></span><a href="#sec-Intl.DurationFormat.prototype" title="Intl.DurationFormat.prototype"><span class="secnum">1.3.1</span> Intl.DurationFormat.prototype</a></li><li><span class="item-toggle-none"></span><a href="#sec-Intl.DurationFormat.supportedLocalesOf" title="Intl.DurationFormat.supportedLocalesOf ( locales [ , options ] )"><span class="secnum">1.3.2</span> Intl.DurationFormat.supportedLocalesOf ( <var>locales</var> [ , <var>options</var> ] )</a></li><li><span class="item-toggle-none"></span><a href="#sec-Intl.DurationFormat-internal-slots" title="Internal slots"><span class="secnum">1.3.3</span> Internal slots</a></li></ol></li><li><span class="item-toggle">◢</span><a href="#sec-properties-of-intl-durationformat-prototype-object" title="Properties of the Intl.DurationFormat Prototype Object"><span class="secnum">1.4</span> Properties of the Intl.DurationFormat Prototype Object</a><ol class="toc"><li><span class="item-toggle-none"></span><a href="#sec-Intl.DurationFormat.prototype.constructor" title="Intl.DurationFormat.prototype.constructor"><span class="secnum">1.4.1</span> Intl.DurationFormat.prototype.constructor</a></li><li><span class="item-toggle-none"></span><a href="#sec-Intl.DurationFormat.prototype-@@tostringtag" title="Intl.DurationFormat.prototype [ @@toStringTag ]"><span class="secnum">1.4.2</span> Intl.DurationFormat.prototype [ @@toStringTag ]</a></li><li><span class="item-toggle-none"></span><a href="#sec-Intl.DurationFormat.prototype.format" title="Intl.DurationFormat.prototype.format ( duration )"><span class="secnum">1.4.3</span> Intl.DurationFormat.prototype.format ( <var>duration</var> )</a></li><li><span class="item-toggle-none"></span><a href="#sec-Intl.DurationFormat.prototype.formatToParts" title="Intl.DurationFormat.prototype.formatToParts ( duration )"><span class="secnum">1.4.4</span> Intl.DurationFormat.prototype.formatToParts ( <var>duration</var> )</a></li><li><span class="item-toggle-none"></span><a href="#sec-Intl.DurationFormat.prototype.resolvedOptions" title="Intl.DurationFormat.prototype.resolvedOptions ( )"><span class="secnum">1.4.5</span> Intl.DurationFormat.prototype.resolvedOptions ( )</a></li></ol></li><li><span class="item-toggle-none"></span><a href="#sec-properties-of-intl-durationformat-instances" title="Properties of Intl.DurationFormat Instances"><span class="secnum">1.5</span> Properties of Intl.DurationFormat Instances</a></li></ol></li><li><span class="item-toggle-none"></span><a href="#sec-copyright-and-software-license" title="Copyright &amp; Software License"><span class="secnum">A</span> Copyright &amp; Software License</a></li></ol></div></div><div id="spec-container"><h1 class="version">Stage 3 Draft / March 8, 2023</h1><h1 class="title">Intl.DurationFormat</h1>
<emu-biblio href="./biblio.json"></emu-biblio>
<emu-clause id="durationformat-objects">
<h1><span class="secnum">1</span> DurationFormat Objects</h1>
Expand Down Expand Up @@ -2810,7 +2810,7 @@ <h1><span class="secnum">1.5</span> Properties of Intl.DurationFormat Instances<
<h1><span class="secnum">A</span> Copyright &amp; Software License</h1>

<h2>Copyright Notice</h2>
<p>© 2022 Ujjwal Sharma, Younies Mahmoud</p>
<p>© 2023 Ujjwal Sharma, Younies Mahmoud</p>

<h2>Software License</h2>
<p>All Software contained in this document ("Software") is protected by copyright and is being made available under the "BSD License", included below. This Software may be subject to third party rights (rights from parties other than Ecma International), including patent rights, and no licenses under such third party rights are granted under this license even if the third party concerned is a member of Ecma International. SEE THE ECMA CODE OF CONDUCT IN PATENT MATTERS AVAILABLE AT https://ecma-international.org/memento/codeofconduct.htm FOR INFORMATION REGARDING THE LICENSING OF PATENT CLAIMS THAT ARE REQUIRED TO IMPLEMENT ECMA INTERNATIONAL STANDARDS.</p>
Expand Down