Skip to content

Commit

Permalink
Revert "Remove era from main spec"
Browse files Browse the repository at this point in the history
This partially reverts commit a7322a6.
'era' is still not present in the spec text, but is present in the
polyfill since this environment includes Intl.

Closes: #1046
  • Loading branch information
ptomato committed Nov 16, 2020
1 parent 3fb0ebb commit 804d218
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 28 deletions.
5 changes: 5 additions & 0 deletions docs/plaindate.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ date.day; // => 24

The `calendar` read-only property gives the calendar that the `year`, `month`, and `day` properties are interpreted in.

### date.**era** : unknown

The `era` read-only property is `undefined` when using the ISO 8601 calendar.
It's used for calendar systems that specify an era in addition to the year.

### date.**dayOfWeek** : number

The `dayOfWeek` read-only property gives the weekday number that the date falls on.
Expand Down
5 changes: 5 additions & 0 deletions docs/plaindatetime.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,11 @@ dt.nanosecond; // => 500

The `calendar` read-only property gives the calendar that the `year`, `month`, `day`, `hour`, `minute`, `second`, `millisecond`, `microsecond`, and `nanosecond` properties are interpreted in.

### datetime.**era** : unknown

The `era` read-only property is `undefined` when using the ISO 8601 calendar.
It's used for calendar systems that specify an era in addition to the year.

### datetime.**dayOfWeek** : number

The `dayOfWeek` read-only property gives the weekday number that the date falls on.
Expand Down
5 changes: 5 additions & 0 deletions docs/plainyearmonth.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ ym.month; // => 6

The `calendar` read-only property gives the calendar that the `year` and `month` properties are interpreted in.

### yearmonth.**era** : unknown

The `era` read-only property is `undefined` when using the ISO 8601 calendar.
It's used for calendar systems that specify an era in addition to the year.

### yearMonth.**daysInMonth** : number

