Skip to content

Commit

Permalink
Update with new toXxx method names
Browse files Browse the repository at this point in the history
- Convert all conversion methods to use new toXxx naming convention
- Fix Test262 tests broken by build changes in tc39#715
  • Loading branch information
justingrant committed Oct 21, 2020
1 parent 4fc4300 commit 8f767b3
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 173 deletions.
52 changes: 26 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions polyfill/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -536,14 +536,14 @@ export namespace Temporal {
| /** @deprecated */ 'day'
>
): Temporal.Duration;
toDateTime(temporalTime: Temporal.Time): Temporal.DateTime;
toYearMonth(): Temporal.YearMonth;
toMonthDay(): Temporal.MonthDay;
toLocalDateTime(
tzLike: TimeZoneProtocol | string,
temporalTime: Temporal.Time,
options?: ToAbsoluteOptions
): Temporal.LocalDateTime;
toDateTime(temporalTime: Temporal.Time): Temporal.DateTime;
toYearMonth(): Temporal.YearMonth;
toMonthDay(): Temporal.MonthDay;
getFields(): DateFields;
getISOFields(): DateISOFields;
toLocaleString(locales?: string | string[], options?: Intl.DateTimeFormatOptions): string;
Expand Down
59 changes: 29 additions & 30 deletions polyfill/lib/localdatetime.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import ES2019 from 'es-abstract/es2019.js';
import { GetIntrinsic, MakeIntrinsicClass } from './intrinsicclass.mjs';
import ToInteger from 'es-abstract/2019/ToInteger.js';
import ToObject from 'es-abstract/2019/ToObject.js';
import ToString from 'es-abstract/2019/ToString.js';

