Skip to content

Commit

Permalink
Split non-ZonedDateTime code path out of NanosecondsToDays
Browse files Browse the repository at this point in the history
Similar to the previous commit, this makes NanosecondsToDays easier to
understand and reason about. After splitting BalanceTimeDuration and
BalanceTimeDurationRelative, there was only one remaining call site of
NanosecondsToDays with a possible non-ZonedDateTime relativeTo, in
RoundDuration. Instead, inline the 24-hour day case into the call site.

UPSTREAM_COMMIT=dffb9298e0c95c8212a624058652ee7f8dba2cdd
  • Loading branch information
ptomato authored and 12wrigja committed Jan 12, 2024
1 parent 935bb23 commit b510572
Showing 1 changed file with 17 additions and 19 deletions.
36 changes: 17 additions & 19 deletions lib/ecmascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3735,24 +3735,18 @@ export function TotalDurationNanoseconds(
return JSBI.add(JSBI.BigInt(nanoseconds), JSBI.multiply(microseconds, THOUSAND));
}

function NanosecondsToDays(nanosecondsParam: JSBI, relativeTo: ReturnType<typeof ToRelativeTemporalObject>) {
function NanosecondsToDays(nanosecondsParam: JSBI, zonedRelativeTo: Temporal.ZonedDateTime) {
const TemporalInstant = GetIntrinsic('%Temporal.Instant%');
const sign = MathSign(JSBI.toNumber(nanosecondsParam));
let nanoseconds = JSBI.BigInt(nanosecondsParam);
let dayLengthNs = 86400e9;
if (sign === 0) return { days: 0, nanoseconds: ZERO, dayLengthNs };
if (!IsTemporalZonedDateTime(relativeTo)) {
let days: JSBI;
({ quotient: days, remainder: nanoseconds } = divmod(nanoseconds, JSBI.BigInt(dayLengthNs)));
return { days: JSBI.toNumber(days), nanoseconds, dayLengthNs };
}
if (sign === 0) return { days: 0, nanoseconds: ZERO, dayLengthNs: JSBI.toNumber(DAY_NANOS) };

const startNs = GetSlot(relativeTo, EPOCHNANOSECONDS);
const start = GetSlot(relativeTo, INSTANT);
const startNs = GetSlot(zonedRelativeTo, EPOCHNANOSECONDS);
const start = GetSlot(zonedRelativeTo, INSTANT);
const endNs = JSBI.add(startNs, nanoseconds);
const end = new TemporalInstant(endNs);
const timeZone = GetSlot(relativeTo, TIME_ZONE);
const calendar = GetSlot(relativeTo, CALENDAR);
const timeZone = GetSlot(zonedRelativeTo, TIME_ZONE);
const calendar = GetSlot(zonedRelativeTo, CALENDAR);

// Find the difference in days only.
const dtStart = GetPlainDateTimeFor(timeZone, start, calendar);
Expand Down Expand Up @@ -3817,6 +3811,7 @@ function NanosecondsToDays(nanosecondsParam: JSBI, relativeTo: ReturnType<typeof

let isOverflow = false;
let relativeInstant = new TemporalInstant(intermediateNs);
let dayLengthNs;
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);
Expand Down Expand Up @@ -6193,15 +6188,18 @@ export function RoundDuration(
// larger. We'll cast away `undefined` when it's used lower down below.
let dayLengthNs: JSBI | undefined;
if (unit === 'year' || unit === 'month' || unit === 'week' || unit === 'day') {
nanoseconds = TotalDurationNanoseconds(0, hours, minutes, seconds, milliseconds, microseconds, nanosecondsParam, 0);
let intermediate;
nanoseconds = TotalDurationNanoseconds(0, hours, minutes, seconds, milliseconds, microseconds, nanoseconds, 0);
let deltaDays;
if (zdtRelative) {
intermediate = MoveRelativeZonedDateTime(zdtRelative, years, months, weeks, days);
const intermediate = MoveRelativeZonedDateTime(zdtRelative, years, months, weeks, days);
let dayLengthNumber;
({ days: deltaDays, nanoseconds, dayLengthNs: dayLengthNumber } = NanosecondsToDays(nanoseconds, intermediate));
dayLengthNs = JSBI.BigInt(dayLengthNumber);
} else {
({ quotient: deltaDays, remainder: nanoseconds } = divmod(nanoseconds, DAY_NANOS));
deltaDays = JSBI.toNumber(deltaDays);
dayLengthNs = DAY_NANOS;
}
let deltaDays;
let dayLength: number;
({ days: deltaDays, nanoseconds, dayLengthNs: dayLength } = NanosecondsToDays(nanoseconds, intermediate));
dayLengthNs = JSBI.BigInt(dayLength);
days += deltaDays;
hours = minutes = seconds = milliseconds = microseconds = 0;
}
Expand Down

0 comments on commit b510572

Please sign in to comment.