Skip to content

Commit

Permalink
Date.withCalendar and DateTime.withCalendar
Browse files Browse the repository at this point in the history
More efficient versions of with({calendar: calendar}). Spec for these
methods is not written yet.
  • Loading branch information
ptomato committed Jun 3, 2020
1 parent 2cf9668 commit 799cc50
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 0 deletions.
15 changes: 15 additions & 0 deletions docs/date.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,21 @@ date = Temporal.Date.from('2006-08-24');
date.with({day: 1, month: date.month + 1}, { disambiguation: 'balance' }) // => 2006-09-01
```

### date.**withCalendar**(_calendar_: Temporal.Calendar | string) : Temporal.Date

**Parameters:**
- `calendar` (`Temporal.Calendar` or string): The calendar into which to project `date`.

**Returns:** a new `Temporal.Date` object which is the date indicated by `date`, projected into `calendar`.

This method is the same as `date.with({ calendar })`, but may be more efficient.

Usage example:
```javascript
date = Temporal.Date.from('2006-08-24[c=japanese]');
date.withCalendar('iso8601') // => 2006-08-24
```

### date.**plus**(_duration_: object, _options_?: object) : Temporal.Date

**Parameters:**
Expand Down
15 changes: 15 additions & 0 deletions docs/datetime.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,21 @@ dt = new Temporal.DateTime(1995, 12, 7, 3, 24, 30, 0, 3, 500);
dt.with({year: 2015, second: 31}) // => 2015-12-07T03:24:31.000003500
```

### datetime.**withCalendar**(_calendar_: Temporal.Calendar | string) : Temporal.DateTime

**Parameters:**
- `calendar` (`Temporal.Calendar` or string): The calendar into which to project `datetime`.

**Returns:** a new `Temporal.DateTime` object which is the date indicated by `datetime`, projected into `calendar`.

This method is the same as `datetime.with({ calendar })`, but may be more efficient.

Usage example:
```javascript
dt = Temporal.DateTime.from('1995-12-07T03:24:30.000003500[c=japanese]');
dt.withCalendar('iso8601') // => 1995-12-07T03:24:30.000003500
```

### datetime.**plus**(_duration_: object, _options_?: object) : Temporal.DateTime

**Parameters:**
Expand Down
2 changes: 2 additions & 0 deletions polyfill/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ export namespace Temporal {
readonly isLeapYear: boolean;
equals(other: Temporal.Date): boolean;
with(dateLike: DateLike, options?: AssignmentOptions): Temporal.Date;
withCalendar(calendar: string | Temporal.Calendar): Temporal.Date;
plus(durationLike: Temporal.Duration | DurationLike, options?: ArithmeticOptions): Temporal.Date;
minus(durationLike: Temporal.Duration | DurationLike, options?: ArithmeticOptions): Temporal.Date;
difference(other: Temporal.Date, options?: DifferenceOptions<'years' | 'months' | 'days'>): Temporal.Duration;
Expand Down Expand Up @@ -375,6 +376,7 @@ export namespace Temporal {
readonly isLeapYear: boolean;
equals(other: Temporal.DateTime): boolean;
with(dateTimeLike: DateTimeLike, options?: AssignmentOptions): Temporal.DateTime;
withCalendar(calendar: string | Temporal.Calendar): Temporal.DateTime;
plus(durationLike: Temporal.Duration | DurationLike, options?: ArithmeticOptions): Temporal.DateTime;
minus(durationLike: Temporal.Duration | DurationLike, options?: ArithmeticOptions): Temporal.DateTime;
difference(
Expand Down
8 changes: 8 additions & 0 deletions polyfill/lib/date.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ export class Date {
if (!ES.IsTemporalDate(result)) throw new TypeError('invalid result');
return result;
}
withCalendar(calendar) {
if (!ES.IsTemporalDate(this)) throw new TypeError('invalid receiver');
calendar = ES.ToTemporalCalendar(calendar);
const Construct = ES.SpeciesConstructor(this, Date);
const result = new Construct(GetSlot(this, ISO_YEAR), GetSlot(this, ISO_MONTH), GetSlot(this, ISO_DAY), calendar);
if (!ES.IsTemporalDate(result)) throw new TypeError('invalid result');
return result;
}
plus(temporalDurationLike, options) {
if (!ES.IsTemporalDate(this)) throw new TypeError('invalid receiver');
const duration = ES.ToLimitedTemporalDuration(temporalDurationLike);
Expand Down
19 changes: 19 additions & 0 deletions polyfill/lib/datetime.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,25 @@ export class DateTime {
if (!ES.IsTemporalDateTime(result)) throw new TypeError('invalid result');
return result;
}
withCalendar(calendar) {
if (!ES.IsTemporalDateTime(this)) throw new TypeError('invalid receiver');
calendar = ES.ToTemporalCalendar(calendar);
const Construct = ES.SpeciesConstructor(this, DateTime);
const result = new Construct(
GetSlot(this, ISO_YEAR),
GetSlot(this, ISO_MONTH),
GetSlot(this, ISO_DAY),
GetSlot(this, HOUR),
GetSlot(this, MINUTE),
GetSlot(this, SECOND),
GetSlot(this, MILLISECOND),
GetSlot(this, MICROSECOND),
GetSlot(this, NANOSECOND),
calendar
);
if (!ES.IsTemporalDateTime(result)) throw new TypeError('invalid result');
return result;
}
plus(temporalDurationLike, options) {
if (!ES.IsTemporalDateTime(this)) throw new TypeError('invalid receiver');
const disambiguation = ES.ToArithmeticTemporalDisambiguation(options);
Expand Down
10 changes: 10 additions & 0 deletions polyfill/test/date.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,16 @@ describe('Date', () => {
equal(Date.compare(d1, d2), 0);
});
});
describe('date.withCalendar()', () => {
const d1 = Date.from('1976-11-18');
it('works', () => {
const calendar = Temporal.Calendar.from('iso8601');
equal(`${d1.withCalendar(calendar)}`, '1976-11-18');
});
it('casts its argument', () => {
equal(`${d1.withCalendar('iso8601')}`, '1976-11-18');
});
});
});

import { normalize } from 'path';
Expand Down
10 changes: 10 additions & 0 deletions polyfill/test/datetime.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,16 @@ describe('DateTime', () => {
equal(DateTime.compare(dt1, dt2), 0);
});
});
describe('dateTime.withCalendar()', () => {
const dt1 = DateTime.from('1976-11-18T15:23:30.123456789');
it('works', () => {
const calendar = Temporal.Calendar.from('iso8601');
equal(`${dt1.withCalendar(calendar)}`, '1976-11-18T15:23:30.123456789');
});
it('casts its argument', () => {
equal(`${dt1.withCalendar('iso8601')}`, '1976-11-18T15:23:30.123456789');
});
});
});

import { normalize } from 'path';
Expand Down

0 comments on commit 799cc50

Please sign in to comment.