Skip to content

Commit bf3510a

Browse files
justingrantptomato
authored andcommitted
Fix Duration docs code samples
1 parent b7c4ac8 commit bf3510a

File tree

2 files changed

+20
-23
lines changed

2 files changed

+20
-23
lines changed

docs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ const duration = Temporal.Duration.from({
193193
minutes: 20
194194
});
195195

196-
duration.total({ largestUnit: 'minutes' }); // => 469200
196+
duration.total({ unit: 'seconds' }); // => 469200
197197
```
198198

199199
See [Temporal.Duration Documentation](./duration.md) for detailed documentation.

docs/duration.md

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ Otherwise, the function will throw a `RangeError`.
9090
Any non-object value is converted to a string, which is expected to be in ISO 8601 format.
9191

9292
> **NOTE:** This function understands strings where weeks and other units are combined, and strings with a single sign character at the start, which are extensions to the ISO 8601 standard described in ISO 8601-2.
93-
> (For example, `P3W1D` is understood to mean three weeks and one day, `-P1Y1M` is a negative duration of one year and one month, and `+P1Y1M` is one year and one month.)
93+
> For example, `P3W1D` is understood to mean three weeks and one day, `-P1Y1M` is a negative duration of one year and one month, and `+P1Y1M` is one year and one month.
9494
> If no sign character is present, then the sign is assumed to be positive.
9595
9696
Usage examples:
@@ -421,8 +421,8 @@ This operation is called "balancing."
421421
For usage examples and a more complete explanation of how balancing works, see [Duration balancing](./balancing.md).
422422

423423
A `largestUnit` value of `'auto'`, which is the default if only `smallestUnit` is given, means that `largestUnit` should be the largest nonzero unit in the duration that is larger than `smallestUnit`.
424-
(For example, in a duration of 3 days and 12 hours, `largestUnit: 'auto'` would mean the same as `largestUnit: 'days'`.)
425-
This means that the default is for the balancing behaviour of this method to not 'grow' the duration beyond its current largest unit unless needed for rounding.
424+
For example, in a duration of 3 days and 12 hours, `largestUnit: 'auto'` would mean the same as `largestUnit: 'days'`.
425+
This behavior implies that the default balancing behaviour of this method to not 'grow' the duration beyond its current largest unit unless needed for rounding.
426426

427427
The `smallestUnit` option determines the unit to round to.
428428
For example, to round to the nearest minute, use `smallestUnit: 'minutes'`.
@@ -437,9 +437,9 @@ The `roundingIncrement` option allows rounding to an integer number of units.
437437
For example, to round to increments of a half hour, use `smallestUnit: 'minutes', roundingIncrement: 30`.
438438

439439
Unless `smallestUnit` is years, months, weeks, or days, the value given as `roundingIncrement` must divide evenly into the next highest unit after `smallestUnit`, and must not be equal to it.
440-
(For example, if `smallestUnit` is `'minutes'`, then the number of minutes given by `roundingIncrement` must divide evenly into 60 minutes, which is one hour.
440+
For example, if `smallestUnit` is `'minutes'`, then the number of minutes given by `roundingIncrement` must divide evenly into 60 minutes, which is one hour.
441441
The valid values in this case are 1 (default), 2, 3, 4, 5, 6, 10, 12, 15, 20, and 30.
442-
Instead of 60 minutes, use 1 hour.)
442+
Instead of 60 minutes, use 1 hour.
443443

444444
The `roundingMode` option controls how the rounding is performed.
445445

@@ -475,16 +475,15 @@ d = Temporal.Duration.from('PT2H34M18S');
475475
d.round({ largestUnit: 'seconds' }).seconds; // => 9258
476476

477477
// Normalize, with and without taking DST into account
478-
d = Temporal.Duration.from({ hours: 1756 });
479-
// FIXME: write this example after ZonedDateTime is added
480-
// d.round({
481-
// relativeTo: '2020-01-01T00:00+01:00[Europe/Rome]',
482-
// largestUnit: 'years'
483-
// }); // => ???
478+
d = Temporal.Duration.from({ hours: 2756 });
479+
d.round({
480+
relativeTo: '2020-01-01T00:00+01:00[Europe/Rome]',
481+
largestUnit: 'years'
482+
}); // => P114DT21H (one hour longer because DST skipped an hour)
484483
d.round({
485484
relativeTo: '2020-01-01',
486485
largestUnit: 'years'
487-
}); // => P73DT4H
486+
}); // => P114DT20H (one hour shorter if ignoring DST)
488487

489488
// Normalize days into months or years
490489
d = Temporal.Duration.from({ days: 190 });
@@ -495,7 +494,7 @@ d.round({ relativeTo: refDate, largestUnit: 'years' }); // => P6M6D
495494
d.round({
496495
relativeTo: refDate.withCalendar('hebrew'),
497496
largestUnit: 'years'
498-
}); // => ???
497+
}); // => P6M13D
499498

500499
// Round a duration up to the next 5-minute billing period
501500
d = Temporal.Duration.from({ minutes: 6 });
@@ -548,24 +547,22 @@ Example usage:
548547
```javascript
549548
// How many seconds in 18 hours and 20 minutes?
550549
d = Temporal.Duration.from({ hours: 130, minutes: 20 });
551-
d.total({ largestUnit: 'minutes' }); // => 469200
550+
d.total({ unit: 'seconds' }); // => 469200
552551

553552
// How many 24-hour days is 123456789 seconds?
554553
d = Temporal.Duration.from('PT123456789S');
555554
d.total({ unit: 'days' }); // 1428.8980208333332
556555

557556
// Find totals in months, with and without taking DST into account
558-
d = Temporal.Duration.from({ hours: 1756 });
559-
// FIXME: write this example after ZonedDateTime is added
560-
// d.round({
561-
// relativeTo: '2020-01-01T00:00+01:00[Europe/Rome]',
562-
// largestUnit: 'months'
563-
// }); // => ???
557+
d = Temporal.Duration.from({ hours: 2756 });
558+
d.total({
559+
relativeTo: '2020-01-01T00:00+01:00[Europe/Rome]',
560+
unit: 'months'
561+
}); // => 3.7958333333333334
564562
d.total({
565563
unit: 'months',
566564
relativeTo: '2020-01-01'
567-
}); // => 2.39247311827957
568-
// FIXME: update the result above after duration rounding is fixed
565+
}); // => 3.7944444444444443
569566
```
570567

571568
### duration.**toString**(_options_?: object) : string

0 commit comments

Comments
 (0)