The `daysInMonth` read-only property gives the number of days in the month.
Expand Down
5 changes: 5 additions & 0 deletions docs/zoneddatetime.md
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,11 @@ zdt.with({ timeZone: 'GMT' }).timeZone;
```
<!-- prettier-ignore-end -->

### zonedDateTime.**era** : unknown

The `era` read-only property is `undefined` when using the ISO 8601 calendar.
It's used for calendar systems that specify an era in addition to the year.

### zonedDateTime.**dayOfWeek** : number

The `dayOfWeek` read-only property gives the weekday number that the date falls on.
Expand Down
36 changes: 8 additions & 28 deletions polyfill/lib/calendar.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import { ES } from './ecmascript.mjs';
import { GetIntrinsic, MakeIntrinsicClass, DefineIntrinsic } from './intrinsicclass.mjs';
import * as REGEX from './regex.mjs';
import {
CALENDAR,
CALENDAR_ID,
INSTANT,
ISO_YEAR,
ISO_MONTH,
ISO_DAY,
Expand All @@ -16,7 +14,6 @@ import {
ISO_MILLISECOND,
ISO_MICROSECOND,
ISO_NANOSECOND,
TIME_ZONE,
CreateSlots,
GetSlot,
HasSlot,
Expand Down Expand Up @@ -110,6 +107,10 @@ export class Calendar {
void date;
throw new Error('not implemented');
}
era(date) {
void date;
throw new Error('not implemented');
}
dayOfWeek(date) {
void date;
throw new Error('not implemented');
Expand Down Expand Up @@ -361,6 +362,10 @@ class ISO8601Calendar extends Calendar {
if (!HasSlot(date, ISO_DAY)) date = ES.ToTemporalDate(date, GetIntrinsic('%Temporal.PlainDate%'));
return GetSlot(date, ISO_DAY);
}
era(date) {
if (!HasSlot(date, ISO_YEAR)) date = ES.ToTemporalDate(date, GetIntrinsic('%Temporal.Date%'));
return undefined;
}
hour(time) {
if (!ES.IsTemporalCalendar(this)) throw new TypeError('invalid receiver');
return GetSlot(time, ISO_HOUR);
Expand Down Expand Up @@ -433,29 +438,6 @@ MakeIntrinsicClass(ISO8601Calendar, 'Temporal.ISO8601Calendar');
// proposal for ECMA-262. These calendars will be standardized as part of
// ECMA-402.

function addCustomPropertyGetter(type, name) {
Object.defineProperty(GetIntrinsic(`%Temporal.${type}.prototype%`), name, {
get() {
return GetSlot(this, CALENDAR)[name](this);
},
configurable: true
});
}

function addEraProperties() {
addCustomPropertyGetter('PlainDate', 'era');
addCustomPropertyGetter('PlainDateTime', 'era');
addCustomPropertyGetter('PlainYearMonth', 'era');
Object.defineProperty(GetIntrinsic('%Temporal.ZonedDateTime.prototype%'), 'era', {
get() {
const calendar = GetSlot(this, CALENDAR);
const dateTime = ES.GetTemporalDateTimeFor(GetSlot(this, TIME_ZONE), GetSlot(this, INSTANT), calendar);
return calendar.era(dateTime);
},
configurable: true
});
}

// Implementation details for Gregorian calendar
const gre = {
isoYear(year, era) {
Expand All @@ -469,7 +451,6 @@ const gre = {
class Gregorian extends ISO8601Calendar {
constructor() {
super('gregory');
addEraProperties();
}

era(date) {
Expand Down Expand Up @@ -567,7 +548,6 @@ const jpn = {
class Japanese extends ISO8601Calendar {
constructor() {
super('japanese');
addEraProperties();
}

era(date) {
Expand Down
4 changes: 4 additions & 0 deletions polyfill/lib/plaindate.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ export class PlainDate {
if (!ES.IsTemporalDate(this)) throw new TypeError('invalid receiver');
return GetSlot(this, CALENDAR);
}
get era() {
if (!ES.IsTemporalDate(this)) throw new TypeError('invalid receiver');
return GetSlot(this, CALENDAR).era(this);
}
get year() {
if (!ES.IsTemporalDate(this)) throw new TypeError('invalid receiver');
return GetSlot(this, CALENDAR).year(this);
Expand Down
4 changes: 4 additions & 0 deletions polyfill/lib/plaindatetime.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ export class PlainDateTime {
if (!ES.IsTemporalDateTime(this)) throw new TypeError('invalid receiver');
return GetSlot(this, CALENDAR).nanosecond(this);
}
get era() {
if (!ES.IsTemporalDateTime(this)) throw new TypeError('invalid receiver');
return GetSlot(this, CALENDAR).era(this);
}
get dayOfWeek() {
if (!ES.IsTemporalDateTime(this)) throw new TypeError('invalid receiver');
return GetSlot(this, CALENDAR).dayOfWeek(this);
Expand Down
4 changes: 4 additions & 0 deletions polyfill/lib/plainyearmonth.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ export class PlainYearMonth {
if (!ES.IsTemporalYearMonth(this)) throw new TypeError('invalid receiver');
return GetSlot(this, CALENDAR);
}
get era() {
if (!ES.IsTemporalYearMonth(this)) throw new TypeError('invalid receiver');
return GetSlot(this, CALENDAR).era(this);
}
get daysInMonth() {
if (!ES.IsTemporalYearMonth(this)) throw new TypeError('invalid receiver');
return GetSlot(this, CALENDAR).daysInMonth(this);
Expand Down
4 changes: 4 additions & 0 deletions polyfill/lib/zoneddatetime.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ export class ZonedDateTime {
if (!ES.IsTemporalZonedDateTime(this)) throw new TypeError('invalid receiver');
return GetSlot(dateTime(this), ISO_NANOSECOND);
}
get era() {
if (!ES.IsTemporalZonedDateTime(this)) throw new TypeError('invalid receiver');
return GetSlot(this, CALENDAR).era(dateTime(this));
}
get epochSeconds() {
if (!ES.IsTemporalZonedDateTime(this)) throw new TypeError('invalid receiver');
const value = GetSlot(this, EPOCHNANOSECONDS);
Expand Down
3 changes: 3 additions & 0 deletions polyfill/test/calendar.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ describe('Calendar', () => {
it('Calendar.prototype.day is a Function', () => {
equal(typeof Calendar.prototype.day, 'function');
});
it('Calendar.prototype.era is a Function', () => {
equal(typeof Calendar.prototype.era, 'function');
});
it('Calendar.prototype.dayOfWeek is a Function', () => {
equal(typeof Calendar.prototype.dayOfWeek, 'function');
});
Expand Down

0 comments on commit 804d218

Please sign in to comment.