Skip to content

Commit

Permalink
Editorial: Rename Normalized Time Duration to time duration
Browse files Browse the repository at this point in the history
A normalized time duration is just a number of nanoseconds; we originally
planned to convert it to a record with a seconds and subseconds field, but
that was never needed. So we don't need to have it be a record in the
first place. Rename it to "time duration" (removing "normalized" for the
same reason as in the previous commits) and define it as an integer within
a certain range.

Renames:
- all of the NormalizedTimeDuration___ operations to just TimeDuration___
- NormalizeTimeDuration to TimeDurationFromComponents
- Internal duration [[NormalizedTime]] field to [[Time]]
- "norm" variables to "timeDuration" or "time" if clear from context

See: #2953
  • Loading branch information
ptomato committed Oct 9, 2024
1 parent ff968bd commit 4405f30
Show file tree
Hide file tree
Showing 13 changed files with 287 additions and 310 deletions.
12 changes: 6 additions & 6 deletions polyfill/lib/duration.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ export class Duration {

if (plainRelativeTo) {
let duration = ES.ToInternalDurationRecordWith24HourDays(this);
const targetTime = ES.AddTime(ES.MidnightTimeRecord(), duration.norm);
const targetTime = ES.AddTime(ES.MidnightTimeRecord(), duration.time);

// Delegate the date part addition to the calendar
const isoRelativeToDate = GetSlot(plainRelativeTo, ISO_DATE);
Expand Down Expand Up @@ -311,7 +311,7 @@ export class Duration {

if (plainRelativeTo) {
const duration = ES.ToInternalDurationRecordWith24HourDays(this);
let targetTime = ES.AddTime(ES.MidnightTimeRecord(), duration.norm);
let targetTime = ES.AddTime(ES.MidnightTimeRecord(), duration.time);

// Delegate the date part addition to the calendar
const isoRelativeToDate = GetSlot(plainRelativeTo, ISO_DATE);
Expand All @@ -333,7 +333,7 @@ export class Duration {
throw new RangeErrorCtor(`a starting point is required for ${unit}s total`);
}
const duration = ES.ToInternalDurationRecordWith24HourDays(this);
return ES.TotalTimeDuration(duration.norm, unit);
return ES.TotalTimeDuration(duration.time, unit);
}
toString(options = undefined) {
if (!ES.IsTemporalDuration(this)) throw new TypeErrorCtor('invalid receiver');
Expand Down Expand Up @@ -423,9 +423,9 @@ export class Duration {
d1 = ES.DateDurationDays(duration1.date, plainRelativeTo);
d2 = ES.DateDurationDays(duration2.date, plainRelativeTo);
}
const norm1 = duration1.norm.add24HourDays(d1);
const norm2 = duration2.norm.add24HourDays(d2);
return norm1.cmp(norm2);
const timeDuration1 = duration1.time.add24HourDays(d1);
const timeDuration2 = duration2.time.add24HourDays(d2);
return timeDuration1.cmp(timeDuration2);
}
}

Expand Down
144 changes: 72 additions & 72 deletions polyfill/lib/ecmascript.mjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion polyfill/lib/math.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export function GetUnsignedRoundingMode(mode, sign) {
}

// Omits first step from spec algorithm so that it can be used both for
// RoundNumberToIncrement and RoundNormalizedTimeDurationToIncrement
// RoundNumberToIncrement and RoundTimeDurationToIncrement
export function ApplyUnsignedRoundingMode(r1, r2, cmp, evenCardinality, unsignedRoundingMode) {
if (unsignedRoundingMode === 'zero') return r1;
if (unsignedRoundingMode === 'infinity') return r2;
Expand Down
2 changes: 1 addition & 1 deletion polyfill/lib/timeduration.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class TimeDuration {
return new TimeDuration(diff);
}

static normalize(h, min, s, ms, µs, ns) {
static fromComponents(h, min, s, ms, µs, ns) {
const totalNs = bigInt(ns)
.add(bigInt(µs).multiply(1e3))
.add(bigInt(ms).multiply(1e6))
Expand Down
46 changes: 23 additions & 23 deletions polyfill/test/timeduration.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -82,40 +82,40 @@ describe('Normalized time duration', () => {
});
});

describe('normalize()', () => {
describe('fromComponents()', () => {
it('basic', () => {
check(TimeDuration.normalize(1, 1, 1, 1, 1, 1), 3661, 1001001);
check(TimeDuration.normalize(-1, -1, -1, -1, -1, -1), -3661, -1001001);
check(TimeDuration.fromComponents(1, 1, 1, 1, 1, 1), 3661, 1001001);
check(TimeDuration.fromComponents(-1, -1, -1, -1, -1, -1), -3661, -1001001);
});

it('overflow from one unit to another', () => {
check(TimeDuration.normalize(1, 61, 61, 998, 1000, 1000), 7321, 999001000);
check(TimeDuration.normalize(-1, -61, -61, -998, -1000, -1000), -7321, -999001000);
check(TimeDuration.fromComponents(1, 61, 61, 998, 1000, 1000), 7321, 999001000);
check(TimeDuration.fromComponents(-1, -61, -61, -998, -1000, -1000), -7321, -999001000);
});

it('overflow from subseconds to seconds', () => {
check(TimeDuration.normalize(0, 0, 1, 1000, 0, 0), 2, 0);
check(TimeDuration.normalize(0, 0, -1, -1000, 0, 0), -2, 0);
check(TimeDuration.fromComponents(0, 0, 1, 1000, 0, 0), 2, 0);
check(TimeDuration.fromComponents(0, 0, -1, -1000, 0, 0), -2, 0);
});

it('multiple overflows from subseconds to seconds', () => {
check(TimeDuration.normalize(0, 0, 0, 1234567890, 1234567890, 1234567890), 1235803, 692457890);
check(TimeDuration.normalize(0, 0, 0, -1234567890, -1234567890, -1234567890), -1235803, -692457890);
check(TimeDuration.fromComponents(0, 0, 0, 1234567890, 1234567890, 1234567890), 1235803, 692457890);
check(TimeDuration.fromComponents(0, 0, 0, -1234567890, -1234567890, -1234567890), -1235803, -692457890);
});

it('fails on overflow', () => {
throws(() => TimeDuration.normalize(2501999792984, 0, 0, 0, 0, 0), RangeError);
throws(() => TimeDuration.normalize(-2501999792984, 0, 0, 0, 0, 0), RangeError);
throws(() => TimeDuration.normalize(0, 150119987579017, 0, 0, 0, 0), RangeError);
throws(() => TimeDuration.normalize(0, -150119987579017, 0, 0, 0, 0), RangeError);
throws(() => TimeDuration.normalize(0, 0, 2 ** 53, 0, 0, 0), RangeError);
throws(() => TimeDuration.normalize(0, 0, -(2 ** 53), 0, 0, 0), RangeError);
throws(() => TimeDuration.normalize(0, 0, Number.MAX_SAFE_INTEGER, 1000, 0, 0), RangeError);
throws(() => TimeDuration.normalize(0, 0, -Number.MAX_SAFE_INTEGER, -1000, 0, 0), RangeError);
throws(() => TimeDuration.normalize(0, 0, Number.MAX_SAFE_INTEGER, 0, 1000000, 0), RangeError);
throws(() => TimeDuration.normalize(0, 0, -Number.MAX_SAFE_INTEGER, 0, -1000000, 0), RangeError);
throws(() => TimeDuration.normalize(0, 0, Number.MAX_SAFE_INTEGER, 0, 0, 1000000000), RangeError);
throws(() => TimeDuration.normalize(0, 0, -Number.MAX_SAFE_INTEGER, 0, 0, -1000000000), RangeError);
throws(() => TimeDuration.fromComponents(2501999792984, 0, 0, 0, 0, 0), RangeError);
throws(() => TimeDuration.fromComponents(-2501999792984, 0, 0, 0, 0, 0), RangeError);
throws(() => TimeDuration.fromComponents(0, 150119987579017, 0, 0, 0, 0), RangeError);
throws(() => TimeDuration.fromComponents(0, -150119987579017, 0, 0, 0, 0), RangeError);
throws(() => TimeDuration.fromComponents(0, 0, 2 ** 53, 0, 0, 0), RangeError);
throws(() => TimeDuration.fromComponents(0, 0, -(2 ** 53), 0, 0, 0), RangeError);
throws(() => TimeDuration.fromComponents(0, 0, Number.MAX_SAFE_INTEGER, 1000, 0, 0), RangeError);
throws(() => TimeDuration.fromComponents(0, 0, -Number.MAX_SAFE_INTEGER, -1000, 0, 0), RangeError);
throws(() => TimeDuration.fromComponents(0, 0, Number.MAX_SAFE_INTEGER, 0, 1000000, 0), RangeError);
throws(() => TimeDuration.fromComponents(0, 0, -Number.MAX_SAFE_INTEGER, 0, -1000000, 0), RangeError);
throws(() => TimeDuration.fromComponents(0, 0, Number.MAX_SAFE_INTEGER, 0, 0, 1000000000), RangeError);
throws(() => TimeDuration.fromComponents(0, 0, -Number.MAX_SAFE_INTEGER, 0, 0, -1000000000), RangeError);
});
});

Expand Down Expand Up @@ -343,7 +343,7 @@ describe('Normalized time duration', () => {
});

it('quotient larger than seconds', () => {
const d = TimeDuration.normalize(25 + 5 * 24, 0, 86401, 333, 666, 999);
const d = TimeDuration.fromComponents(25 + 5 * 24, 0, 86401, 333, 666, 999);
const { quotient, remainder } = d.divmod(86400e9);
equal(quotient, 7);
check(remainder, 3601, 333666999);
Expand Down Expand Up @@ -401,7 +401,7 @@ describe('Normalized time duration', () => {
});

it('quotient larger than seconds', () => {
const d = TimeDuration.normalize(25 + 5 * 24, 0, 86401, 333, 666, 999);
const d = TimeDuration.fromComponents(25 + 5 * 24, 0, 86401, 333, 666, 999);
checkFloat(d.fdiv(86400e9), 7.041682102627303);
});

Expand Down
Loading

0 comments on commit 4405f30

Please sign in to comment.