const Temporal = {
get DateTime() {
Expand Down Expand Up @@ -35,7 +37,7 @@ function fromObject(item, options) {
// Simplest case: absolute + time zone + optional calendar
if (absolute !== undefined) {
const abs = Temporal.Absolute.from(absolute);
const dt = abs.inTimeZone(tz, cal);
const dt = abs.toDateTime(tz, cal);
if (timeZoneOffsetNanoseconds !== undefined) {
checkCompareProperty('timeZoneOffsetNanoseconds', timeZoneOffsetNanoseconds, tz.getOffsetNanosecondsFor(abs), tz);
}
Expand All @@ -53,15 +55,15 @@ function fromObject(item, options) {
// Simple case: no time zone offset, so use user-supplied disambiguation options
// TODO: edit below depending on https://github.com/tc39/proposal-temporal/issues/607
const dt = Temporal.DateTime.from(item, { disambiguation: overflow });
const abs = dt.inTimeZone(tz, { disambiguation });
const abs = dt.toAbsolute(tz, { disambiguation });
return new LocalDateTime(abs, tz, dt.calendar);
} else {
// There is a time zone offset, so we'll have to pick the correct `Absolute`
// matching that offset.
// TODO: edit below depending on https://github.com/tc39/proposal-temporal/issues/607
const dt = Temporal.DateTime.from(item, { disambiguation: overflow });
const earlier = dt.inTimeZone(tz, { disambiguation: 'earlier' });
const later = dt.inTimeZone(tz, { disambiguation: 'later' });
const earlier = dt.toAbsolute(tz, { disambiguation: 'earlier' });
const later = dt.toAbsolute(tz, { disambiguation: 'later' });
if (Temporal.Absolute.compare(earlier, later) === 0) {
// no ambiguity
return new LocalDateTime(earlier, tz, dt.calendar);
Expand All @@ -71,7 +73,7 @@ function fromObject(item, options) {

if (disambiguation === 'reject') {
// delegate the exception throwing to Temporal.DateTime
dt.inTimeZone(tz, { disambiguation });
dt.toAbsolute(tz, { disambiguation });
throw new Error('This code should be unreachable');
}

Expand Down Expand Up @@ -132,7 +134,7 @@ function fromIsoString(isoString, options) {
if (!offset) {
// Simple case: ISO string without a TZ offset, so just convert the DateTime
// to Absolute in the given time zone.
const absolute = dt.inTimeZone(timeZone, { disambiguation });
const absolute = dt.toAbsolute(timeZone, { disambiguation });
return new LocalDateTime(absolute, timeZone, dt.calendar);
}

Expand All @@ -141,8 +143,8 @@ function fromIsoString(isoString, options) {
// for the hour skipped by Spring DST transitions, and that API won't return
// any values for those times.
let possibleAbsolutes = [
dt.inTimeZone(timeZone, { disambiguation: 'earlier' }),
dt.inTimeZone(timeZone, { disambiguation: 'later' })
dt.toAbsolute(timeZone, { disambiguation: 'earlier' }),
dt.toAbsolute(timeZone, { disambiguation: 'later' })
];

if (possibleAbsolutes[0].equals(possibleAbsolutes[1])) possibleAbsolutes = [possibleAbsolutes[0]];
Expand Down Expand Up @@ -177,7 +179,7 @@ function fromIsoString(isoString, options) {
absolute = possibleAbsolutes[1];
break;
case 'compatible':
absolute = timeZone.getPossibleAbsolutesFor(dt)[0] || dt.inTimeZone(timeZone, { disambiguation: 'later' });
absolute = timeZone.getPossibleAbsolutesFor(dt)[0] || dt.toAbsolute(timeZone, { disambiguation: 'later' });
break;
case 'reject':
default:
Expand Down Expand Up @@ -205,7 +207,7 @@ function doPlusOrMinus(op, durationLike, options, localDateTime) {
// Absolute doesn't use disambiguation, while RFC 5455 specifies 'compatible' behavior
// for disambiguation. Therefore, only 'dateTime' durations can use this option.
if (disambiguation !== 'compatible' && durationKind !== 'dateTime') {
throw new RangeError('Arithmetic disambiguation is only allowed for `dateTime` durations');
throw new RangeError('Disambiguation options are only valid for `dateTime` durations');
}

switch (durationKind) {
Expand All @@ -220,7 +222,7 @@ function doPlusOrMinus(op, durationLike, options, localDateTime) {
// avoid disambiguation that might change the absolute.
if (newDateTime.equals(dateTime)) return LocalDateTime.from(localDateTime);
// Otherwise, return the result.
const abs = newDateTime.inTimeZone(timeZone, { disambiguation });
const abs = newDateTime.toAbsolute(timeZone, { disambiguation });
return new LocalDateTime(abs, timeZone, calendar);
}
case 'hybrid': {
Expand All @@ -245,14 +247,14 @@ function doPlusOrMinus(op, durationLike, options, localDateTime) {
if (weeks) newDateTime = newDateTime[op]({ weeks }, dateTimeOverflowOption);
if (days) newDateTime = newDateTime[op]({ days }, dateTimeOverflowOption);
if (isZeroDuration(timeDuration)) {
const absolute = newDateTime.inTimeZone(timeZone);
const absolute = newDateTime.toAbsolute(timeZone);
return LocalDateTime.from({ absolute, timeZone, calendar: localDateTime.calendar });
} else {
// Now add/subtract the time. Because all time units are always the same
// length, we can add/subtract all of them together without worrying about
// order of operations.
newDateTime = newDateTime[op](timeDuration, dateTimeOverflowOption);
let absolute = newDateTime.inTimeZone(timeZone);
let absolute = newDateTime.toAbsolute(timeZone);
const reverseOp = op === 'plus' ? 'minus' : 'plus';
const backUpAbs = absolute[reverseOp]({ nanoseconds: totalNanoseconds(timeDuration) });
const backUpOffset = timeZone.getOffsetNanosecondsFor(backUpAbs);
Expand Down Expand Up @@ -305,7 +307,7 @@ export class LocalDateTime {
constructor(absolute, timeZone, calendar) {
this._tz = Temporal.TimeZone.from(timeZone);
this._abs = Temporal.Absolute.from(absolute);
this._dt = this._abs.inTimeZone(this._tz, calendar);
this._dt = this._abs.toDateTime(this._tz, calendar);
}

/**
Expand Down Expand Up @@ -468,9 +470,6 @@ export class LocalDateTime {
return this.with({ calendar });
}

// `inTimeZone` is replaced by the `absolute` field.
// inTimeZone(tzLike: Temporal.TimeZone | string, options?: Temporal.ToAbsoluteOptions): Temporal.Absolute;

/**
* Returns the absolute timestamp of this `Temporal.LocalDateTime` instance as
* a `Temporal.Absolute`.
Expand Down Expand Up @@ -543,11 +542,11 @@ export class LocalDateTime {
* midnight and the next day's midnight.
*/
get hoursInDay() {
const today = this.toDate().withTime(new Temporal.Time());
const today = this.toDate().toDateTime(new Temporal.Time());
const tomorrow = today.plus({ days: 1 });
// TODO: add tests for Azores timezone on midnight of a DST transition
const todayAbs = today.inTimeZone(this._tz);
const tomorrowAbs = tomorrow.inTimeZone(this._tz);
const todayAbs = today.toAbsolute(this._tz);
const tomorrowAbs = tomorrow.toAbsolute(this._tz);
const diff = tomorrowAbs.difference(todayAbs, { largestUnit: 'hours' });
const hours =
diff.hours +
Expand Down Expand Up @@ -828,7 +827,7 @@ export class LocalDateTime {
// case where both `this` and `other` are both within 25 hours of an
// offset transition, but in practice this will be exceedingly rare.
let intermediateDt = this._dt.minus(dateDuration);
let intermediateAbs = intermediateDt.inTimeZone(this._tz, { disambiguation });
let intermediateAbs = intermediateDt.toAbsolute(this._tz, { disambiguation });
let adjustedTimeDuration;
if (this._tz.getOffsetNanosecondsFor(intermediateAbs) === other.timeZoneOffsetNanoseconds) {
// The transition was in the date portion which is what we want.
Expand All @@ -838,7 +837,7 @@ export class LocalDateTime {
// time portion is on the other side next to `this`, where there's
// unlikely to be another transition.
intermediateDt = other._dt.plus(dateDuration);
intermediateAbs = intermediateDt.inTimeZone(this._tz, { disambiguation });
intermediateAbs = intermediateDt.toAbsolute(this._tz, { disambiguation });
adjustedTimeDuration = this._abs.difference(intermediateAbs, { largestUnit: 'hours' });
}

Expand Down Expand Up @@ -942,16 +941,16 @@ export class LocalDateTime {
return this._dt.isLeapYear;
}
toDate() {
return this._dt.getDate();
return this._dt.toDate();
}
toYearMonth() {
return this._dt.getYearMonth();
return this._dt.toYearMonth();
}
toMonthDay() {
return this._dt.getMonthDay();
return this._dt.toMonthDay();
}
toTime() {
return this._dt.getTime();
return this._dt.toTime();
}
valueOf() {
throw new TypeError('use compare() or equals() to compare Temporal.LocalDateTime');
Expand Down Expand Up @@ -1113,9 +1112,9 @@ function toLargestTemporalUnit(options, fallback, disallowedStrings = []) {
}

const ES = {
ToInteger: ES2019.ToInteger,
ToString: ES2019.ToString,
ToObject: ES2019.ToObject,
ToInteger: ToInteger,
ToString: ToString,
ToObject: ToObject,

// replace this with public parsing API after it lands
ParseFullISOString: (isoString) => {
Expand Down
Loading

0 comments on commit 8f767b3

Please sign in to comment.