Skip to content

Commit

Permalink
some more fixes, diffEpochMilliByDay does its own flooring
Browse files Browse the repository at this point in the history
  • Loading branch information
arshaw committed Mar 29, 2024
1 parent 129fbeb commit ad49d4d
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 15 deletions.
4 changes: 3 additions & 1 deletion packages/temporal-polyfill/src/classApi/zonedDateTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,9 @@ export const [ZonedDateTime, createZonedDateTime] = createSlotClass(
startOfDay(
slots: ZonedDateTimeSlots<CalendarSlot, TimeZoneSlot>,
): ZonedDateTime {
return computeZonedStartOfDay(createTimeZoneOps, slots)
return createZonedDateTime(
computeZonedStartOfDay(createTimeZoneOps, slots),
)
},
equals(
slots: ZonedDateTimeSlots<CalendarSlot, TimeZoneSlot>,
Expand Down
5 changes: 3 additions & 2 deletions packages/temporal-polyfill/src/internal/calendarNative.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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
}

Expand Down
7 changes: 2 additions & 5 deletions packages/temporal-polyfill/src/internal/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ export function diffPlainYearMonth<C extends IdLike>(

function diffDateLike(
invert: boolean,
getCalendarOps: () => DiffOps,
getCalendarOps: () => DiffOps, // TODO: devise better system!
startIsoFields: IsoDateFields,
endIsoFields: IsoDateFields,
largestUnit: Unit, // TODO: large field
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions packages/temporal-polyfill/src/internal/isoParse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import {
createZonedDateTimeSlots,
} from './slots'
import {
checkEpochNanoInBounds,
checkIsoDateInBounds,
checkIsoDateTimeInBounds,
checkIsoYearMonthInBounds,
Expand Down Expand Up @@ -93,7 +92,7 @@ export function parseInstant(s: string): InstantSlots {
offsetNano,
)

return createInstantSlots(checkEpochNanoInBounds(epochNanoseconds))
return createInstantSlots(epochNanoseconds)
}

export function parseRelativeToSlots(
Expand Down
8 changes: 5 additions & 3 deletions packages/temporal-polyfill/src/internal/timeMath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/temporal-polyfill/src/internal/timeZoneNative.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion packages/temporal-polyfill/src/internal/timeZoneOps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down

0 comments on commit ad49d4d

Please sign in to comment.