Skip to content
Merged
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
8 changes: 2 additions & 6 deletions docs/plainyearmonth.md
Original file line number Diff line number Diff line change
Expand Up @@ -336,9 +336,7 @@ This method adds `duration` to `yearMonth`, returning a month that is in the fut

The `duration` argument is an object with properties denoting a duration, such as `{ months: 5 }`, or a string such as `P5M`, or a `Temporal.Duration` object.
If `duration` is not a `Temporal.Duration` object, then it will be converted to one as if it were passed to `Temporal.Duration.from()`.

If `duration` has any units smaller than `months`, they will be treated as if they are being added to the first moment of the month given by `yearMonth`.
Effectively, this means that adding things like `{ days: 1 }` will be ignored.
There must not be any units smaller than `months` in `duration`.

If the result is earlier or later than the range of dates that `Temporal.PlainYearMonth` can represent (approximately half a million years centered on the [Unix epoch](https://en.wikipedia.org/wiki/Unix_time)), then this method will throw a `RangeError` regardless of `overflow`.

Expand Down Expand Up @@ -372,9 +370,7 @@ This method subtracts `duration` from `yearMonth`, returning a month that is in

The `duration` argument is an object with properties denoting a duration, such as `{ months: 5 }`, or a string such as `P5M`, or a `Temporal.Duration` object.
If `duration` is not a `Temporal.Duration` object, then it will be converted to one as if it were passed to `Temporal.Duration.from()`.

If `duration` has any units smaller than `months`, they will be treated as if they are being subtracted from the last moment of the month given by `yearMonth`.
Effectively, this means that subtracting things like `{ days: 1 }` will be ignored.
There must not be any units smaller than `months` in `duration`.

If the result is earlier or later than the range of dates that `Temporal.PlainYearMonth` can represent (approximately half a million years centered on the [Unix epoch](https://en.wikipedia.org/wiki/Unix_time)), then this method will throw a `RangeError` regardless of `overflow`.

Expand Down
18 changes: 9 additions & 9 deletions polyfill/lib/ecmascript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4128,21 +4128,21 @@ export function AddDurationToTime(operation, temporalTime, durationLike) {
export function AddDurationToYearMonth(operation, yearMonth, durationLike, options) {
let duration = ToTemporalDuration(durationLike);
if (operation === 'subtract') duration = CreateNegatedTemporalDuration(duration);
const internalDuration = ToInternalDurationRecord(duration);

const resolvedOptions = GetOptionsObject(options);
const overflow = GetTemporalOverflowOption(resolvedOptions);
const sign = DurationSign(duration);

const durationToAdd = internalDuration.date;
if (durationToAdd.weeks !== 0 || durationToAdd.days !== 0 || !internalDuration.time.isZero()) {
throw new RangeErrorCtor('only years and months can be added to Temporal.PlainYearMonth');
}

const calendar = GetSlot(yearMonth, CALENDAR);
const fields = ISODateToFields(calendar, GetSlot(yearMonth, ISO_DATE), 'year-month');
fields.day = 1;
let startDate = CalendarDateFromFields(calendar, fields, 'constrain');
if (sign < 0) {
const nextMonth = CalendarDateAdd(calendar, startDate, { months: 1 }, 'constrain');
startDate = AddDaysToISODate(nextMonth, -1);
}
const durationToAdd = ToDateDurationRecordWithoutTime(duration);
RejectDateRange(startDate);
const addedDate = CalendarDateAdd(calendar, startDate, durationToAdd, overflow);
const date = CalendarDateFromFields(calendar, fields, 'constrain');
const addedDate = CalendarDateAdd(calendar, date, durationToAdd, overflow);
const addedDateFields = ISODateToFields(calendar, addedDate, 'year-month');

const isoDate = CalendarYearMonthFromFields(calendar, addedDateFields, overflow);
Expand Down
6 changes: 5 additions & 1 deletion polyfill/test/thorough/yearmonthaddition.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import {
} from './support.mjs';

const interestingYearMonths = makeYearMonthCases();
const interestingDurations = makeDurationCases();
const interestingDurations = makeDurationCases().filter(
// We don't need to test over and over that units lower than months are not
// allowed, but years/months mixed with lower units are still interesting
([duration]) => duration.blank || duration.years !== 0 || duration.months !== 0
);
const total = interestingYearMonths.length * interestingDurations.length;

await time(async (start) => {
Expand Down
Loading