Skip to content

Commit

Permalink
Implemented issue #253: Duration.toISO() doesn't include milliseconds (
Browse files Browse the repository at this point in the history
#284)

Include milliseconds in Duration#toISO
  • Loading branch information
Bogdan Polak authored and icambron committed Jun 27, 2018
1 parent 2b65af4 commit b8b8a9b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ node_modules
build
#*
.#*
coverage/
coverage/
.DS_Store
4 changes: 3 additions & 1 deletion src/duration.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ export default class Duration {
* @example Duration.fromObject({ months: 4, seconds: 45 }).toISO() //=> 'P4MT45S'
* @example Duration.fromObject({ months: 5 }).toISO() //=> 'P5M'
* @example Duration.fromObject({ minutes: 5 }).toISO() //=> 'PT5M'
* @example Duration.fromObject({ milliseconds: 6 }).toISO() //=> 'PT0.006S'
* @return {string}
*/
toISO() {
Expand All @@ -399,7 +400,8 @@ export default class Duration {
if (norm.hours > 0 || norm.minutes > 0 || norm.seconds > 0 || norm.milliseconds > 0) s += 'T';
if (norm.hours > 0) s += norm.hours + 'H';
if (norm.minutes > 0) s += norm.minutes + 'M';
if (norm.seconds > 0) s += norm.seconds + 'S';
if (norm.seconds > 0 || norm.milliseconds > 0)
s += norm.seconds + norm.milliseconds / 1000 + 'S';
return s;
}

Expand Down
6 changes: 5 additions & 1 deletion test/duration/format.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const dur = () =>
// #toISO()
//------
test('Duration#toISO fills out every field', () => {
expect(dur().toISO()).toBe('P1Y2M3DT4H5M6S');
expect(dur().toISO()).toBe('P1Y2M3DT4H5M6.007S');
});

test('Duration#toISO creates a minimal string', () => {
Expand All @@ -38,6 +38,10 @@ test('Duration#toISO returns null for invalid durations', () => {
expect(Duration.invalid('because').toISO()).toBe(null);
});

test('Duration#toISO handles milliseconds duration', () => {
expect(Duration.fromObject({ milliseconds: 7 }).toISO()).toBe('PT0.007S');
});

//------
// #toJSON()
//------
Expand Down

0 comments on commit b8b8a9b

Please sign in to comment.