From 52a048a0d19f8f87b249f70b977b1bc933a0a153 Mon Sep 17 00:00:00 2001 From: Jens Ochsenmeier Date: Tue, 7 Dec 2021 16:01:21 +0100 Subject: [PATCH 1/2] fix ts issues in plaindate --- lib/calendar.ts | 4 ++-- lib/ecmascript.ts | 22 +++++++++++----------- lib/plaindate.ts | 11 +++++------ lib/slots.ts | 3 ++- 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/lib/calendar.ts b/lib/calendar.ts index 185dbfc5..693d59ec 100644 --- a/lib/calendar.ts +++ b/lib/calendar.ts @@ -117,7 +117,7 @@ export class Calendar implements Temporal.Calendar { return ES.ToString(this); } dateFromFields( - fields: Params['dateFromFields'][0], + fields?: Params['dateFromFields'][0], optionsParam: Params['dateFromFields'][1] = undefined ): Return['dateFromFields'] { if (!ES.IsTemporalCalendar(this)) throw new TypeError('invalid receiver'); @@ -178,7 +178,7 @@ export class Calendar implements Temporal.Calendar { if (!ES.IsTemporalCalendar(this)) throw new TypeError('invalid receiver'); const date = ES.ToTemporalDate(dateParam); const duration = ES.ToTemporalDuration(durationParam); - const options = ES.GetOptionsObject(optionsParam); + const options = ES.GetOptionsObject(optionsParam); const overflow = ES.ToTemporalOverflow(options); const { days } = ES.BalanceDuration( GetSlot(duration, DAYS), diff --git a/lib/ecmascript.ts b/lib/ecmascript.ts index 893cefa9..a2c6cf0c 100644 --- a/lib/ecmascript.ts +++ b/lib/ecmascript.ts @@ -1069,8 +1069,8 @@ export function ToPartialRecord( } export function PrepareTemporalFields( - bag: B, - fields: ReadonlyArray> + bag?: B, + fields: ReadonlyArray> = [] ): Required | undefined { if (!IsObject(bag)) return undefined; const result = {} as B; @@ -1109,8 +1109,8 @@ export function PrepareTemporalFields( // field access in the following operations is intentionally alphabetical export function ToTemporalDateFields( - bag: Temporal.PlainDateLike, - fieldNames: readonly (keyof Temporal.PlainDateLike)[] + bag?: Temporal.PlainDateLike, + fieldNames: readonly (keyof Temporal.PlainDateLike)[] = [] ) { const entries: [keyof Temporal.PlainDateLike, 0 | undefined][] = [ ['day', undefined], @@ -1899,8 +1899,8 @@ export function CalendarFields>(calendar: Temporal.Ca export function CalendarMergeFields( calendar: Temporal.CalendarProtocol, - fields: Record, - additionalFields: Record + fields?: Record, + additionalFields?: Record ) { const calMergeFields = calendar.mergeFields; if (!calMergeFields) { @@ -2102,7 +2102,7 @@ export function ConsolidateCalendars(one: Temporal.CalendarProtocol, two: Tempor export function DateFromFields( calendar: Temporal.CalendarProtocol, - fields: CalendarProtocolParams['dateFromFields'][0], + fields?: CalendarProtocolParams['dateFromFields'][0], options?: Partial ) { const result = calendar.dateFromFields(fields, options); @@ -2112,7 +2112,7 @@ export function DateFromFields( export function YearMonthFromFields( calendar: Temporal.CalendarProtocol, - fields: CalendarProtocolParams['yearMonthFromFields'][0], + fields?: CalendarProtocolParams['yearMonthFromFields'][0], options?: CalendarProtocolParams['yearMonthFromFields'][1] ) { const result = calendar.yearMonthFromFields(fields, options); @@ -2122,7 +2122,7 @@ export function YearMonthFromFields( export function MonthDayFromFields( calendar: Temporal.CalendarProtocol, - fields: CalendarProtocolParams['monthDayFromFields'][0], + fields?: CalendarProtocolParams['monthDayFromFields'][0], options?: CalendarProtocolParams['monthDayFromFields'][1] ) { const result = calendar.monthDayFromFields(fields, options); @@ -4666,7 +4666,7 @@ export function RoundDuration( nanosecondsParam: number, increment: number, unit: Temporal.DateTimeUnit, - roundingMode: Temporal.RoundingMode, + roundingMode: Temporal.RoundingMode = 'trunc', relativeToParam: ReturnType = undefined ) { let years = yearsParam; @@ -4996,7 +4996,7 @@ export function ComparisonResult(value: number) { return value < 0 ? -1 : value > 0 ? 1 : (value as 0); } -export function GetOptionsObject(options: T) { +export function GetOptionsObject(options: T | undefined) { if (options === undefined) return ObjectCreate(null) as T; if (IsObject(options) && options !== null) return options; throw new TypeError(`Options parameter must be an object, not ${options === null ? 'null' : `${typeof options}`}`); diff --git a/lib/plaindate.ts b/lib/plaindate.ts index 48a3627a..c6e492b7 100644 --- a/lib/plaindate.ts +++ b/lib/plaindate.ts @@ -1,5 +1,5 @@ import * as ES from './ecmascript'; -import { GetIntrinsic, MakeIntrinsicClass } from './intrinsicclass'; +import { MakeIntrinsicClass } from './intrinsicclass'; import { ISO_YEAR, ISO_MONTH, @@ -188,8 +188,8 @@ export class PlainDate implements Temporal.PlainDate { this )); - const Duration = GetIntrinsic('%Temporal.Duration%'); - return new Duration(years, months, weeks, days, 0, 0, 0, 0, 0, 0); + // const Duration = GetIntrinsic('%Temporal.Duration%')!; + return new Temporal.Duration(years, months, weeks, days, 0, 0, 0, 0, 0, 0); } since(otherParam: Params['since'][0], optionsParam: Params['since'][1] = undefined): Return['since'] { if (!ES.IsTemporalDate(this)) throw new TypeError('invalid receiver'); @@ -212,9 +212,8 @@ export class PlainDate implements Temporal.PlainDate { const untilOptions = { ...options, largestUnit }; let { years, months, weeks, days } = ES.CalendarDateUntil(calendar, this, other, untilOptions); - const Duration = GetIntrinsic('%Temporal.Duration%'); if (smallestUnit === 'day' && roundingIncrement === 1) { - return new Duration(-years, -months, -weeks, -days, 0, 0, 0, 0, 0, 0); + return new Temporal.Duration(-years, -months, -weeks, -days, 0, 0, 0, 0, 0, 0); } ({ years, months, weeks, days } = ES.RoundDuration( years, @@ -233,7 +232,7 @@ export class PlainDate implements Temporal.PlainDate { this )); - return new Duration(-years, -months, -weeks, -days, 0, 0, 0, 0, 0, 0); + return new Temporal.Duration(-years, -months, -weeks, -days, 0, 0, 0, 0, 0, 0); } equals(otherParam: Params['equals'][0]): Return['equals'] { if (!ES.IsTemporalDate(this)) throw new TypeError('invalid receiver'); diff --git a/lib/slots.ts b/lib/slots.ts index 843a65bd..2e890d91 100644 --- a/lib/slots.ts +++ b/lib/slots.ts @@ -275,9 +275,10 @@ export function HasSlot(container: unknown, ...ids: (keyof Slots)[]): boolean { return !!myslots && ids.reduce((all: boolean, id) => all && id in myslots, true); } export function GetSlot( - container: Slots[typeof id]['usedBy'], + container: Slots[typeof id]['usedBy'] | undefined, id: KeyT ): Slots[KeyT]['value'] { + if (container === undefined) throw new TypeError('Missing container'); const value = GetSlots(container)[id]; if (value === undefined) throw new TypeError(`Missing internal slot ${id}`); return value; From ad911d841a13fa58481611ff89a5de7108f13561 Mon Sep 17 00:00:00 2001 From: Jens Ochsenmeier Date: Tue, 7 Dec 2021 19:53:51 +0100 Subject: [PATCH 2/2] remove getIntrinsic --- .gitignore | 1 + lib/calendar.ts | 4 +- lib/ecmascript.ts | 174 ++++++++++++++---------------------------- lib/instant.ts | 5 +- lib/intl.ts | 25 ++++-- lib/intrinsicclass.ts | 3 - lib/now.ts | 3 +- lib/plaindate.ts | 5 +- lib/plaindatetime.ts | 5 +- lib/plaintime.ts | 7 +- lib/plainyearmonth.ts | 5 +- lib/slots.ts | 2 +- lib/timezone.ts | 6 +- lib/zoneddatetime.ts | 35 +++++---- 14 files changed, 115 insertions(+), 165 deletions(-) diff --git a/.gitignore b/.gitignore index d7c7c711..9c991656 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ tsc-out/ .vscode/* !.vscode/launch.json *.tgz +.DS_STORE \ No newline at end of file diff --git a/lib/calendar.ts b/lib/calendar.ts index 693d59ec..caca9fcc 100644 --- a/lib/calendar.ts +++ b/lib/calendar.ts @@ -1,6 +1,6 @@ import { DEBUG } from './debug'; import * as ES from './ecmascript'; -import { GetIntrinsic, MakeIntrinsicClass, DefineIntrinsic } from './intrinsicclass'; +import { MakeIntrinsicClass, DefineIntrinsic } from './intrinsicclass'; import { CALENDAR_ID, ISO_YEAR, @@ -28,6 +28,7 @@ import type { CalendarReturn as Return, FieldRecord } from './internaltypes'; +import { Duration } from './duration'; const ArrayIncludes = Array.prototype.includes; const ArrayPrototypePush = Array.prototype.push; @@ -216,7 +217,6 @@ export class Calendar implements Temporal.Calendar { 'day' ); const { years, months, weeks, days } = impl[GetSlot(this, CALENDAR_ID)].dateUntil(one, two, largestUnit); - const Duration = GetIntrinsic('%Temporal.Duration%'); return new Duration(years, months, weeks, days, 0, 0, 0, 0, 0, 0); } year(dateParam: Params['year'][0]): Return['year'] { diff --git a/lib/ecmascript.ts b/lib/ecmascript.ts index a2c6cf0c..13429825 100644 --- a/lib/ecmascript.ts +++ b/lib/ecmascript.ts @@ -38,7 +38,6 @@ import type { PlainYearMonthParams, FieldRecord } from './internaltypes'; -import { GetIntrinsic } from './intrinsicclass'; import { CreateSlots, GetSlot, @@ -73,7 +72,7 @@ import { MICROSECONDS, NANOSECONDS } from './slots'; -import { IsBuiltinCalendar } from './calendar'; +import { Calendar, IsBuiltinCalendar } from './calendar'; const DAY_SECONDS = 86400; const DAY_NANOS = bigInt(DAY_SECONDS).multiply(1e9); const NS_MIN = bigInt(-DAY_SECONDS).multiply(1e17); @@ -205,6 +204,15 @@ const SINGULAR_PLURAL_UNITS: readonly PluralAndSingularUnitTuple(); @@ -1257,8 +1265,7 @@ export function ToTemporalDate( ToTemporalOverflow(options); // validate and ignore const { year, month, day, calendar, z } = ParseTemporalDateString(ToString(item)); if (z) throw new RangeError('Z designator not supported for PlainDate'); - const TemporalPlainDate = GetIntrinsic('%Temporal.PlainDate%'); - return new TemporalPlainDate(year, month, day, calendar); // include validation + return new PlainDate(year, month, day, calendar); // include validation } export function InterpretTemporalDateTimeFields( @@ -1364,30 +1371,16 @@ export function ToTemporalDuration(item: DurationParams['from'][0]) { ({ years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds } = ParseTemporalDurationString(ToString(item))); } - const TemporalDuration = GetIntrinsic('%Temporal.Duration%'); - return new TemporalDuration( - years, - months, - weeks, - days, - hours, - minutes, - seconds, - milliseconds, - microseconds, - nanoseconds - ); + return new Duration(years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds); } export function ToTemporalInstant(item: InstantParams['from'][0]) { if (IsTemporalInstant(item)) return item; if (IsTemporalZonedDateTime(item)) { - const TemporalInstant = GetIntrinsic('%Temporal.Instant%'); - return new TemporalInstant(GetSlot(item, EPOCHNANOSECONDS)); + return new Instant(GetSlot(item, EPOCHNANOSECONDS)); } const ns = ParseTemporalInstant(ToString(item)); - const TemporalInstant = GetIntrinsic('%Temporal.Instant%'); - return new TemporalInstant(ns); + return new Instant(ns); } export function ToTemporalMonthDay( @@ -1449,8 +1442,7 @@ export function ToTemporalTime( ); } if (IsTemporalDateTime(item)) { - const TemporalPlainTime = GetIntrinsic('%Temporal.PlainTime%'); - return new TemporalPlainTime( + return new PlainTime( GetSlot(item, ISO_HOUR), GetSlot(item, ISO_MINUTE), GetSlot(item, ISO_SECOND), @@ -1482,8 +1474,7 @@ export function ToTemporalTime( throw new RangeError('PlainTime can only have iso8601 calendar'); } } - const TemporalPlainTime = GetIntrinsic('%Temporal.PlainTime%'); - return new TemporalPlainTime(hour, minute, second, millisecond, microsecond, nanosecond); + return new PlainTime(hour, minute, second, millisecond, microsecond, nanosecond); } export function ToTemporalYearMonth( @@ -1533,8 +1524,7 @@ export function InterpretISODateTimeOffset( offsetOpt: Temporal.OffsetDisambiguationOptions['offset'], matchMinute: boolean ) { - const DateTime = GetIntrinsic('%Temporal.PlainDateTime%'); - const dt = new DateTime(year, month, day, hour, minute, second, millisecond, microsecond, nanosecond); + const dt = new PlainDateTime(year, month, day, hour, minute, second, millisecond, microsecond, nanosecond); if (offsetBehaviour === 'wall' || offsetOpt === 'ignore') { // Simple case: ISO string without a TZ offset (or caller wants to ignore @@ -1636,8 +1626,7 @@ export function ToTemporalZonedDateTime( } else if (!offset) { offsetBehaviour = 'wall'; } - const TemporalTimeZone = GetIntrinsic('%Temporal.TimeZone%'); - timeZone = new TemporalTimeZone(ianaName); + timeZone = new TimeZone(ianaName); if (!calendar) calendar = GetISO8601Calendar(); calendar = ToTemporalCalendar(calendar); matchMinute = true; // ISO strings may specify offset with less precision @@ -1699,8 +1688,7 @@ export function CreateTemporalDate( isoDay: number, calendar: Temporal.CalendarProtocol = GetISO8601Calendar() ) { - const TemporalPlainDate = GetIntrinsic('%Temporal.PlainDate%'); - const result = ObjectCreate(TemporalPlainDate.prototype); + const result = ObjectCreate(PlainDate.prototype); CreateTemporalDateSlots(result, isoYear, isoMonth, isoDay, calendar); return result; } @@ -1755,8 +1743,7 @@ export function CreateTemporalDateTime( ns: number, calendar: Temporal.CalendarProtocol = GetISO8601Calendar() ) { - const TemporalPlainDateTime = GetIntrinsic('%Temporal.PlainDateTime%'); - const result = ObjectCreate(TemporalPlainDateTime.prototype); + const result = ObjectCreate(PlainDateTime.prototype); CreateTemporalDateTimeSlots(result, isoYear, isoMonth, isoDay, h, min, s, ms, µs, ns, calendar); return result as Temporal.PlainDateTime; } @@ -1794,8 +1781,7 @@ export function CreateTemporalMonthDay( calendar: Temporal.CalendarProtocol = GetISO8601Calendar(), referenceISOYear = 1972 ) { - const TemporalPlainMonthDay = GetIntrinsic('%Temporal.PlainMonthDay%'); - const result = ObjectCreate(TemporalPlainMonthDay.prototype); + const result = ObjectCreate(PlainMonthDay.prototype); CreateTemporalMonthDaySlots(result, isoMonth, isoDay, calendar, referenceISOYear); return result; } @@ -1833,8 +1819,7 @@ export function CreateTemporalYearMonth( calendar: Temporal.CalendarProtocol = GetISO8601Calendar(), referenceISODay = 1 ) { - const TemporalPlainYearMonth = GetIntrinsic('%Temporal.PlainYearMonth%'); - const result = ObjectCreate(TemporalPlainYearMonth.prototype); + const result = ObjectCreate(PlainYearMonth.prototype); CreateTemporalYearMonthSlots(result, isoYear, isoMonth, calendar, referenceISODay); return result; } @@ -1852,8 +1837,7 @@ export function CreateTemporalZonedDateTimeSlots( SetSlot(result, TIME_ZONE, timeZone); SetSlot(result, CALENDAR, calendar); - const TemporalInstant = GetIntrinsic('%Temporal.Instant%'); - const instant = new TemporalInstant(GetSlot(result, EPOCHNANOSECONDS)); + const instant = new Instant(GetSlot(result, EPOCHNANOSECONDS)); SetSlot(result, INSTANT, instant); if (DEBUG) { @@ -1871,15 +1855,13 @@ export function CreateTemporalZonedDateTime( timeZone: Temporal.TimeZoneProtocol, calendar: Temporal.CalendarProtocol = GetISO8601Calendar() ) { - const TemporalZonedDateTime = GetIntrinsic('%Temporal.ZonedDateTime%'); - const result = ObjectCreate(TemporalZonedDateTime.prototype); + const result = ObjectCreate(ZonedDateTime.prototype); CreateTemporalZonedDateTimeSlots(result, epochNanoseconds, timeZone, calendar); return result; } export function GetISO8601Calendar() { - const TemporalCalendar = GetIntrinsic('%Temporal.Calendar%'); - return new TemporalCalendar('iso8601'); + return new Calendar('iso8601'); } // TODO: should (can?) we make this generic so the field names are checked @@ -2059,8 +2041,7 @@ export function ToTemporalCalendar(calendarLikeParam: CalendarParams['from'][0]) if (IsObject(calendarLike) && !('calendar' in calendarLike)) return calendarLike; } const identifier = ToString(calendarLike); - const TemporalCalendar = GetIntrinsic('%Temporal.Calendar%'); - if (IsBuiltinCalendar(identifier)) return new TemporalCalendar(identifier); + if (IsBuiltinCalendar(identifier)) return new Calendar(identifier); let calendar; try { ({ calendar } = ParseISODateTime(identifier, { zoneRequired: false })); @@ -2068,7 +2049,7 @@ export function ToTemporalCalendar(calendarLikeParam: CalendarParams['from'][0]) throw new RangeError(`Invalid calendar: ${identifier}`); } if (!calendar) calendar = 'iso8601'; - return new TemporalCalendar(calendar); + return new Calendar(calendar); } function GetTemporalCalendarWithISODefault( @@ -2142,8 +2123,7 @@ export function ToTemporalTimeZone(temporalTimeZoneLikeParam: TimeZoneParams['fr } const identifier = ToString(temporalTimeZoneLike); const timeZone = ParseTemporalTimeZone(identifier); - const TemporalTimeZone = GetIntrinsic('%Temporal.TimeZone%'); - return new TemporalTimeZone(timeZone); + return new TimeZone(timeZone); } export function TimeZoneEquals(one: Temporal.TimeZoneProtocol, two: Temporal.TimeZoneProtocol) { @@ -2163,8 +2143,7 @@ export function TemporalDateTimeToDate(dateTime: Temporal.PlainDateTime) { } export function TemporalDateTimeToTime(dateTime: Temporal.PlainDateTime) { - const Time = GetIntrinsic('%Temporal.PlainTime%'); - return new Time( + return new PlainTime( GetSlot(dateTime, ISO_HOUR), GetSlot(dateTime, ISO_MINUTE), GetSlot(dateTime, ISO_SECOND), @@ -2178,10 +2157,7 @@ export function GetOffsetNanosecondsFor( timeZone: Temporal.TimeZoneProtocol, instant: TimeZoneProtocolParams['getOffsetNanosecondsFor'][0] ) { - let getOffsetNanosecondsFor = timeZone.getOffsetNanosecondsFor; - if (getOffsetNanosecondsFor === undefined) { - getOffsetNanosecondsFor = GetIntrinsic('%Temporal.TimeZone.prototype.getOffsetNanosecondsFor%'); - } + const getOffsetNanosecondsFor = timeZone.getOffsetNanosecondsFor; const offsetNs = Reflect.apply(getOffsetNanosecondsFor, timeZone, [instant]); if (typeof offsetNs !== 'number') { throw new TypeError('bad return from getOffsetNanosecondsFor'); @@ -2234,7 +2210,6 @@ function DisambiguatePossibleInstants( dateTime: Temporal.PlainDateTime, disambiguation: Temporal.ToInstantOptions['disambiguation'] ) { - const Instant = GetIntrinsic('%Temporal.Instant%'); const numInstants = possibleInstants.length; if (numInstants === 1) return possibleInstants[0]; @@ -2271,7 +2246,6 @@ function DisambiguatePossibleInstants( switch (disambiguation) { case 'earlier': { const calendar = GetSlot(dateTime, CALENDAR); - const PlainDateTime = GetIntrinsic('%Temporal.PlainDateTime%'); const earlier = AddDateTime( year, month, @@ -2313,7 +2287,6 @@ function DisambiguatePossibleInstants( // fall through because 'compatible' means 'later' for "spring forward" transitions case 'later': { const calendar = GetSlot(dateTime, CALENDAR); - const PlainDateTime = GetIntrinsic('%Temporal.PlainDateTime%'); const later = AddDateTime( year, month, @@ -2419,8 +2392,7 @@ export function TemporalInstantToString( ) { let outputTimeZone = timeZone; if (outputTimeZone === undefined) { - const TemporalTimeZone = GetIntrinsic('%Temporal.TimeZone%'); - outputTimeZone = new TemporalTimeZone('UTC'); + outputTimeZone = new TimeZone('UTC'); } const iso = GetISO8601Calendar(); const dateTime = BuiltinTimeZoneGetPlainDateTimeFor(outputTimeZone, instant, iso); @@ -2634,8 +2606,7 @@ export function TemporalZonedDateTimeToString( if (options) { const { unit, increment, roundingMode } = options; const ns = RoundInstant(GetSlot(zdt, EPOCHNANOSECONDS), increment, unit, roundingMode); - const TemporalInstant = GetIntrinsic('%Temporal.Instant%'); - instant = new TemporalInstant(ns); + instant = new Instant(ns); } const tz = GetSlot(zdt, TIME_ZONE); @@ -3109,7 +3080,6 @@ function NanosecondsToDays( nanosecondsParam: bigInt.BigInteger, relativeTo: ReturnType ) { - const TemporalInstant = GetIntrinsic('%Temporal.Instant%'); const sign = MathSign(nanosecondsParam.toJSNumber()); let nanoseconds = bigInt(nanosecondsParam); let dayLengthNs = 86400e9; @@ -3124,7 +3094,7 @@ function NanosecondsToDays( const startNs = GetSlot(relativeTo, EPOCHNANOSECONDS); const start = GetSlot(relativeTo, INSTANT); const endNs = startNs.add(nanoseconds); - const end = new TemporalInstant(endNs); + const end = new Instant(endNs); const timeZone = GetSlot(relativeTo, TIME_ZONE); const calendar = GetSlot(relativeTo, CALENDAR); @@ -3174,7 +3144,7 @@ function NanosecondsToDays( nanoseconds = endNs.subtract(intermediateNs); let isOverflow = false; - let relativeInstant = new TemporalInstant(intermediateNs); + let relativeInstant = new Instant(intermediateNs); do { // calculate length of the next day (day that contains the time remainder) const oneDayFartherNs = AddZonedDateTime(relativeInstant, timeZone, calendar, 0, 0, 0, sign, 0, 0, 0, 0, 0, 0); @@ -3183,7 +3153,7 @@ function NanosecondsToDays( isOverflow = nanoseconds.subtract(dayLengthNs).multiply(sign).geq(0); if (isOverflow) { nanoseconds = nanoseconds.subtract(dayLengthNs); - relativeInstant = new TemporalInstant(oneDayFartherNs); + relativeInstant = new Instant(oneDayFartherNs); days += sign; } } while (isOverflow); @@ -3306,7 +3276,6 @@ export function UnbalanceDurationRelative( let months = monthsParam; let weeks = weeksParam; let days = daysParam; - const TemporalDuration = GetIntrinsic('%Temporal.Duration%'); const sign = DurationSign(years, months, weeks, days, 0, 0, 0, 0, 0, 0); let calendar; @@ -3316,9 +3285,9 @@ export function UnbalanceDurationRelative( calendar = GetSlot(relativeTo, CALENDAR); } - const oneYear = new TemporalDuration(sign); - const oneMonth = new TemporalDuration(0, sign); - const oneWeek = new TemporalDuration(0, 0, sign); + const oneYear = new Duration(sign); + const oneMonth = new Duration(0, sign); + const oneWeek = new Duration(0, 0, sign); switch (largestUnit) { case 'year': @@ -3407,7 +3376,6 @@ export function BalanceDurationRelative( let months = monthsParam; let weeks = weeksParam; let days = daysParam; - const TemporalDuration = GetIntrinsic('%Temporal.Duration%'); const sign = DurationSign(years, months, weeks, days, 0, 0, 0, 0, 0, 0); if (sign === 0) return { years, months, weeks, days }; @@ -3418,9 +3386,9 @@ export function BalanceDurationRelative( calendar = GetSlot(relativeTo, CALENDAR); } - const oneYear = new TemporalDuration(sign); - const oneMonth = new TemporalDuration(0, sign); - const oneWeek = new TemporalDuration(0, 0, sign); + const oneYear = new Duration(sign); + const oneMonth = new Duration(0, sign); + const oneWeek = new Duration(0, 0, sign); switch (largestUnit) { case 'year': { @@ -3520,8 +3488,7 @@ export function CalculateOffsetShift( const calendar = GetSlot(relativeTo, CALENDAR); const offsetBefore = GetOffsetNanosecondsFor(timeZone, instant); const after = AddZonedDateTime(instant, timeZone, calendar, y, mon, w, d, h, min, s, ms, µs, ns); - const TemporalInstant = GetIntrinsic('%Temporal.Instant%'); - const instantAfter = new TemporalInstant(after); + const instantAfter = new Instant(after); const offsetAfter = GetOffsetNanosecondsFor(timeZone, instantAfter); return offsetAfter - offsetBefore; } @@ -3529,8 +3496,7 @@ export function CalculateOffsetShift( } export function CreateNegatedTemporalDuration(duration: Temporal.Duration) { - const TemporalDuration = GetIntrinsic('%Temporal.Duration%'); - return new TemporalDuration( + return new Duration( -GetSlot(duration, YEARS), -GetSlot(duration, MONTHS), -GetSlot(duration, WEEKS), @@ -3956,9 +3922,8 @@ export function DifferenceZonedDateTime( } // Find the difference in dates only. - const TemporalInstant = GetIntrinsic('%Temporal.Instant%'); - const start = new TemporalInstant(ns1); - const end = new TemporalInstant(ns2); + const start = new Instant(ns1); + const end = new Instant(ns2); const dtStart = BuiltinTimeZoneGetPlainDateTimeFor(timeZone, start, calendar); const dtEnd = BuiltinTimeZoneGetPlainDateTimeFor(timeZone, end, calendar); let { years, months, weeks, days } = DifferenceISODateTime( @@ -4115,11 +4080,10 @@ export function AddDuration( largestUnit )); } else if (IsTemporalDate(relativeTo)) { - const TemporalDuration = GetIntrinsic('%Temporal.Duration%'); const calendar = GetSlot(relativeTo, CALENDAR); - const dateDuration1 = new TemporalDuration(y1, mon1, w1, d1, 0, 0, 0, 0, 0, 0); - const dateDuration2 = new TemporalDuration(y2, mon2, w2, d2, 0, 0, 0, 0, 0, 0); + const dateDuration1 = new Duration(y1, mon1, w1, d1, 0, 0, 0, 0, 0, 0); + const dateDuration2 = new Duration(y2, mon2, w2, d2, 0, 0, 0, 0, 0, 0); const dateAdd = calendar.dateAdd; const firstAddOptions = ObjectCreate(null); const intermediate = CalendarDateAdd(calendar, relativeTo, dateDuration1, firstAddOptions, dateAdd); @@ -4143,7 +4107,6 @@ export function AddDuration( )); } else { // relativeTo is a ZonedDateTime - const TemporalInstant = GetIntrinsic('%Temporal.Instant%'); const timeZone = GetSlot(relativeTo, TIME_ZONE); const calendar = GetSlot(relativeTo, CALENDAR); const intermediateNs = AddZonedDateTime( @@ -4162,7 +4125,7 @@ export function AddDuration( ns1 ); const endNs = AddZonedDateTime( - new TemporalInstant(intermediateNs), + new Instant(intermediateNs), timeZone, calendar, y2, @@ -4273,9 +4236,8 @@ export function AddDateTime( days += deltaDays; // Delegate the date part addition to the calendar - const TemporalDuration = GetIntrinsic('%Temporal.Duration%'); const datePart = CreateTemporalDate(year, month, day, calendar); - const dateDuration = new TemporalDuration(years, months, weeks, days, 0, 0, 0, 0, 0, 0); + const dateDuration = new Duration(years, months, weeks, days, 0, 0, 0, 0, 0, 0); const addedDate = CalendarDateAdd(calendar, datePart, dateDuration, options); return { @@ -4315,7 +4277,6 @@ export function AddZonedDateTime( // not expected and so is avoided below via a fast path for time-only // arithmetic. // BTW, this behavior is similar in spirit to offset: 'prefer' in `with`. - const TemporalDuration = GetIntrinsic('%Temporal.Duration%'); if (DurationSign(years, months, weeks, days, 0, 0, 0, 0, 0, 0) === 0) { return AddInstant(GetSlot(instant, EPOCHNANOSECONDS), h, min, s, ms, µs, ns); } @@ -4324,7 +4285,7 @@ export function AddZonedDateTime( // time portion to be added in exact time. const dt = BuiltinTimeZoneGetPlainDateTimeFor(timeZone, instant, calendar); const datePart = CreateTemporalDate(GetSlot(dt, ISO_YEAR), GetSlot(dt, ISO_MONTH), GetSlot(dt, ISO_DAY), calendar); - const dateDuration = new TemporalDuration(years, months, weeks, days, 0, 0, 0, 0, 0, 0); + const dateDuration = new Duration(years, months, weeks, days, 0, 0, 0, 0, 0, 0); const addedDate = CalendarDateAdd(calendar, datePart, dateDuration, options); const dtIntermediate = CreateTemporalDateTime( GetSlot(addedDate, ISO_YEAR), @@ -4596,22 +4557,7 @@ export function AdjustRoundedDurationDays( 0, 0 ); - const TemporalInstant = GetIntrinsic('%Temporal.Instant%'); - const dayEnd = AddZonedDateTime( - new TemporalInstant(dayStart), - timeZone, - calendar, - 0, - 0, - 0, - direction, - 0, - 0, - 0, - 0, - 0, - 0 - ); + const dayEnd = AddZonedDateTime(new Instant(dayStart), timeZone, calendar, 0, 0, 0, direction, 0, 0, 0, 0, 0, 0); const dayLengthNs = dayEnd.subtract(dayStart); if (timeRemainderNs.subtract(dayLengthNs).multiply(direction).geq(0)) { @@ -4679,7 +4625,6 @@ export function RoundDuration( let milliseconds = millisecondsParam; let microseconds = microsecondsParam; let nanoseconds = bigInt(nanosecondsParam); - const TemporalDuration = GetIntrinsic('%Temporal.Duration%'); let calendar, zdtRelative; // if it's a ZDT, it will be reassigned to a PDT below. let relativeTo: Parameters[1] = relativeToParam; @@ -4720,11 +4665,11 @@ export function RoundDuration( // convert months and weeks to days by calculating difference( // relativeTo + years, relativeTo + { years, months, weeks }) - const yearsDuration = new TemporalDuration(years); + const yearsDuration = new Duration(years); const dateAdd = calendar.dateAdd; const firstAddOptions = ObjectCreate(null); const yearsLater = CalendarDateAdd(calendar, relativeTo, yearsDuration, firstAddOptions, dateAdd); - const yearsMonthsWeeks = new TemporalDuration(years, months, weeks); + const yearsMonthsWeeks = new Duration(years, months, weeks); const secondAddOptions = ObjectCreate(null); const yearsMonthsWeeksLater = CalendarDateAdd(calendar, relativeTo, yearsMonthsWeeks, secondAddOptions, dateAdd); const monthsWeeksInDays = DaysUntil(yearsLater, yearsMonthsWeeksLater); @@ -4742,7 +4687,7 @@ export function RoundDuration( relativeTo = CalendarDateAdd(calendar, relativeTo, { years: yearsPassed }, fourthAddOptions, dateAdd); const daysPassed = DaysUntil(oldRelativeTo, relativeTo); days -= daysPassed; - const oneYear = new TemporalDuration(days < 0 ? -1 : 1); + const oneYear = new Duration(days < 0 ? -1 : 1); let { days: oneYearDays } = MoveRelativeDate(calendar, relativeTo, oneYear); // Note that `nanoseconds` below (here and in similar code for months, @@ -4766,11 +4711,11 @@ export function RoundDuration( // convert weeks to days by calculating difference(relativeTo + // { years, months }, relativeTo + { years, months, weeks }) - const yearsMonths = new TemporalDuration(years, months); + const yearsMonths = new Duration(years, months); const dateAdd = calendar.dateAdd; const firstAddOptions = ObjectCreate(null); const yearsMonthsLater = CalendarDateAdd(calendar, relativeTo, yearsMonths, firstAddOptions, dateAdd); - const yearsMonthsWeeks = new TemporalDuration(years, months, weeks); + const yearsMonthsWeeks = new Duration(years, months, weeks); const secondAddOptions = ObjectCreate(null); const yearsMonthsWeeksLater = CalendarDateAdd(calendar, relativeTo, yearsMonthsWeeks, secondAddOptions, dateAdd); const weeksInDays = DaysUntil(yearsMonthsLater, yearsMonthsWeeksLater); @@ -4780,7 +4725,7 @@ export function RoundDuration( // Months may be different lengths of days depending on the calendar, // convert days to months in a loop as described above under 'years'. const sign = MathSign(days); - const oneMonth = new TemporalDuration(0, days < 0 ? -1 : 1); + const oneMonth = new Duration(0, days < 0 ? -1 : 1); let oneMonthDays; ({ relativeTo, days: oneMonthDays } = MoveRelativeDate(calendar, relativeTo, oneMonth)); while (MathAbs(days) >= MathAbs(oneMonthDays)) { @@ -4803,7 +4748,7 @@ export function RoundDuration( // Weeks may be different lengths of days depending on the calendar, // convert days to weeks in a loop as described above under 'years'. const sign = MathSign(days); - const oneWeek = new TemporalDuration(0, 0, days < 0 ? -1 : 1); + const oneWeek = new Duration(0, 0, days < 0 ? -1 : 1); let oneWeekDays; ({ relativeTo, days: oneWeekDays } = MoveRelativeDate(calendar, relativeTo, oneWeek)); while (MathAbs(days) >= MathAbs(oneWeekDays)) { @@ -4988,8 +4933,7 @@ export const SystemUTCEpochNanoSeconds: () => bigInt.BigInteger = (() => { export function SystemTimeZone() { const fmt = new IntlDateTimeFormat('en-us'); - const TemporalTimeZone = GetIntrinsic('%Temporal.TimeZone%'); - return new TemporalTimeZone(ParseTemporalTimeZone(fmt.resolvedOptions().timeZone)); + return new TimeZone(ParseTemporalTimeZone(fmt.resolvedOptions().timeZone)); } export function ComparisonResult(value: number) { diff --git a/lib/instant.ts b/lib/instant.ts index b65f5672..ef86d9d0 100644 --- a/lib/instant.ts +++ b/lib/instant.ts @@ -1,12 +1,13 @@ import { DEBUG } from './debug'; import * as ES from './ecmascript'; -import { GetIntrinsic, MakeIntrinsicClass } from './intrinsicclass'; +import { MakeIntrinsicClass } from './intrinsicclass'; import { EPOCHNANOSECONDS, CreateSlots, GetSlot, SetSlot } from './slots'; import { Temporal } from '..'; import { DateTimeFormat } from './intl'; import type { InstantParams as Params, InstantReturn as Return } from './internaltypes'; import bigInt from 'big-integer'; +import { Duration } from './duration'; const DISALLOWED_UNITS = ['year', 'month', 'week', 'day'] as const; const MAX_DIFFERENCE_INCREMENTS = { @@ -126,7 +127,6 @@ export class Instant implements Temporal.Instant { nanoseconds, largestUnit )); - const Duration = GetIntrinsic('%Temporal.Duration%'); return new Duration(0, 0, 0, 0, hours, minutes, seconds, milliseconds, microseconds, nanoseconds); } since(otherParam: Params['since'][0], optionsParam: Params['since'][1] = undefined) { @@ -159,7 +159,6 @@ export class Instant implements Temporal.Instant { nanoseconds, largestUnit )); - const Duration = GetIntrinsic('%Temporal.Duration%'); return new Duration(0, 0, 0, 0, hours, minutes, seconds, milliseconds, microseconds, nanoseconds); } round(optionsParam: Params['round'][0]): Return['round'] { diff --git a/lib/intl.ts b/lib/intl.ts index 49a02de4..5777f1cd 100644 --- a/lib/intl.ts +++ b/lib/intl.ts @@ -1,5 +1,4 @@ import * as ES from './ecmascript'; -import { GetIntrinsic } from './intrinsicclass'; import { GetSlot, INSTANT, @@ -26,6 +25,7 @@ import { PlainTimeParams, PlainYearMonthParams } from './internaltypes'; +import { PlainDateTime } from './plaindatetime'; const DATE = Symbol('date'); const YM = Symbol('ym'); @@ -505,8 +505,6 @@ type TypesWithToLocaleString = | Temporal.ZonedDateTime; function extractOverrides(temporalObj: Params['format'][0], main: DateTimeFormatImpl) { - const DateTime = GetIntrinsic('%Temporal.PlainDateTime%'); - if (ES.IsTemporalTime(temporalObj)) { const hour = GetSlot(temporalObj, ISO_HOUR); const minute = GetSlot(temporalObj, ISO_MINUTE); @@ -514,7 +512,18 @@ function extractOverrides(temporalObj: Params['format'][0], main: DateTimeFormat const millisecond = GetSlot(temporalObj, ISO_MILLISECOND); const microsecond = GetSlot(temporalObj, ISO_MICROSECOND); const nanosecond = GetSlot(temporalObj, ISO_NANOSECOND); - const datetime = new DateTime(1970, 1, 1, hour, minute, second, millisecond, microsecond, nanosecond, main[CAL_ID]); + const datetime = new PlainDateTime( + 1970, + 1, + 1, + hour, + minute, + second, + millisecond, + microsecond, + nanosecond, + main[CAL_ID] + ); return { instant: ES.BuiltinTimeZoneGetInstantFor(getResolvedTimeZoneLazy(main), datetime, 'compatible'), formatter: getPropLazy(main, TIME) @@ -531,7 +540,7 @@ function extractOverrides(temporalObj: Params['format'][0], main: DateTimeFormat `cannot format PlainYearMonth with calendar ${calendar} in locale with calendar ${main[CAL_ID]}` ); } - const datetime = new DateTime(isoYear, isoMonth, referenceISODay, 12, 0, 0, 0, 0, 0, calendar); + const datetime = new PlainDateTime(isoYear, isoMonth, referenceISODay, 12, 0, 0, 0, 0, 0, calendar); return { instant: ES.BuiltinTimeZoneGetInstantFor(getResolvedTimeZoneLazy(main), datetime, 'compatible'), formatter: getPropLazy(main, YM) @@ -548,7 +557,7 @@ function extractOverrides(temporalObj: Params['format'][0], main: DateTimeFormat `cannot format PlainMonthDay with calendar ${calendar} in locale with calendar ${main[CAL_ID]}` ); } - const datetime = new DateTime(referenceISOYear, isoMonth, isoDay, 12, 0, 0, 0, 0, 0, calendar); + const datetime = new PlainDateTime(referenceISOYear, isoMonth, isoDay, 12, 0, 0, 0, 0, 0, calendar); return { instant: ES.BuiltinTimeZoneGetInstantFor(getResolvedTimeZoneLazy(main), datetime, 'compatible'), formatter: getPropLazy(main, MD) @@ -563,7 +572,7 @@ function extractOverrides(temporalObj: Params['format'][0], main: DateTimeFormat if (calendar !== 'iso8601' && calendar !== main[CAL_ID]) { throw new RangeError(`cannot format PlainDate with calendar ${calendar} in locale with calendar ${main[CAL_ID]}`); } - const datetime = new DateTime(isoYear, isoMonth, isoDay, 12, 0, 0, 0, 0, 0, main[CAL_ID]); + const datetime = new PlainDateTime(isoYear, isoMonth, isoDay, 12, 0, 0, 0, 0, 0, main[CAL_ID]); return { instant: ES.BuiltinTimeZoneGetInstantFor(getResolvedTimeZoneLazy(main), datetime, 'compatible'), formatter: getPropLazy(main, DATE) @@ -588,7 +597,7 @@ function extractOverrides(temporalObj: Params['format'][0], main: DateTimeFormat } let datetime = temporalObj; if (calendar === 'iso8601') { - datetime = new DateTime( + datetime = new PlainDateTime( isoYear, isoMonth, isoDay, diff --git a/lib/intrinsicclass.ts b/lib/intrinsicclass.ts index 8de7092c..2ff35f7c 100644 --- a/lib/intrinsicclass.ts +++ b/lib/intrinsicclass.ts @@ -135,6 +135,3 @@ export function DefineIntrinsic(name: KeyT if (INTRINSICS[key] !== undefined) throw new Error(`intrinsic ${name} already exists`); INTRINSICS[key] = value; } -export function GetIntrinsic(intrinsic: KeyT): typeof INTRINSICS[KeyT] { - return INTRINSICS[intrinsic]; -} diff --git a/lib/now.ts b/lib/now.ts index befb9bed..0de18fe6 100644 --- a/lib/now.ts +++ b/lib/now.ts @@ -1,9 +1,8 @@ import * as ES from './ecmascript'; -import { GetIntrinsic } from './intrinsicclass'; import { Temporal } from '..'; +import { Instant } from './instant'; const instant: typeof Temporal.Now['instant'] = () => { - const Instant = GetIntrinsic('%Temporal.Instant%'); return new Instant(ES.SystemUTCEpochNanoSeconds()); }; const plainDateTime: typeof Temporal.Now['plainDateTime'] = (calendarLike, temporalTimeZoneLike = timeZone()) => { diff --git a/lib/plaindate.ts b/lib/plaindate.ts index c6e492b7..0f9e21cb 100644 --- a/lib/plaindate.ts +++ b/lib/plaindate.ts @@ -17,6 +17,7 @@ import { import { Temporal } from '..'; import { DateTimeFormat } from './intl'; import type { PlainDateParams as Params, PlainDateReturn as Return } from './internaltypes'; +import { Duration } from './duration'; const DISALLOWED_UNITS = ['hour', 'minute', 'second', 'millisecond', 'microsecond', 'nanosecond'] as const; @@ -188,8 +189,7 @@ export class PlainDate implements Temporal.PlainDate { this )); - // const Duration = GetIntrinsic('%Temporal.Duration%')!; - return new Temporal.Duration(years, months, weeks, days, 0, 0, 0, 0, 0, 0); + return new Duration(years, months, weeks, days, 0, 0, 0, 0, 0, 0); } since(otherParam: Params['since'][0], optionsParam: Params['since'][1] = undefined): Return['since'] { if (!ES.IsTemporalDate(this)) throw new TypeError('invalid receiver'); @@ -350,6 +350,7 @@ export class PlainDate implements Temporal.PlainDate { calendar ); const instant = ES.BuiltinTimeZoneGetInstantFor(timeZone, dt, 'compatible'); + if (!instant) throw new TypeError(`Could not create instant for timezone "${timeZone}" and dateTime "${dt}"`); return ES.CreateTemporalZonedDateTime(GetSlot(instant, EPOCHNANOSECONDS), timeZone, calendar); } toPlainYearMonth(): Return['toPlainYearMonth'] { diff --git a/lib/plaindatetime.ts b/lib/plaindatetime.ts index 6eebf490..2d905ee4 100644 --- a/lib/plaindatetime.ts +++ b/lib/plaindatetime.ts @@ -1,5 +1,5 @@ import * as ES from './ecmascript'; -import { GetIntrinsic, MakeIntrinsicClass } from './intrinsicclass'; +import { MakeIntrinsicClass } from './intrinsicclass'; import { ISO_YEAR, @@ -18,6 +18,7 @@ import { import { Temporal } from '..'; import { DateTimeFormat } from './intl'; import type { PlainDateTimeParams as Params, PlainDateTimeReturn as Return } from './internaltypes'; +import { Duration } from './duration'; export class PlainDateTime implements Temporal.PlainDateTime { constructor( @@ -428,7 +429,6 @@ export class PlainDateTime implements Temporal.PlainDateTime { largestUnit )); - const Duration = GetIntrinsic('%Temporal.Duration%'); return new Duration(years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds); } since(otherParam: Params['since'][0], optionsParam: Params['since'][1] = undefined): Return['since'] { @@ -503,7 +503,6 @@ export class PlainDateTime implements Temporal.PlainDateTime { largestUnit )); - const Duration = GetIntrinsic('%Temporal.Duration%'); return new Duration( -years, -months, diff --git a/lib/plaintime.ts b/lib/plaintime.ts index f9c041b2..eaa0bcc4 100644 --- a/lib/plaintime.ts +++ b/lib/plaintime.ts @@ -1,6 +1,6 @@ import { DEBUG } from './debug'; import * as ES from './ecmascript'; -import { GetIntrinsic, MakeIntrinsicClass } from './intrinsicclass'; +import { MakeIntrinsicClass } from './intrinsicclass'; import { ISO_YEAR, @@ -21,6 +21,8 @@ import { import { Temporal } from '..'; import { DateTimeFormat } from './intl'; import type { PlainTimeParams as Params, PlainTimeReturn as Return } from './internaltypes'; +import { Duration } from './duration'; +import { PlainDateTime } from './plaindatetime'; const ObjectAssign = Object.assign; @@ -291,7 +293,6 @@ export class PlainTime implements Temporal.PlainTime { nanoseconds, largestUnit )); - const Duration = GetIntrinsic('%Temporal.Duration%'); return new Duration(0, 0, 0, 0, hours, minutes, seconds, milliseconds, microseconds, nanoseconds); } since(otherParam: Params['since'][0], optionsParam: Params['since'][1] = undefined): Return['since'] { @@ -348,7 +349,6 @@ export class PlainTime implements Temporal.PlainTime { nanoseconds, largestUnit )); - const Duration = GetIntrinsic('%Temporal.Duration%'); return new Duration(0, 0, 0, 0, hours, minutes, seconds, milliseconds, microseconds, nanoseconds); } round(optionsParam: Params['round'][0]): Return['round'] { @@ -475,7 +475,6 @@ export class PlainTime implements Temporal.PlainTime { const microsecond = GetSlot(this, ISO_MICROSECOND); const nanosecond = GetSlot(this, ISO_NANOSECOND); - const PlainDateTime = GetIntrinsic('%Temporal.PlainDateTime%'); const dt = new PlainDateTime( year, month, diff --git a/lib/plainyearmonth.ts b/lib/plainyearmonth.ts index 537c3046..6f64610b 100644 --- a/lib/plainyearmonth.ts +++ b/lib/plainyearmonth.ts @@ -1,9 +1,10 @@ import * as ES from './ecmascript'; -import { GetIntrinsic, MakeIntrinsicClass } from './intrinsicclass'; +import { MakeIntrinsicClass } from './intrinsicclass'; import { ISO_YEAR, ISO_MONTH, ISO_DAY, CALENDAR, GetSlot } from './slots'; import { Temporal } from '..'; import { DateTimeFormat } from './intl'; import type { FieldRecord, PlainYearMonthParams as Params, PlainYearMonthReturn as Return } from './internaltypes'; +import { Duration } from './duration'; const ObjectCreate = Object.create; @@ -203,7 +204,6 @@ export class PlainYearMonth implements Temporal.PlainYearMonth { thisDate )); - const Duration = GetIntrinsic('%Temporal.Duration%'); return new Duration(years, months, 0, 0, 0, 0, 0, 0, 0, 0); } since(otherParam: Params['since'][0], optionsParam: Params['since'][1] = undefined): Return['since'] { @@ -233,7 +233,6 @@ export class PlainYearMonth implements Temporal.PlainYearMonth { const untilOptions = { ...options, largestUnit }; let { years, months } = ES.CalendarDateUntil(calendar, thisDate, otherDate, untilOptions); - const Duration = GetIntrinsic('%Temporal.Duration%'); if (smallestUnit === 'month' && roundingIncrement === 1) { return new Duration(-years, -months, 0, 0, 0, 0, 0, 0, 0, 0); } diff --git a/lib/slots.ts b/lib/slots.ts index 2e890d91..3a1f5ab3 100644 --- a/lib/slots.ts +++ b/lib/slots.ts @@ -275,7 +275,7 @@ export function HasSlot(container: unknown, ...ids: (keyof Slots)[]): boolean { return !!myslots && ids.reduce((all: boolean, id) => all && id in myslots, true); } export function GetSlot( - container: Slots[typeof id]['usedBy'] | undefined, + container: Slots[typeof id]['usedBy'], id: KeyT ): Slots[KeyT]['value'] { if (container === undefined) throw new TypeError('Missing container'); diff --git a/lib/timezone.ts b/lib/timezone.ts index 36df85b1..e36f37d4 100644 --- a/lib/timezone.ts +++ b/lib/timezone.ts @@ -1,6 +1,6 @@ import { DEBUG } from './debug'; import * as ES from './ecmascript'; -import { GetIntrinsic, MakeIntrinsicClass, DefineIntrinsic } from './intrinsicclass'; +import { MakeIntrinsicClass, DefineIntrinsic } from './intrinsicclass'; import { TIMEZONE_ID, EPOCHNANOSECONDS, @@ -19,6 +19,7 @@ import { } from './slots'; import { Temporal } from '..'; import type { TimeZoneParams as Params, TimeZoneReturn as Return } from './internaltypes'; +import { Instant } from './instant'; export class TimeZone implements Temporal.TimeZone { constructor(timeZoneIdentifierParam: string) { @@ -80,7 +81,6 @@ export class TimeZone implements Temporal.TimeZone { getPossibleInstantsFor(dateTimeParam: Params['getPossibleInstantsFor'][0]): Return['getPossibleInstantsFor'] { if (!ES.IsTemporalTimeZone(this)) throw new TypeError('invalid receiver'); const dateTime = ES.ToTemporalDateTime(dateTimeParam); - const Instant = GetIntrinsic('%Temporal.Instant%'); const id = GetSlot(this, TIMEZONE_ID); const offsetNs = ES.ParseOffsetString(id); @@ -125,7 +125,6 @@ export class TimeZone implements Temporal.TimeZone { } let epochNanoseconds = GetSlot(startingPoint, EPOCHNANOSECONDS); - const Instant = GetIntrinsic('%Temporal.Instant%'); epochNanoseconds = ES.GetIANATimeZoneNextTransition(epochNanoseconds, id); return epochNanoseconds === null ? null : new Instant(epochNanoseconds); } @@ -140,7 +139,6 @@ export class TimeZone implements Temporal.TimeZone { } let epochNanoseconds = GetSlot(startingPoint, EPOCHNANOSECONDS); - const Instant = GetIntrinsic('%Temporal.Instant%'); epochNanoseconds = ES.GetIANATimeZonePreviousTransition(epochNanoseconds, id); return epochNanoseconds === null ? null : new Instant(epochNanoseconds); } diff --git a/lib/zoneddatetime.ts b/lib/zoneddatetime.ts index a275de72..00b3ed94 100644 --- a/lib/zoneddatetime.ts +++ b/lib/zoneddatetime.ts @@ -1,5 +1,5 @@ import * as ES from './ecmascript'; -import { GetIntrinsic, MakeIntrinsicClass } from './intrinsicclass'; +import { MakeIntrinsicClass } from './intrinsicclass'; import { CALENDAR, EPOCHNANOSECONDS, @@ -21,6 +21,10 @@ import { DateTimeFormat } from './intl'; import type { ZonedDateTimeParams as Params, ZonedDateTimeReturn as Return } from './internaltypes'; import bigInt from 'big-integer'; +import { PlainDateTime } from './plaindatetime'; +import { PlainTime } from './plaintime'; +import { Duration } from './duration'; +import { Instant } from './instant'; const ArrayPrototypePush = Array.prototype.push; @@ -133,13 +137,12 @@ export class ZonedDateTime implements Temporal.ZonedDateTime { get hoursInDay(): Return['hoursInDay'] { if (!ES.IsTemporalZonedDateTime(this)) throw new TypeError('invalid receiver'); const dt = dateTime(this); - const DateTime = GetIntrinsic('%Temporal.PlainDateTime%'); const year = GetSlot(dt, ISO_YEAR); const month = GetSlot(dt, ISO_MONTH); const day = GetSlot(dt, ISO_DAY); - const today = new DateTime(year, month, day, 0, 0, 0, 0, 0, 0); + const today = new PlainDateTime(year, month, day, 0, 0, 0, 0, 0, 0); const tomorrowFields = ES.AddISODate(year, month, day, 0, 0, 0, 1, 'reject'); - const tomorrow = new DateTime(tomorrowFields.year, tomorrowFields.month, tomorrowFields.day, 0, 0, 0, 0, 0, 0); + const tomorrow = new PlainDateTime(tomorrowFields.year, tomorrowFields.month, tomorrowFields.day, 0, 0, 0, 0, 0, 0); const timeZone = GetSlot(this, TIME_ZONE); const todayNs = GetSlot(ES.BuiltinTimeZoneGetInstantFor(timeZone, today, 'compatible'), EPOCHNANOSECONDS); const tomorrowNs = GetSlot(ES.BuiltinTimeZoneGetInstantFor(timeZone, tomorrow, 'compatible'), EPOCHNANOSECONDS); @@ -269,7 +272,6 @@ export class ZonedDateTime implements Temporal.ZonedDateTime { calendar = ES.ConsolidateCalendars(GetSlot(this, CALENDAR), calendar); const timeZone = GetSlot(this, TIME_ZONE); - const PlainDateTime = GetIntrinsic('%Temporal.PlainDateTime%'); const dt = new PlainDateTime( year, month, @@ -288,7 +290,6 @@ export class ZonedDateTime implements Temporal.ZonedDateTime { withPlainTime(temporalTimeParam: Params['withPlainTime'][0] = undefined): Return['withPlainTime'] { if (!ES.IsTemporalZonedDateTime(this)) throw new TypeError('invalid receiver'); - const PlainTime = GetIntrinsic('%Temporal.PlainTime%'); const temporalTime = temporalTimeParam == undefined ? new PlainTime() : ES.ToTemporalTime(temporalTimeParam); const thisDt = dateTime(this); @@ -304,7 +305,6 @@ export class ZonedDateTime implements Temporal.ZonedDateTime { const nanosecond = GetSlot(temporalTime, ISO_NANOSECOND); const timeZone = GetSlot(this, TIME_ZONE); - const PlainDateTime = GetIntrinsic('%Temporal.PlainDateTime%'); const dt = new PlainDateTime( year, month, @@ -474,7 +474,6 @@ export class ZonedDateTime implements Temporal.ZonedDateTime { )); } - const Duration = GetIntrinsic('%Temporal.Duration%'); return new Duration(years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds); } since(otherParam: Params['since'][0], optionsParam: Params['since'][1] = undefined) { @@ -569,7 +568,6 @@ export class ZonedDateTime implements Temporal.ZonedDateTime { )); } - const Duration = GetIntrinsic('%Temporal.Duration%'); return new Duration( -years, -months, @@ -616,10 +614,19 @@ export class ZonedDateTime implements Temporal.ZonedDateTime { let microsecond = GetSlot(dt, ISO_MICROSECOND); let nanosecond = GetSlot(dt, ISO_NANOSECOND); - const DateTime = GetIntrinsic('%Temporal.PlainDateTime%'); const timeZone = GetSlot(this, TIME_ZONE); const calendar = GetSlot(this, CALENDAR); - const dtStart = new DateTime(GetSlot(dt, ISO_YEAR), GetSlot(dt, ISO_MONTH), GetSlot(dt, ISO_DAY), 0, 0, 0, 0, 0, 0); + const dtStart = new PlainDateTime( + GetSlot(dt, ISO_YEAR), + GetSlot(dt, ISO_MONTH), + GetSlot(dt, ISO_DAY), + 0, + 0, + 0, + 0, + 0, + 0 + ); const instantStart = ES.BuiltinTimeZoneGetInstantFor(timeZone, dtStart, 'compatible'); const endNs = ES.AddZonedDateTime(instantStart, timeZone, calendar, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0); const dayLengthNs = endNs.subtract(GetSlot(instantStart, EPOCHNANOSECONDS)); @@ -710,9 +717,8 @@ export class ZonedDateTime implements Temporal.ZonedDateTime { startOfDay(): Return['startOfDay'] { if (!ES.IsTemporalZonedDateTime(this)) throw new TypeError('invalid receiver'); const dt = dateTime(this); - const DateTime = GetIntrinsic('%Temporal.PlainDateTime%'); const calendar = GetSlot(this, CALENDAR); - const dtStart = new DateTime( + const dtStart = new PlainDateTime( GetSlot(dt, ISO_YEAR), GetSlot(dt, ISO_MONTH), GetSlot(dt, ISO_DAY), @@ -730,8 +736,7 @@ export class ZonedDateTime implements Temporal.ZonedDateTime { } toInstant(): Return['toInstant'] { if (!ES.IsTemporalZonedDateTime(this)) throw new TypeError('invalid receiver'); - const TemporalInstant = GetIntrinsic('%Temporal.Instant%'); - return new TemporalInstant(GetSlot(this, EPOCHNANOSECONDS)); + return new Instant(GetSlot(this, EPOCHNANOSECONDS)); } toPlainDate(): Return['toPlainDate'] { if (!ES.IsTemporalZonedDateTime(this)) throw new TypeError('invalid receiver');