Skip to content

Commit 64b2eab

Browse files
12wrigjaptomato
andauthored
Co-authored-by: Philip Chimento <[email protected]>
1 parent f3f8a99 commit 64b2eab

File tree

4 files changed

+36
-146
lines changed

4 files changed

+36
-146
lines changed

lib/ecmascript.ts

Lines changed: 28 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,7 @@ export function ToRelativeTemporalObject(options: {
922922
| Temporal.PlainDateTimeLike
923923
| string
924924
| undefined;
925-
}): Temporal.ZonedDateTime | Temporal.PlainDateTime | undefined {
925+
}): Temporal.ZonedDateTime | Temporal.PlainDate | undefined {
926926
const relativeTo = options.relativeTo;
927927
// TODO: `as undefined` below should not be needed. Verify that it can be
928928
// removed after strictNullChecks is enabled.
@@ -932,21 +932,8 @@ export function ToRelativeTemporalObject(options: {
932932
let matchMinutes = false;
933933
let year, month, day, hour, minute, second, millisecond, microsecond, nanosecond, calendar, timeZone, offset;
934934
if (IsObject(relativeTo)) {
935-
if (IsTemporalZonedDateTime(relativeTo) || IsTemporalDateTime(relativeTo)) return relativeTo;
936-
if (IsTemporalDate(relativeTo)) {
937-
return CreateTemporalDateTime(
938-
GetSlot(relativeTo, ISO_YEAR),
939-
GetSlot(relativeTo, ISO_MONTH),
940-
GetSlot(relativeTo, ISO_DAY),
941-
0,
942-
0,
943-
0,
944-
0,
945-
0,
946-
0,
947-
GetSlot(relativeTo, CALENDAR)
948-
);
949-
}
935+
if (IsTemporalZonedDateTime(relativeTo) || IsTemporalDate(relativeTo)) return relativeTo;
936+
if (IsTemporalDateTime(relativeTo)) return TemporalDateTimeToDate(relativeTo);
950937
calendar = GetTemporalCalendarWithISODefault(relativeTo);
951938
const fieldNames = CalendarFields(calendar, [
952939
'day',
@@ -1011,7 +998,7 @@ export function ToRelativeTemporalObject(options: {
1011998
);
1012999
return CreateTemporalZonedDateTime(epochNanoseconds, timeZone, calendar);
10131000
}
1014-
return CreateTemporalDateTime(year, month, day, hour, minute, second, millisecond, microsecond, nanosecond, calendar);
1001+
return CreateTemporalDate(year, month, day, calendar);
10151002
}
10161003

10171004
export function ValidateTemporalUnitRange(largestUnit: Temporal.DateTimeUnit, smallestUnit: Temporal.DateTimeUnit) {
@@ -1243,7 +1230,7 @@ function ToTemporalZonedDateTimeFields(
12431230
export function ToTemporalDate(
12441231
itemParam: PlainDateParams['from'][0],
12451232
options: PlainDateParams['from'][1] = ObjectCreate(null)
1246-
) {
1233+
): Temporal.PlainDate {
12471234
let item = itemParam;
12481235
if (IsObject(item)) {
12491236
if (IsTemporalDate(item)) return item;
@@ -3326,9 +3313,9 @@ export function UnbalanceDurationRelative(
33263313
const sign = DurationSign(years, months, weeks, days, 0, 0, 0, 0, 0, 0);
33273314

33283315
let calendar;
3329-
let relativeTo: Temporal.PlainDateTime;
3316+
let relativeTo: Temporal.PlainDate;
33303317
if (relativeToParam) {
3331-
relativeTo = ToTemporalDateTime(relativeToParam);
3318+
relativeTo = ToTemporalDate(relativeToParam);
33323319
calendar = GetSlot(relativeTo, CALENDAR);
33333320
}
33343321

@@ -3428,9 +3415,9 @@ export function BalanceDurationRelative(
34283415
if (sign === 0) return { years, months, weeks, days };
34293416

34303417
let calendar;
3431-
let relativeTo: Temporal.PlainDateTime;
3418+
let relativeTo: Temporal.PlainDate;
34323419
if (relativeToParam) {
3433-
relativeTo = ToTemporalDateTime(relativeToParam);
3420+
relativeTo = ToTemporalDate(relativeToParam);
34343421
calendar = GetSlot(relativeTo, CALENDAR);
34353422
}
34363423

@@ -3470,16 +3457,15 @@ export function BalanceDurationRelative(
34703457
untilOptions.largestUnit = 'month';
34713458
let untilResult = CalendarDateUntil(calendar, relativeTo, newRelativeTo, untilOptions, dateUntil);
34723459
let oneYearMonths = GetSlot(untilResult, MONTHS);
3473-
let relativeToDateOnly: Temporal.PlainDateLike = relativeTo;
34743460
while (MathAbs(months) >= MathAbs(oneYearMonths)) {
34753461
months -= oneYearMonths;
34763462
years += sign;
3477-
relativeToDateOnly = newRelativeTo;
3463+
relativeTo = newRelativeTo;
34783464
const addOptions = ObjectCreate(null);
3479-
newRelativeTo = CalendarDateAdd(calendar, relativeToDateOnly, oneYear, addOptions, dateAdd);
3465+
newRelativeTo = CalendarDateAdd(calendar, relativeTo, oneYear, addOptions, dateAdd);
34803466
const untilOptions = ObjectCreate(null);
34813467
untilOptions.largestUnit = 'month';
3482-
untilResult = CalendarDateUntil(calendar, relativeToDateOnly, newRelativeTo, untilOptions, dateUntil);
3468+
untilResult = CalendarDateUntil(calendar, relativeTo, newRelativeTo, untilOptions, dateUntil);
34833469
oneYearMonths = GetSlot(untilResult, MONTHS);
34843470
}
34853471
break;
@@ -4131,28 +4117,22 @@ export function AddDuration(
41314117
ns1 + ns2,
41324118
largestUnit
41334119
));
4134-
} else if (IsTemporalDateTime(relativeTo)) {
4120+
} else if (IsTemporalDate(relativeTo)) {
41354121
const TemporalDuration = GetIntrinsic('%Temporal.Duration%');
41364122
const calendar = GetSlot(relativeTo, CALENDAR);
41374123

4138-
const datePart = CreateTemporalDate(
4139-
GetSlot(relativeTo, ISO_YEAR),
4140-
GetSlot(relativeTo, ISO_MONTH),
4141-
GetSlot(relativeTo, ISO_DAY),
4142-
calendar
4143-
);
41444124
const dateDuration1 = new TemporalDuration(y1, mon1, w1, d1, 0, 0, 0, 0, 0, 0);
41454125
const dateDuration2 = new TemporalDuration(y2, mon2, w2, d2, 0, 0, 0, 0, 0, 0);
41464126
const dateAdd = calendar.dateAdd;
41474127
const firstAddOptions = ObjectCreate(null);
4148-
const intermediate = CalendarDateAdd(calendar, datePart, dateDuration1, firstAddOptions, dateAdd);
4128+
const intermediate = CalendarDateAdd(calendar, relativeTo, dateDuration1, firstAddOptions, dateAdd);
41494129
const secondAddOptions = ObjectCreate(null);
41504130
const end = CalendarDateAdd(calendar, intermediate, dateDuration2, secondAddOptions, dateAdd);
41514131

41524132
const dateLargestUnit = LargerOfTwoTemporalUnits('day', largestUnit);
41534133
const differenceOptions = ObjectCreate(null);
41544134
differenceOptions.largestUnit = dateLargestUnit;
4155-
({ years, months, weeks, days } = CalendarDateUntil(calendar, datePart, end, differenceOptions));
4135+
({ years, months, weeks, days } = CalendarDateUntil(calendar, relativeTo, end, differenceOptions));
41564136
// Signs of date part and time part may not agree; balance them together
41574137
({ days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds } = BalanceDuration(
41584138
days,
@@ -4515,19 +4495,7 @@ function MoveRelativeDate(
45154495
const options = ObjectCreate(null);
45164496
const later = CalendarDateAdd(calendar, relativeToParam, duration, options);
45174497
const days = DaysUntil(relativeToParam, later);
4518-
const relativeTo = CreateTemporalDateTime(
4519-
GetSlot(later, ISO_YEAR),
4520-
GetSlot(later, ISO_MONTH),
4521-
GetSlot(later, ISO_DAY),
4522-
GetSlot(relativeToParam, ISO_HOUR),
4523-
GetSlot(relativeToParam, ISO_MINUTE),
4524-
GetSlot(relativeToParam, ISO_SECOND),
4525-
GetSlot(relativeToParam, ISO_MILLISECOND),
4526-
GetSlot(relativeToParam, ISO_MICROSECOND),
4527-
GetSlot(relativeToParam, ISO_NANOSECOND),
4528-
GetSlot(relativeToParam, CALENDAR)
4529-
);
4530-
return { relativeTo, days };
4498+
return { relativeTo: later, days };
45314499
}
45324500

45334501
export function MoveRelativeZonedDateTime(
@@ -4717,17 +4685,18 @@ export function RoundDuration(
47174685
const TemporalDuration = GetIntrinsic('%Temporal.Duration%');
47184686
let calendar, zdtRelative;
47194687
// if it's a ZDT, it will be reassigned to a PDT below.
4720-
let relativeTo = relativeToParam as Temporal.PlainDateTime;
4688+
let relativeTo: Parameters<typeof MoveRelativeDate>[1] = relativeToParam;
47214689
if (relativeToParam) {
47224690
if (IsTemporalZonedDateTime(relativeTo)) {
47234691
zdtRelative = relativeTo;
4724-
relativeTo = BuiltinTimeZoneGetPlainDateTimeFor(
4692+
const pdt = BuiltinTimeZoneGetPlainDateTimeFor(
47254693
GetSlot(relativeTo, TIME_ZONE),
47264694
GetSlot(relativeTo, INSTANT),
47274695
GetSlot(relativeTo, CALENDAR)
47284696
);
4729-
} else if (!IsTemporalDateTime(relativeTo)) {
4730-
throw new TypeError('starting point must be PlainDateTime or ZonedDateTime');
4697+
relativeTo = TemporalDateTimeToDate(pdt);
4698+
} else if (!IsTemporalDate(relativeTo)) {
4699+
throw new TypeError('starting point must be PlainDate or ZonedDateTime');
47314700
}
47324701
calendar = GetSlot(relativeTo, CALENDAR);
47334702
}
@@ -4762,41 +4731,21 @@ export function RoundDuration(
47624731
const secondAddOptions = ObjectCreate(null);
47634732
const yearsMonthsWeeksLater = CalendarDateAdd(calendar, relativeTo, yearsMonthsWeeks, secondAddOptions, dateAdd);
47644733
const monthsWeeksInDays = DaysUntil(yearsLater, yearsMonthsWeeksLater);
4765-
let relativeToDateOnly = yearsLater;
4734+
relativeTo = yearsLater;
47664735
days += monthsWeeksInDays;
47674736

47684737
const thirdAddOptions = ObjectCreate(null);
4769-
const daysLater = CalendarDateAdd(calendar, relativeToDateOnly, { days }, thirdAddOptions, dateAdd);
4738+
const daysLater = CalendarDateAdd(calendar, relativeTo, { days }, thirdAddOptions, dateAdd);
47704739
const untilOptions = ObjectCreate(null);
47714740
untilOptions.largestUnit = 'year';
4772-
const yearsPassed = CalendarDateUntil(calendar, relativeToDateOnly, daysLater, untilOptions).years;
4741+
const yearsPassed = CalendarDateUntil(calendar, relativeTo, daysLater, untilOptions).years;
47734742
years += yearsPassed;
4774-
const oldRelativeTo = relativeToDateOnly;
4743+
const oldRelativeTo = relativeTo;
47754744
const fourthAddOptions = ObjectCreate(null);
4776-
relativeToDateOnly = CalendarDateAdd(
4777-
calendar,
4778-
relativeToDateOnly,
4779-
{ years: yearsPassed },
4780-
fourthAddOptions,
4781-
dateAdd
4782-
);
4783-
const daysPassed = DaysUntil(oldRelativeTo, relativeToDateOnly);
4745+
relativeTo = CalendarDateAdd(calendar, relativeTo, { years: yearsPassed }, fourthAddOptions, dateAdd);
4746+
const daysPassed = DaysUntil(oldRelativeTo, relativeTo);
47844747
days -= daysPassed;
47854748
const oneYear = new TemporalDuration(days < 0 ? -1 : 1);
4786-
// This conversion is needed because of an issue in the spec that's likely to change.
4787-
// See https://github.com/tc39/proposal-temporal/issues/1821.
4788-
relativeTo = CreateTemporalDateTime(
4789-
GetSlot(relativeToDateOnly, ISO_YEAR),
4790-
GetSlot(relativeToDateOnly, ISO_MONTH),
4791-
GetSlot(relativeToDateOnly, ISO_DAY),
4792-
0,
4793-
0,
4794-
0,
4795-
0,
4796-
0,
4797-
0,
4798-
GetSlot(relativeTo, CALENDAR)
4799-
);
48004749
let { days: oneYearDays } = MoveRelativeDate(calendar, relativeTo, oneYear);
48014750

48024751
// Note that `nanoseconds` below (here and in similar code for months,
@@ -4828,20 +4777,7 @@ export function RoundDuration(
48284777
const secondAddOptions = ObjectCreate(null);
48294778
const yearsMonthsWeeksLater = CalendarDateAdd(calendar, relativeTo, yearsMonthsWeeks, secondAddOptions, dateAdd);
48304779
const weeksInDays = DaysUntil(yearsMonthsLater, yearsMonthsWeeksLater);
4831-
// This conversion is needed because of an issue in the spec that's likely to change.
4832-
// See https://github.com/tc39/proposal-temporal/issues/1821.
4833-
relativeTo = CreateTemporalDateTime(
4834-
GetSlot(yearsMonthsLater, ISO_YEAR),
4835-
GetSlot(yearsMonthsLater, ISO_MONTH),
4836-
GetSlot(yearsMonthsLater, ISO_DAY),
4837-
0,
4838-
0,
4839-
0,
4840-
0,
4841-
0,
4842-
0,
4843-
GetSlot(yearsMonthsLater, CALENDAR)
4844-
);
4780+
relativeTo = yearsMonthsLater;
48454781
days += weeksInDays;
48464782

48474783
// Months may be different lengths of days depending on the calendar,

lib/plaindate.ts

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -171,18 +171,6 @@ export class PlainDate implements Temporal.PlainDate {
171171
if (smallestUnit === 'day' && roundingIncrement === 1) return result;
172172

173173
let { years, months, weeks, days } = result;
174-
const relativeTo = ES.CreateTemporalDateTime(
175-
GetSlot(this, ISO_YEAR),
176-
GetSlot(this, ISO_MONTH),
177-
GetSlot(this, ISO_DAY),
178-
0,
179-
0,
180-
0,
181-
0,
182-
0,
183-
0,
184-
GetSlot(this, CALENDAR)
185-
);
186174
({ years, months, weeks, days } = ES.RoundDuration(
187175
years,
188176
months,
@@ -197,7 +185,7 @@ export class PlainDate implements Temporal.PlainDate {
197185
roundingIncrement,
198186
smallestUnit,
199187
roundingMode,
200-
relativeTo
188+
this
201189
));
202190

203191
const Duration = GetIntrinsic('%Temporal.Duration%');
@@ -228,18 +216,6 @@ export class PlainDate implements Temporal.PlainDate {
228216
if (smallestUnit === 'day' && roundingIncrement === 1) {
229217
return new Duration(-years, -months, -weeks, -days, 0, 0, 0, 0, 0, 0);
230218
}
231-
const relativeTo = ES.CreateTemporalDateTime(
232-
GetSlot(this, ISO_YEAR),
233-
GetSlot(this, ISO_MONTH),
234-
GetSlot(this, ISO_DAY),
235-
0,
236-
0,
237-
0,
238-
0,
239-
0,
240-
0,
241-
GetSlot(this, CALENDAR)
242-
);
243219
({ years, months, weeks, days } = ES.RoundDuration(
244220
years,
245221
months,
@@ -254,7 +230,7 @@ export class PlainDate implements Temporal.PlainDate {
254230
roundingIncrement,
255231
smallestUnit,
256232
ES.NegateTemporalRoundingMode(roundingMode),
257-
relativeTo
233+
this
258234
));
259235

260236
return new Duration(-years, -months, -weeks, -days, 0, 0, 0, 0, 0, 0);

lib/plaindatetime.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ export class PlainDateTime implements Temporal.PlainDateTime {
399399
options
400400
);
401401

402+
const relativeTo = ES.TemporalDateTimeToDate(this);
402403
({ years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds } =
403404
ES.RoundDuration(
404405
years,
@@ -414,7 +415,7 @@ export class PlainDateTime implements Temporal.PlainDateTime {
414415
roundingIncrement,
415416
smallestUnit,
416417
roundingMode,
417-
this
418+
relativeTo
418419
));
419420
({ days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds } = ES.BalanceDuration(
420421
days,
@@ -473,6 +474,7 @@ export class PlainDateTime implements Temporal.PlainDateTime {
473474
options
474475
);
475476

477+
const relativeTo = ES.TemporalDateTimeToDate(this);
476478
({ years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds } =
477479
ES.RoundDuration(
478480
years,
@@ -488,7 +490,7 @@ export class PlainDateTime implements Temporal.PlainDateTime {
488490
roundingIncrement,
489491
smallestUnit,
490492
ES.NegateTemporalRoundingMode(roundingMode),
491-
this
493+
relativeTo
492494
));
493495
({ days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds } = ES.BalanceDuration(
494496
days,

lib/plainyearmonth.ts

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -186,18 +186,6 @@ export class PlainYearMonth implements Temporal.PlainYearMonth {
186186
if (smallestUnit === 'month' && roundingIncrement === 1) return result;
187187

188188
let { years, months } = result;
189-
const relativeTo = ES.CreateTemporalDateTime(
190-
GetSlot(thisDate, ISO_YEAR),
191-
GetSlot(thisDate, ISO_MONTH),
192-
GetSlot(thisDate, ISO_DAY),
193-
0,
194-
0,
195-
0,
196-
0,
197-
0,
198-
0,
199-
calendar
200-
);
201189
({ years, months } = ES.RoundDuration(
202190
years,
203191
months,
@@ -212,7 +200,7 @@ export class PlainYearMonth implements Temporal.PlainYearMonth {
212200
roundingIncrement,
213201
smallestUnit,
214202
roundingMode,
215-
relativeTo
203+
thisDate
216204
));
217205

218206
const Duration = GetIntrinsic('%Temporal.Duration%');
@@ -249,18 +237,6 @@ export class PlainYearMonth implements Temporal.PlainYearMonth {
249237
if (smallestUnit === 'month' && roundingIncrement === 1) {
250238
return new Duration(-years, -months, 0, 0, 0, 0, 0, 0, 0, 0);
251239
}
252-
const relativeTo = ES.CreateTemporalDateTime(
253-
GetSlot(thisDate, ISO_YEAR),
254-
GetSlot(thisDate, ISO_MONTH),
255-
GetSlot(thisDate, ISO_DAY),
256-
0,
257-
0,
258-
0,
259-
0,
260-
0,
261-
0,
262-
calendar
263-
);
264240
({ years, months } = ES.RoundDuration(
265241
years,
266242
months,
@@ -275,7 +251,7 @@ export class PlainYearMonth implements Temporal.PlainYearMonth {
275251
roundingIncrement,
276252
smallestUnit,
277253
ES.NegateTemporalRoundingMode(roundingMode),
278-
relativeTo
254+
thisDate
279255
));
280256

281257
return new Duration(-years, -months, 0, 0, 0, 0, 0, 0, 0, 0);

0 commit comments

Comments
 (0)