Skip to content

Commit

Permalink
Merge 51e76f3 into 658a12c
Browse files Browse the repository at this point in the history
  • Loading branch information
ptomato authored Feb 26, 2021
2 parents 658a12c + 51e76f3 commit dd4a730
Show file tree
Hide file tree
Showing 18 changed files with 456 additions and 368 deletions.
4 changes: 2 additions & 2 deletions docs/calendar.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ Overriding all the other members is optional.
If you don't override the optional members, then they will behave as in the base calendar.

The other, more difficult, way to create a custom calendar is to create a plain object implementing the `Temporal.Calendar` protocol, without subclassing.
The object must implement all of the `Temporal.Calendar` properties and methods except for `id`, `fields()`, and `toJSON()`.
The object must implement all of the `Temporal.Calendar` properties and methods except for `id`, `fields()`, `mergeFields()`, and `toJSON()`.
Any object with the required methods will return the correct output from any Temporal property or method.
However, most other code will assume that custom calendars act like built-in `Temporal.Calendar` objects.
To interoperate with libraries or other code that you didn't write, then you should implement the `id` property and the `fields()` and `toJSON()` methods as well.
To interoperate with libraries or other code that you didn't write, then you should implement the `id` property and the `fields()`, `mergeFields()`, and `toJSON()` methods as well.
Your object must not have a `calendar` property, so that it can be distinguished in `Temporal.Calendar.from()` from other Temporal objects that have a calendar.

