From ad49d4d0cc8028364ab9169b28270b0085618df2 Mon Sep 17 00:00:00 2001 From: Adam Shaw Date: Thu, 28 Mar 2024 20:28:16 -0400 Subject: [PATCH] some more fixes, diffEpochMilliByDay does its own flooring --- packages/temporal-polyfill/src/classApi/zonedDateTime.ts | 4 +++- packages/temporal-polyfill/src/internal/calendarNative.ts | 5 +++-- packages/temporal-polyfill/src/internal/diff.ts | 7 ++----- packages/temporal-polyfill/src/internal/isoParse.ts | 3 +-- packages/temporal-polyfill/src/internal/timeMath.ts | 8 +++++--- packages/temporal-polyfill/src/internal/timeZoneNative.ts | 2 +- packages/temporal-polyfill/src/internal/timeZoneOps.ts | 2 +- 7 files changed, 16 insertions(+), 15 deletions(-) diff --git a/packages/temporal-polyfill/src/classApi/zonedDateTime.ts b/packages/temporal-polyfill/src/classApi/zonedDateTime.ts index 267e8154..1e433ac3 100644 --- a/packages/temporal-polyfill/src/classApi/zonedDateTime.ts +++ b/packages/temporal-polyfill/src/classApi/zonedDateTime.ts @@ -282,7 +282,9 @@ export const [ZonedDateTime, createZonedDateTime] = createSlotClass( startOfDay( slots: ZonedDateTimeSlots, ): ZonedDateTime { - return computeZonedStartOfDay(createTimeZoneOps, slots) + return createZonedDateTime( + computeZonedStartOfDay(createTimeZoneOps, slots), + ) }, equals( slots: ZonedDateTimeSlots, diff --git a/packages/temporal-polyfill/src/internal/calendarNative.ts b/packages/temporal-polyfill/src/internal/calendarNative.ts index 4a893419..59e487b9 100644 --- a/packages/temporal-polyfill/src/internal/calendarNative.ts +++ b/packages/temporal-polyfill/src/internal/calendarNative.ts @@ -16,6 +16,7 @@ import { import { diffEpochMilliByDay } from './diff' import * as errorMessages from './errorMessages' import { IsoDateFields } from './isoFields' +import { isoToEpochMilli } from './timeMath' import { padNumber2 } from './utils' // TODO: move most of these types into CalendarOps? @@ -300,12 +301,12 @@ export function computeNativeDaysInYear( } export function computeNativeDayOfYear( - this: { dateParts: DatePartsOp; epochMilli: EpochMilliOp }, + this: NativeDayOfYearOps, isoFields: IsoDateFields, ): number { const [year] = this.dateParts(isoFields) const milli0 = this.epochMilli(year) - const milli1 = this.epochMilli(year + 1) + const milli1 = isoToEpochMilli(isoFields)! return diffEpochMilliByDay(milli0, milli1) + 1 } diff --git a/packages/temporal-polyfill/src/internal/diff.ts b/packages/temporal-polyfill/src/internal/diff.ts index d8bebb40..3c284643 100644 --- a/packages/temporal-polyfill/src/internal/diff.ts +++ b/packages/temporal-polyfill/src/internal/diff.ts @@ -291,7 +291,7 @@ export function diffPlainYearMonth( function diffDateLike( invert: boolean, - getCalendarOps: () => DiffOps, + getCalendarOps: () => DiffOps, // TODO: devise better system! startIsoFields: IsoDateFields, endIsoFields: IsoDateFields, largestUnit: Unit, // TODO: large field @@ -604,14 +604,11 @@ export function diffDays( ) } -/* -Must always be given start-of-day -*/ export function diffEpochMilliByDay( epochMilli0: number, epochMilli1: number, ): number { - return Math.round((epochMilli1 - epochMilli0) / milliInDay) + return Math.floor((epochMilli1 - epochMilli0) / milliInDay) } // Native diff --git a/packages/temporal-polyfill/src/internal/isoParse.ts b/packages/temporal-polyfill/src/internal/isoParse.ts index 08c9934d..1e394001 100644 --- a/packages/temporal-polyfill/src/internal/isoParse.ts +++ b/packages/temporal-polyfill/src/internal/isoParse.ts @@ -38,7 +38,6 @@ import { createZonedDateTimeSlots, } from './slots' import { - checkEpochNanoInBounds, checkIsoDateInBounds, checkIsoDateTimeInBounds, checkIsoYearMonthInBounds, @@ -93,7 +92,7 @@ export function parseInstant(s: string): InstantSlots { offsetNano, ) - return createInstantSlots(checkEpochNanoInBounds(epochNanoseconds)) + return createInstantSlots(epochNanoseconds) } export function parseRelativeToSlots( diff --git a/packages/temporal-polyfill/src/internal/timeMath.ts b/packages/temporal-polyfill/src/internal/timeMath.ts index fd83e56d..358a9b63 100644 --- a/packages/temporal-polyfill/src/internal/timeMath.ts +++ b/packages/temporal-polyfill/src/internal/timeMath.ts @@ -221,20 +221,22 @@ export function isoToEpochNano( /* For converting to proper epochNano values -Ensures in bounds +CALLERS DO NOT NEED TO CHECK in-bounds! +(Result should be considered a finalized "Instant") */ export function isoToEpochNanoWithOffset( isoFields: IsoDateTimeFields, offsetNano: number, -): BigNano | undefined { +): BigNano { const [newIsoTimeFields, dayDelta] = nanoToIsoTimeAndDay( isoTimeFieldsToNano(isoFields) - offsetNano, ) - return isoToEpochNano({ + const epochNano = isoToEpochNano({ ...isoFields, isoDay: isoFields.isoDay + dayDelta, ...newIsoTimeFields, }) + return checkEpochNanoInBounds(epochNano) } // ISO Arguments -> Epoch diff --git a/packages/temporal-polyfill/src/internal/timeZoneNative.ts b/packages/temporal-polyfill/src/internal/timeZoneNative.ts index 84f23363..8eff5891 100644 --- a/packages/temporal-polyfill/src/internal/timeZoneNative.ts +++ b/packages/temporal-polyfill/src/internal/timeZoneNative.ts @@ -44,7 +44,7 @@ export class FixedTimeZone implements NativeTimeZone { } getPossibleInstantsFor(isoDateTimeFields: IsoDateTimeFields): BigNano[] { - return [isoToEpochNanoWithOffset(isoDateTimeFields, this.offsetNano)!] + return [isoToEpochNanoWithOffset(isoDateTimeFields, this.offsetNano)] } getTransition(): BigNano | undefined { diff --git a/packages/temporal-polyfill/src/internal/timeZoneOps.ts b/packages/temporal-polyfill/src/internal/timeZoneOps.ts index 94293961..08925326 100644 --- a/packages/temporal-polyfill/src/internal/timeZoneOps.ts +++ b/packages/temporal-polyfill/src/internal/timeZoneOps.ts @@ -101,7 +101,7 @@ export function getMatchingInstantFor( if (offsetNano !== undefined && offsetDisambig === OffsetDisambig.Use) { // we ALWAYS use Z as a zero offset if (offsetDisambig === OffsetDisambig.Use || hasZ) { - return isoToEpochNanoWithOffset(isoFields, offsetNano)! + return isoToEpochNanoWithOffset(isoFields, offsetNano) } }