Skip to content

Commit

Permalink
Update PrepareTemporalFields to match the spec.
Browse files Browse the repository at this point in the history
UPSTREAM_COMMIT=1e9ea70f694b2afc0f18a1faae64a8b3cf0604b6

Co-authored-by: James Wright <[email protected]>
  • Loading branch information
Ms2ger and 12wrigja committed Oct 4, 2022
1 parent 2fc29a7 commit f25be3a
Show file tree
Hide file tree
Showing 9 changed files with 199 additions and 324 deletions.
54 changes: 16 additions & 38 deletions lib/calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,33 +346,23 @@ DefineIntrinsic('Temporal.Calendar.from', Calendar.from);
impl['iso8601'] = {
dateFromFields(fieldsParam, options, calendar) {
const overflow = ES.ToTemporalOverflow(options);
let fields = ES.PrepareTemporalFields(fieldsParam, [
['day'],
['month', undefined],
['monthCode', undefined],
['year']
]);
let fields = ES.PrepareTemporalFields(fieldsParam, ['day', 'month', 'monthCode', 'year'], ['year', 'day']);
fields = resolveNonLunisolarMonth(fields);
let { year, month, day } = fields;
({ year, month, day } = ES.RegulateISODate(year, month, day, overflow));
return ES.CreateTemporalDate(year, month, day, calendar);
},
yearMonthFromFields(fieldsParam, options, calendar) {
const overflow = ES.ToTemporalOverflow(options);
let fields = ES.PrepareTemporalFields(fieldsParam, [['month', undefined], ['monthCode', undefined], ['year']]);
let fields = ES.PrepareTemporalFields(fieldsParam, ['month', 'monthCode', 'year'], ['year']);
fields = resolveNonLunisolarMonth(fields);
let { year, month } = fields;
({ year, month } = ES.RegulateISOYearMonth(year, month, overflow));
return ES.CreateTemporalYearMonth(year, month, calendar, /* referenceISODay = */ 1);
},
monthDayFromFields(fieldsParam, options, calendar) {
const overflow = ES.ToTemporalOverflow(options);
let fields = ES.PrepareTemporalFields(fieldsParam, [
['day'],
['month', undefined],
['monthCode', undefined],
['year', undefined]
]);
let fields = ES.PrepareTemporalFields(fieldsParam, ['day', 'month', 'monthCode', 'year'], ['day']);
if (fields.month !== undefined && fields.year === undefined && fields.monthCode === undefined) {
throw new TypeError('either year or monthCode required with month');
}
Expand Down Expand Up @@ -1028,8 +1018,8 @@ abstract class HelperBase {
compareCalendarDates(date1Param: Partial<CalendarYMD>, date2Param: Partial<CalendarYMD>): 0 | 1 | -1 {
// `date1` and `date2` are already records. The calls below simply validate
// that all three required fields are present.
const date1 = ES.PrepareTemporalFields(date1Param, [['day'], ['month'], ['year']]);
const date2 = ES.PrepareTemporalFields(date2Param, [['day'], ['month'], ['year']]);
const date1 = ES.PrepareTemporalFields(date1Param, ['day', 'month', 'year'], ['day', 'month', 'year']);
const date2 = ES.PrepareTemporalFields(date2Param, ['day', 'month', 'year'], ['day', 'month', 'year']);
if (date1.year !== date2.year) return ES.ComparisonResult(date1.year - date2.year);
if (date1.month !== date2.month) return ES.ComparisonResult(date1.month - date2.month);
if (date1.day !== date2.day) return ES.ComparisonResult(date1.day - date2.day);
Expand Down Expand Up @@ -2296,14 +2286,11 @@ const nonIsoImpl: NonIsoImpl = {
const overflow = ES.ToTemporalOverflow(options);
const cache = new OneObjectCache();
// Intentionally alphabetical
const fields = ES.PrepareTemporalFields(fieldsParam, [
['day'],
['era', undefined],
['eraYear', undefined],
['month', undefined],
['monthCode', undefined],
['year', undefined]
]);
const fields = ES.PrepareTemporalFields(
fieldsParam,
['day', 'era', 'eraYear', 'month', 'monthCode', 'year'],
['day']
);
const { year, month, day } = this.helper.calendarToIsoDate(fields, overflow, cache);
const result = ES.CreateTemporalDate(year, month, day, calendar);
cache.setObject(result);
Expand All @@ -2313,13 +2300,7 @@ const nonIsoImpl: NonIsoImpl = {
const overflow = ES.ToTemporalOverflow(options);
const cache = new OneObjectCache();
// Intentionally alphabetical
const fields = ES.PrepareTemporalFields(fieldsParam, [
['era', undefined],
['eraYear', undefined],
['month', undefined],
['monthCode', undefined],
['year', undefined]
]);
const fields = ES.PrepareTemporalFields(fieldsParam, ['era', 'eraYear', 'month', 'monthCode', 'year'], []);
const { year, month, day } = this.helper.calendarToIsoDate({ ...fields, day: 1 }, overflow, cache);
const result = ES.CreateTemporalYearMonth(year, month, calendar, /* referenceISODay = */ day);
cache.setObject(result);
Expand All @@ -2336,14 +2317,11 @@ const nonIsoImpl: NonIsoImpl = {
// or `year` must be provided because `month` is ambiguous without a year or
// a code.
const cache = new OneObjectCache();
const fields = ES.PrepareTemporalFields(fieldsParam, [
['day'],
['era', undefined],
['eraYear', undefined],
['month', undefined],
['monthCode', undefined],
['year', undefined]
]);
const fields = ES.PrepareTemporalFields(
fieldsParam,
['day', 'era', 'eraYear', 'month', 'monthCode', 'year'],
['day']
);
const { year, month, day } = this.helper.monthDayFromFields(fields, overflow, cache);
// `year` is a reference year where this month/day exists in this calendar
const result = ES.CreateTemporalMonthDay(month, day, calendar, /* referenceISOYear = */ year);
Expand Down
Loading

0 comments on commit f25be3a

Please sign in to comment.