The identifier of a custom calendar must consist of one or more components of between 3 and 8 ASCII alphanumeric characters each, separated by dashes, as described in [Unicode Technical Standard 35](https://unicode.org/reports/tr35/tr35.html#Unicode_locale_identifier).
Expand Down
177 changes: 144 additions & 33 deletions polyfill/lib/ecmascript.mjs

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions polyfill/lib/intl.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ function formatRange(a, b) {
}
const { instant: aa, formatter: aformatter, timeZone: atz } = extractOverrides(a, this);
const { instant: bb, formatter: bformatter, timeZone: btz } = extractOverrides(b, this);
if (atz && btz && !ES.TimeZoneEquals(atz, btz)) {
if (atz && btz && atz !== btz) {
throw new RangeError('cannot format range between different time zones');
}
if (aa && bb && aformatter && bformatter && aformatter === bformatter) {
Expand All @@ -131,7 +131,7 @@ function formatRangeToParts(a, b) {
}
const { instant: aa, formatter: aformatter, timeZone: atz } = extractOverrides(a, this);
const { instant: bb, formatter: bformatter, timeZone: btz } = extractOverrides(b, this);
if (atz && btz && !ES.TimeZoneEquals(atz, btz)) {
if (atz && btz && atz !== btz) {
throw new RangeError('cannot format range between different time zones');
}
if (aa && bb && aformatter && bformatter && aformatter === bformatter) {
Expand Down Expand Up @@ -377,7 +377,7 @@ function extractOverrides(temporalObj, main) {
return {
instant: GetSlot(temporalObj, INSTANT),
formatter: main[ZONED],
timeZone
timeZone: objTimeZone
};
}

Expand Down
64 changes: 18 additions & 46 deletions polyfill/lib/plaindate.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -67,83 +67,59 @@ export class PlainDate {
}
get era() {
if (!ES.IsTemporalDate(this)) throw new TypeError('invalid receiver');
let result = GetSlot(this, CALENDAR).era(this);
if (result !== undefined) {
result = ES.ToString(result);
}
return result;
return ES.CalendarEra(GetSlot(this, CALENDAR), this);
}
get eraYear() {
if (!ES.IsTemporalDate(this)) throw new TypeError('invalid receiver');
let result = GetSlot(this, CALENDAR).eraYear(this);
if (result !== undefined) {
result = ES.ToInteger(result);
}
return result;
return ES.CalendarEraYear(GetSlot(this, CALENDAR), this);
}
get year() {
if (!ES.IsTemporalDate(this)) throw new TypeError('invalid receiver');
const result = GetSlot(this, CALENDAR).year(this);
if (result === undefined) {
throw new RangeError('calendar year result must be an integer');
}
return ES.ToInteger(result);
return ES.CalendarYear(GetSlot(this, CALENDAR), this);
}
get month() {
if (!ES.IsTemporalDate(this)) throw new TypeError('invalid receiver');
const result = GetSlot(this, CALENDAR).month(this);
if (result === undefined) {
throw new RangeError('calendar month result must be a positive integer');
}
return ES.ToPositiveInteger(result);
return ES.CalendarMonth(GetSlot(this, CALENDAR), this);
}
get monthCode() {
if (!ES.IsTemporalDate(this)) throw new TypeError('invalid receiver');
let result = GetSlot(this, CALENDAR).monthCode(this);
if (result === undefined) {
throw new RangeError('calendar monthCode result must be a string');
}
return ES.ToString(result);
return ES.CalendarMonthCode(GetSlot(this, CALENDAR), this);
}
get day() {
if (!ES.IsTemporalDate(this)) throw new TypeError('invalid receiver');
const result = GetSlot(this, CALENDAR).day(this);
if (result === undefined) {
throw new RangeError('calendar day result must be a positive integer');
}
return ES.ToPositiveInteger(result);
return ES.CalendarDay(GetSlot(this, CALENDAR), this);
}
get dayOfWeek() {
if (!ES.IsTemporalDate(this)) throw new TypeError('invalid receiver');
return GetSlot(this, CALENDAR).dayOfWeek(this);
return ES.CalendarDayOfWeek(GetSlot(this, CALENDAR), this);
}
get dayOfYear() {
if (!ES.IsTemporalDate(this)) throw new TypeError('invalid receiver');
return GetSlot(this, CALENDAR).dayOfYear(this);
return ES.CalendarDayOfYear(GetSlot(this, CALENDAR), this);
}
get weekOfYear() {
if (!ES.IsTemporalDate(this)) throw new TypeError('invalid receiver');
return GetSlot(this, CALENDAR).weekOfYear(this);
return ES.CalendarWeekOfYear(GetSlot(this, CALENDAR), this);
}
get daysInWeek() {
if (!ES.IsTemporalDate(this)) throw new TypeError('invalid receiver');
return GetSlot(this, CALENDAR).daysInWeek(this);
return ES.CalendarDaysInWeek(GetSlot(this, CALENDAR), this);
}
get daysInMonth() {
if (!ES.IsTemporalDate(this)) throw new TypeError('invalid receiver');
return GetSlot(this, CALENDAR).daysInMonth(this);
return ES.CalendarDaysInMonth(GetSlot(this, CALENDAR), this);
}
get daysInYear() {
if (!ES.IsTemporalDate(this)) throw new TypeError('invalid receiver');
return GetSlot(this, CALENDAR).daysInYear(this);
return ES.CalendarDaysInYear(GetSlot(this, CALENDAR), this);
}
get monthsInYear() {
if (!ES.IsTemporalDate(this)) throw new TypeError('invalid receiver');
return GetSlot(this, CALENDAR).monthsInYear(this);
return ES.CalendarMonthsInYear(GetSlot(this, CALENDAR), this);
}
get inLeapYear() {
if (!ES.IsTemporalDate(this)) throw new TypeError('invalid receiver');
return GetSlot(this, CALENDAR).inLeapYear(this);
return ES.CalendarInLeapYear(GetSlot(this, CALENDAR), this);
}
with(temporalDateLike, options = undefined) {
if (!ES.IsTemporalDate(this)) throw new TypeError('invalid receiver');
Expand Down Expand Up @@ -190,9 +166,7 @@ export class PlainDate {
({ days } = ES.BalanceDuration(days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds, 'days'));
duration = { years, months, weeks, days };
const Construct = ES.SpeciesConstructor(this, PlainDate);
const result = GetSlot(this, CALENDAR).dateAdd(this, duration, options, Construct);
if (!ES.IsTemporalDate(result)) throw new TypeError('invalid result');
return result;
return ES.CalendarDateAdd(GetSlot(this, CALENDAR), this, duration, options, Construct);
}
subtract(temporalDurationLike, options = undefined) {
if (!ES.IsTemporalDate(this)) throw new TypeError('invalid receiver');
Expand All @@ -205,9 +179,7 @@ export class PlainDate {
({ days } = ES.BalanceDuration(days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds, 'days'));
duration = { years: -years, months: -months, weeks: -weeks, days: -days };
const Construct = ES.SpeciesConstructor(this, PlainDate);
const result = GetSlot(this, CALENDAR).dateAdd(this, duration, options, Construct);
if (!ES.IsTemporalDate(result)) throw new TypeError('invalid result');
return result;
return ES.CalendarDateAdd(GetSlot(this, CALENDAR), this, duration, options, Construct);
}
until(other, options = undefined) {
if (!ES.IsTemporalDate(this)) throw new TypeError('invalid receiver');
Expand All @@ -229,7 +201,7 @@ export class PlainDate {
const roundingMode = ES.ToTemporalRoundingMode(options, 'trunc');
const roundingIncrement = ES.ToTemporalRoundingIncrement(options, undefined, false);

const result = calendar.dateUntil(this, other, options);
const result = ES.CalendarDateUntil(calendar, this, other, options);
if (smallestUnit === 'days' && roundingIncrement === 1) return result;

let { years, months, weeks, days } = result;
Expand Down Expand Up @@ -286,7 +258,7 @@ export class PlainDate {
const roundingMode = ES.ToTemporalRoundingMode(options, 'trunc');
const roundingIncrement = ES.ToTemporalRoundingIncrement(options, undefined, false);

let { years, months, weeks, days } = calendar.dateUntil(this, other, options);
let { years, months, weeks, days } = ES.CalendarDateUntil(calendar, this, other, options);
const Duration = GetIntrinsic('%Temporal.Duration%');
if (smallestUnit === 'days' && roundingIncrement === 1) {
return new Duration(-years, -months, -weeks, -days, 0, 0, 0, 0, 0, 0);
Expand Down
50 changes: 14 additions & 36 deletions polyfill/lib/plaindatetime.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -122,33 +122,19 @@ export class PlainDateTime {
}
get year() {
if (!ES.IsTemporalDateTime(this)) throw new TypeError('invalid receiver');
const result = GetSlot(this, CALENDAR).year(this);
if (result === undefined) {
throw new RangeError('calendar year result must be an integer');
}
return ES.ToInteger(result);
return ES.CalendarYear(GetSlot(this, CALENDAR), this);
}
get month() {
if (!ES.IsTemporalDateTime(this)) throw new TypeError('invalid receiver');
const result = GetSlot(this, CALENDAR).month(this);
if (result === undefined) {
throw new RangeError('calendar month result must be a positive integer');
}
return ES.ToPositiveInteger(result);
return ES.CalendarMonth(GetSlot(this, CALENDAR), this);
}
get monthCode() {
if (!ES.IsTemporalDateTime(this)) throw new TypeError('invalid receiver');
let result = GetSlot(this, CALENDAR).monthCode(this);
if (result !== undefined) result = ES.ToString(result);
return result;
return ES.CalendarMonthCode(GetSlot(this, CALENDAR), this);
}
get day() {
if (!ES.IsTemporalDateTime(this)) throw new TypeError('invalid receiver');
const result = GetSlot(this, CALENDAR).day(this);
if (result === undefined) {
throw new RangeError('calendar day result must be a positive integer');
}
return ES.ToPositiveInteger(result);
return ES.CalendarDay(GetSlot(this, CALENDAR), this);
}
get hour() {
if (!ES.IsTemporalDateTime(this)) throw new TypeError('invalid receiver');
Expand Down Expand Up @@ -176,51 +162,43 @@ export class PlainDateTime {
}
get era() {
if (!ES.IsTemporalDateTime(this)) throw new TypeError('invalid receiver');
let result = GetSlot(this, CALENDAR).era(this);
if (result !== undefined) {
result = ES.ToString(result);
}
return result;
return ES.CalendarEra(GetSlot(this, CALENDAR), this);
}
get eraYear() {
if (!ES.IsTemporalDateTime(this)) throw new TypeError('invalid receiver');
let result = GetSlot(this, CALENDAR).eraYear(this);
if (result !== undefined) {
result = ES.ToInteger(result);
}
return result;
return ES.CalendarEraYear(GetSlot(this, CALENDAR), this);
}
get dayOfWeek() {
if (!ES.IsTemporalDateTime(this)) throw new TypeError('invalid receiver');
return GetSlot(this, CALENDAR).dayOfWeek(this);
return ES.CalendarDayOfWeek(GetSlot(this, CALENDAR), this);
}
get dayOfYear() {
if (!ES.IsTemporalDateTime(this)) throw new TypeError('invalid receiver');
return GetSlot(this, CALENDAR).dayOfYear(this);
return ES.CalendarDayOfYear(GetSlot(this, CALENDAR), this);
}
get weekOfYear() {
if (!ES.IsTemporalDateTime(this)) throw new TypeError('invalid receiver');
return GetSlot(this, CALENDAR).weekOfYear(this);
return ES.CalendarWeekOfYear(GetSlot(this, CALENDAR), this);
}
get daysInWeek() {
if (!ES.IsTemporalDateTime(this)) throw new TypeError('invalid receiver');
return GetSlot(this, CALENDAR).daysInWeek(this);
return ES.CalendarDaysInWeek(GetSlot(this, CALENDAR), this);
}
get daysInYear() {
if (!ES.IsTemporalDateTime(this)) throw new TypeError('invalid receiver');
return GetSlot(this, CALENDAR).daysInYear(this);
return ES.CalendarDaysInYear(GetSlot(this, CALENDAR), this);
}
get daysInMonth() {
if (!ES.IsTemporalDateTime(this)) throw new TypeError('invalid receiver');
return GetSlot(this, CALENDAR).daysInMonth(this);
return ES.CalendarDaysInMonth(GetSlot(this, CALENDAR), this);
}
get monthsInYear() {
if (!ES.IsTemporalDateTime(this)) throw new TypeError('invalid receiver');
return GetSlot(this, CALENDAR).monthsInYear(this);
return ES.CalendarMonthsInYear(GetSlot(this, CALENDAR), this);
}
get inLeapYear() {
if (!ES.IsTemporalDateTime(this)) throw new TypeError('invalid receiver');
return GetSlot(this, CALENDAR).inLeapYear(this);
return ES.CalendarInLeapYear(GetSlot(this, CALENDAR), this);
}
with(temporalDateTimeLike, options = undefined) {
if (!ES.IsTemporalDateTime(this)) throw new TypeError('invalid receiver');
Expand Down
12 changes: 2 additions & 10 deletions polyfill/lib/plainmonthday.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,11 @@ export class PlainMonthDay {

get monthCode() {
if (!ES.IsTemporalMonthDay(this)) throw new TypeError('invalid receiver');
const result = GetSlot(this, CALENDAR).monthCode(this);
if (result === undefined) {
throw new RangeError('calendar monthCode result must be a string');
}
return ES.ToString(result);
return ES.CalendarMonthCode(GetSlot(this, CALENDAR), this);
}
get day() {
if (!ES.IsTemporalMonthDay(this)) throw new TypeError('invalid receiver');
const result = GetSlot(this, CALENDAR).day(this);
if (result === undefined) {
throw new RangeError('calendar day result must be a positive integer');
}
return ES.ToPositiveInteger(result);
return ES.CalendarDay(GetSlot(this, CALENDAR), this);
}
get calendar() {
if (!ES.IsTemporalMonthDay(this)) throw new TypeError('invalid receiver');
Expand Down
Loading

0 comments on commit dd4a730

Please sign in to comment.