Expand toString() rounding tests from PlainTime to cover other types

This takes the tests of the rounding functionality of
Temporal.PlainTime.p.toString() and adds similar tests covering the
equivalent functionality to Duration, Instant, PlainDateTime, and
ZonedDateTime: all the types that have rounding and precision controls
for how they output their subsecond values.

It also takes the opportunity to improve the existing PlainTime tests:

- fractionalseconddigits-auto.js: More descriptive variable names. Added
  assertion messages.
- fractionalseconddigits-number.js: Ditto.
- rounding-cross-midnight.js: Use constructor directly to remove coupling
  with from().
- roundingmode-*.js: Add additional tests for specifying the precision
  using fractionalSecondDigits.
- smallestunit-fractionalseconddigits.js: Add assertion messages.
This commit is contained in:
Philip Chimento 2022-04-08 11:39:40 -07:00 committed by Ms2ger
parent 4ac16c2589
commit 3905c0c80a
39 changed files with 1084 additions and 43 deletions

View File

@ -0,0 +1,21 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.duration.prototype.tostring
description: auto value for fractionalSecondDigits option
features: [Temporal]
---*/
const wholeSeconds = new Temporal.Duration(1, 2, 3, 4, 5, 6, 7);
const subSeconds = new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 987, 650);
const tests = [
[wholeSeconds, "P1Y2M3W4DT5H6M7S"],
[subSeconds, "P1Y2M3W4DT5H6M7.98765S"],
];
for (const [duration, expected] of tests) {
assert.sameValue(duration.toString(), expected, "default is to emit seconds and drop trailing zeroes");
assert.sameValue(duration.toString({ fractionalSecondDigits: "auto" }), expected, "auto is the default");
}

View File

@ -0,0 +1,28 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.duration.prototype.tostring
description: Number for fractionalSecondDigits option
features: [Temporal]
---*/
const wholeSeconds = new Temporal.Duration(1, 2, 3, 4, 5, 6, 7);
const subSeconds = new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 987, 650);
assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 0 }), "P1Y2M3W4DT5H6M7S",
"truncates 4 decimal places to 0");
assert.sameValue(wholeSeconds.toString({ fractionalSecondDigits: 2 }), "P1Y2M3W4DT5H6M7.00S",
"pads whole seconds to 2 decimal places");
assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 2 }), "P1Y2M3W4DT5H6M7.98S",
"truncates 4 decimal places to 2");
assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 3 }), "P1Y2M3W4DT5H6M7.987S",
"truncates 4 decimal places to 3");
assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 6 }), "P1Y2M3W4DT5H6M7.987650S",
"pads 4 decimal places to 6");
assert.sameValue(wholeSeconds.toString({ fractionalSecondDigits: 7 }), "P1Y2M3W4DT5H6M7.0000000S",
"pads whole seconds to 7 decimal places");
assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 7 }), "P1Y2M3W4DT5H6M7.9876500S",
"pads 4 decimal places to 7");
assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 9 }), "P1Y2M3W4DT5H6M7.987650000S",
"pads 4 decimal places to 9");

View File

@ -0,0 +1,34 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.duration.prototype.tostring
description: ceil value for roundingMode option
features: [Temporal]
---*/
const duration = new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 123, 987, 500);
const result1 = duration.toString({ smallestUnit: "microsecond", roundingMode: "ceil" });
assert.sameValue(result1, "P1Y2M3W4DT5H6M7.123988S",
"roundingMode is ceil (with 6 digits from smallestUnit)");
const result2 = duration.toString({ fractionalSecondDigits: 6, roundingMode: "ceil" });
assert.sameValue(result2, "P1Y2M3W4DT5H6M7.123988S",
"roundingMode is ceil (with 6 digits from fractionalSecondDigits)");
const result3 = duration.toString({ smallestUnit: "millisecond", roundingMode: "ceil" });
assert.sameValue(result3, "P1Y2M3W4DT5H6M7.124S",
"roundingMode is ceil (with 3 digits from smallestUnit)");
const result4 = duration.toString({ fractionalSecondDigits: 3, roundingMode: "ceil" });
assert.sameValue(result4, "P1Y2M3W4DT5H6M7.124S",
"roundingMode is ceil (with 3 digits from fractionalSecondDigits)");
const result5 = duration.toString({ smallestUnit: "second", roundingMode: "ceil" });
assert.sameValue(result5, "P1Y2M3W4DT5H6M8S",
"roundingMode is ceil (with 0 digits from smallestUnit)");
const result6 = duration.toString({ fractionalSecondDigits: 0, roundingMode: "ceil" });
assert.sameValue(result6, "P1Y2M3W4DT5H6M8S",
"roundingMode is ceil (with 0 digits from fractionalSecondDigits)");

View File

@ -0,0 +1,34 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.duration.prototype.tostring
description: floor value for roundingMode option
features: [Temporal]
---*/
const duration = new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 123, 987, 500);
const result1 = duration.toString({ smallestUnit: "microsecond", roundingMode: "floor" });
assert.sameValue(result1, "P1Y2M3W4DT5H6M7.123987S",
"roundingMode is floor (with 6 digits from smallestUnit)");
const result2 = duration.toString({ fractionalSecondDigits: 6, roundingMode: "floor" });
assert.sameValue(result2, "P1Y2M3W4DT5H6M7.123987S",
"roundingMode is floor (with 6 digits from fractionalSecondDigits)");
const result3 = duration.toString({ smallestUnit: "millisecond", roundingMode: "floor" });
assert.sameValue(result3, "P1Y2M3W4DT5H6M7.123S",
"roundingMode is floor (with 3 digits from smallestUnit)");
const result4 = duration.toString({ fractionalSecondDigits: 3, roundingMode: "floor" });
assert.sameValue(result4, "P1Y2M3W4DT5H6M7.123S",
"roundingMode is floor (with 3 digits from fractionalSecondDigits)");
const result5 = duration.toString({ smallestUnit: "second", roundingMode: "floor" });
assert.sameValue(result5, "P1Y2M3W4DT5H6M7S",
"roundingMode is floor (with 0 digits from smallestUnit)");
const result6 = duration.toString({ fractionalSecondDigits: 0, roundingMode: "floor" });
assert.sameValue(result6, "P1Y2M3W4DT5H6M7S",
"roundingMode is floor (with 0 digits from fractionalSecondDigits)");

View File

@ -0,0 +1,34 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.duration.prototype.tostring
description: halfExpand value for roundingMode option
features: [Temporal]
---*/
const duration = new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 123, 987, 500);
const result1 = duration.toString({ smallestUnit: "microsecond", roundingMode: "halfExpand" });
assert.sameValue(result1, "P1Y2M3W4DT5H6M7.123988S",
"roundingMode is halfExpand (with 6 digits from smallestUnit)");
const result2 = duration.toString({ fractionalSecondDigits: 6, roundingMode: "halfExpand" });
assert.sameValue(result2, "P1Y2M3W4DT5H6M7.123988S",
"roundingMode is halfExpand (with 6 digits from fractionalSecondDigits)");
const result3 = duration.toString({ smallestUnit: "millisecond", roundingMode: "halfExpand" });
assert.sameValue(result3, "P1Y2M3W4DT5H6M7.124S",
"roundingMode is halfExpand (with 3 digits from smallestUnit)");
const result4 = duration.toString({ fractionalSecondDigits: 3, roundingMode: "halfExpand" });
assert.sameValue(result4, "P1Y2M3W4DT5H6M7.124S",
"roundingMode is halfExpand (with 3 digits from fractionalSecondDigits)");
const result5 = duration.toString({ smallestUnit: "second", roundingMode: "halfExpand" });
assert.sameValue(result5, "P1Y2M3W4DT5H6M7S",
"roundingMode is halfExpand (with 0 digits from smallestUnit)");
const result6 = duration.toString({ fractionalSecondDigits: 0, roundingMode: "halfExpand" });
assert.sameValue(result6, "P1Y2M3W4DT5H6M7S",
"roundingMode is halfExpand (with 0 digits from fractionalSecondDigits)");

View File

@ -0,0 +1,34 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.duration.prototype.tostring
description: trunc value for roundingMode option
features: [Temporal]
---*/
const duration = new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 123, 987, 500);
const result1 = duration.toString({ smallestUnit: "microsecond", roundingMode: "trunc" });
assert.sameValue(result1, "P1Y2M3W4DT5H6M7.123987S",
"roundingMode is trunc (with 6 digits from smallestUnit)");
const result2 = duration.toString({ fractionalSecondDigits: 6, roundingMode: "trunc" });
assert.sameValue(result2, "P1Y2M3W4DT5H6M7.123987S",
"roundingMode is trunc (with 6 digits from fractionalSecondDigits)");
const result3 = duration.toString({ smallestUnit: "millisecond", roundingMode: "trunc" });
assert.sameValue(result3, "P1Y2M3W4DT5H6M7.123S",
"roundingMode is trunc (with 3 digits from smallestUnit)");
const result4 = duration.toString({ fractionalSecondDigits: 3, roundingMode: "trunc" });
assert.sameValue(result4, "P1Y2M3W4DT5H6M7.123S",
"roundingMode is trunc (with 3 digits from fractionalSecondDigits)");
const result5 = duration.toString({ smallestUnit: "second", roundingMode: "trunc" });
assert.sameValue(result5, "P1Y2M3W4DT5H6M7S",
"roundingMode is trunc (with 0 digits from smallestUnit)");
const result6 = duration.toString({ fractionalSecondDigits: 0, roundingMode: "trunc" });
assert.sameValue(result6, "P1Y2M3W4DT5H6M7S",
"roundingMode is trunc (with 0 digits from fractionalSecondDigits)");

View File

@ -0,0 +1,29 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.duration.prototype.tostring
description: fractionalSecondDigits option is not used with smallestUnit present
features: [Temporal]
---*/
const duration = new Temporal.Duration(1, 2, 3, 4, 5, 6, 7, 789, 999, 999);
const tests = [
["second", "P1Y2M3W4DT5H6M7S"],
["millisecond", "P1Y2M3W4DT5H6M7.789S"],
["microsecond", "P1Y2M3W4DT5H6M7.789999S"],
["nanosecond", "P1Y2M3W4DT5H6M7.789999999S"],
];
for (const [smallestUnit, expected] of tests) {
const string = duration.toString({
smallestUnit,
get fractionalSecondDigits() { throw new Test262Error("should not get fractionalSecondDigits") }
});
assert.sameValue(string, expected, `smallestUnit: "${smallestUnit}" overrides fractionalSecondDigits`);
}
assert.throws(RangeError, () => duration.toString({
smallestUnit: "hour",
get fractionalSecondDigits() { throw new Test262Error("should not get fractionalSecondDigits") }
}), "hour is an invalid smallestUnit but still overrides fractionalSecondDigits");

View File

@ -0,0 +1,23 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.instant.prototype.tostring
description: auto value for fractionalSecondDigits option
features: [BigInt, Temporal]
---*/
const zeroSeconds = new Temporal.Instant(0n);
const wholeSeconds = new Temporal.Instant(30_000_000_000n);
const subSeconds = new Temporal.Instant(30_123_400_000n);
const tests = [
[zeroSeconds, "1970-01-01T00:00:00Z"],
[wholeSeconds, "1970-01-01T00:00:30Z"],
[subSeconds, "1970-01-01T00:00:30.1234Z"],
];
for (const [instant, expected] of tests) {
assert.sameValue(instant.toString(), expected, "default is to emit seconds and drop trailing zeroes");
assert.sameValue(instant.toString({ fractionalSecondDigits: "auto" }), expected, "auto is the default");
}

View File

@ -0,0 +1,33 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.instant.prototype.tostring
description: Number for fractionalSecondDigits option
features: [BigInt, Temporal]
---*/
const zeroSeconds = new Temporal.Instant(0n);
const wholeSeconds = new Temporal.Instant(30_000_000_000n);
const subSeconds = new Temporal.Instant(30_123_400_000n);
assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 0 }), "1970-01-01T00:00:30Z",
"truncates 4 decimal places to 0");
assert.sameValue(zeroSeconds.toString({ fractionalSecondDigits: 2 }), "1970-01-01T00:00:00.00Z",
"pads zero seconds to 2 decimal places");
assert.sameValue(wholeSeconds.toString({ fractionalSecondDigits: 2 }), "1970-01-01T00:00:30.00Z",
"pads whole seconds to 2 decimal places");
assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 2 }), "1970-01-01T00:00:30.12Z",
"truncates 4 decimal places to 2");
assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 3 }), "1970-01-01T00:00:30.123Z",
"truncates 4 decimal places to 3");
assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 6 }), "1970-01-01T00:00:30.123400Z",
"pads 4 decimal places to 6");
assert.sameValue(zeroSeconds.toString({ fractionalSecondDigits: 7 }), "1970-01-01T00:00:00.0000000Z",
"pads zero seconds to 7 decimal places");
assert.sameValue(wholeSeconds.toString({ fractionalSecondDigits: 7 }), "1970-01-01T00:00:30.0000000Z",
"pads whole seconds to 7 decimal places");
assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 7 }), "1970-01-01T00:00:30.1234000Z",
"pads 4 decimal places to 7");
assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 9 }), "1970-01-01T00:00:30.123400000Z",
"pads 4 decimal places to 9");

View File

@ -0,0 +1,13 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.instant.prototype.tostring
description: Rounding can cross midnight
features: [Temporal]
---*/
const instant = new Temporal.Instant(946_684_799_999_999_999n); // one nanosecond before 2000-01-01T00:00:00
for (const roundingMode of ["ceil", "halfExpand"]) {
assert.sameValue(instant.toString({ fractionalSecondDigits: 8, roundingMode }), "2000-01-01T00:00:00.00000000Z");
}

View File

@ -0,0 +1,37 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.instant.prototype.tostring
description: ceil value for roundingMode option
features: [Temporal]
---*/
const instant = new Temporal.Instant(1_000_000_000_123_987_500n);
const result1 = instant.toString({ smallestUnit: "microsecond", roundingMode: "ceil" });
assert.sameValue(result1, "2001-09-09T01:46:40.123988Z",
"roundingMode is ceil (with 6 digits from smallestUnit)");
const result2 = instant.toString({ fractionalSecondDigits: 6, roundingMode: "ceil" });
assert.sameValue(result2, "2001-09-09T01:46:40.123988Z",
"roundingMode is ceil (with 6 digits from fractionalSecondDigits)");
const result3 = instant.toString({ smallestUnit: "millisecond", roundingMode: "ceil" });
assert.sameValue(result3, "2001-09-09T01:46:40.124Z",
"roundingMode is ceil (with 3 digits from smallestUnit)");
const result4 = instant.toString({ fractionalSecondDigits: 3, roundingMode: "ceil" });
assert.sameValue(result4, "2001-09-09T01:46:40.124Z",
"roundingMode is ceil (with 3 digits from fractionalSecondDigits)");
const result5 = instant.toString({ smallestUnit: "second", roundingMode: "ceil" });
assert.sameValue(result5, "2001-09-09T01:46:41Z",
"roundingMode is ceil (with 0 digits from smallestUnit)");
const result6 = instant.toString({ fractionalSecondDigits: 0, roundingMode: "ceil" });
assert.sameValue(result6, "2001-09-09T01:46:41Z",
"roundingMode is ceil (with 0 digits from fractionalSecondDigits)");
const result7 = instant.toString({ smallestUnit: "minute", roundingMode: "ceil" });
assert.sameValue(result7, "2001-09-09T01:47Z", "roundingMode is ceil (round to minute)");

View File

@ -0,0 +1,37 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.instant.prototype.tostring
description: floor value for roundingMode option
features: [Temporal]
---*/
const instant = new Temporal.Instant(1_000_000_000_123_987_500n);
const result1 = instant.toString({ smallestUnit: "microsecond", roundingMode: "floor" });
assert.sameValue(result1, "2001-09-09T01:46:40.123987Z",
"roundingMode is floor (with 6 digits from smallestUnit)");
const result2 = instant.toString({ fractionalSecondDigits: 6, roundingMode: "floor" });
assert.sameValue(result2, "2001-09-09T01:46:40.123987Z",
"roundingMode is floor (with 6 digits from fractionalSecondDigits)");
const result3 = instant.toString({ smallestUnit: "millisecond", roundingMode: "floor" });
assert.sameValue(result3, "2001-09-09T01:46:40.123Z",
"roundingMode is floor (with 3 digits from smallestUnit)");
const result4 = instant.toString({ fractionalSecondDigits: 3, roundingMode: "floor" });
assert.sameValue(result4, "2001-09-09T01:46:40.123Z",
"roundingMode is floor (with 3 digits from fractionalSecondDigits)");
const result5 = instant.toString({ smallestUnit: "second", roundingMode: "floor" });
assert.sameValue(result5, "2001-09-09T01:46:40Z",
"roundingMode is floor (with 0 digits from smallestUnit)");
const result6 = instant.toString({ fractionalSecondDigits: 0, roundingMode: "floor" });
assert.sameValue(result6, "2001-09-09T01:46:40Z",
"roundingMode is floor (with 0 digits from fractionalSecondDigits)");
const result7 = instant.toString({ smallestUnit: "minute", roundingMode: "floor" });
assert.sameValue(result7, "2001-09-09T01:46Z", "roundingMode is floor (round to minute)");

View File

@ -0,0 +1,37 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.instant.prototype.tostring
description: halfExpand value for roundingMode option
features: [Temporal]
---*/
const instant = new Temporal.Instant(1_000_000_000_123_987_500n);
const result1 = instant.toString({ smallestUnit: "microsecond", roundingMode: "halfExpand" });
assert.sameValue(result1, "2001-09-09T01:46:40.123988Z",
"roundingMode is halfExpand (with 6 digits from smallestUnit)");
const result2 = instant.toString({ fractionalSecondDigits: 6, roundingMode: "halfExpand" });
assert.sameValue(result2, "2001-09-09T01:46:40.123988Z",
"roundingMode is halfExpand (with 6 digits from fractionalSecondDigits)");
const result3 = instant.toString({ smallestUnit: "millisecond", roundingMode: "halfExpand" });
assert.sameValue(result3, "2001-09-09T01:46:40.124Z",
"roundingMode is halfExpand (with 3 digits from smallestUnit)");
const result4 = instant.toString({ fractionalSecondDigits: 3, roundingMode: "halfExpand" });
assert.sameValue(result4, "2001-09-09T01:46:40.124Z",
"roundingMode is halfExpand (with 3 digits from fractionalSecondDigits)");
const result5 = instant.toString({ smallestUnit: "second", roundingMode: "halfExpand" });
assert.sameValue(result5, "2001-09-09T01:46:40Z",
"roundingMode is halfExpand (with 0 digits from smallestUnit)");
const result6 = instant.toString({ fractionalSecondDigits: 0, roundingMode: "halfExpand" });
assert.sameValue(result6, "2001-09-09T01:46:40Z",
"roundingMode is halfExpand (with 0 digits from fractionalSecondDigits)");
const result7 = instant.toString({ smallestUnit: "minute", roundingMode: "halfExpand" });
assert.sameValue(result7, "2001-09-09T01:47Z", "roundingMode is halfExpand (round to minute)");

View File

@ -0,0 +1,37 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.instant.prototype.tostring
description: trunc value for roundingMode option
features: [Temporal]
---*/
const instant = new Temporal.Instant(1_000_000_000_123_987_500n);
const result1 = instant.toString({ smallestUnit: "microsecond", roundingMode: "trunc" });
assert.sameValue(result1, "2001-09-09T01:46:40.123987Z",
"roundingMode is trunc (with 6 digits from smallestUnit)");
const result2 = instant.toString({ fractionalSecondDigits: 6, roundingMode: "trunc" });
assert.sameValue(result2, "2001-09-09T01:46:40.123987Z",
"roundingMode is trunc (with 6 digits from fractionalSecondDigits)");
const result3 = instant.toString({ smallestUnit: "millisecond", roundingMode: "trunc" });
assert.sameValue(result3, "2001-09-09T01:46:40.123Z",
"roundingMode is trunc (with 3 digits from smallestUnit)");
const result4 = instant.toString({ fractionalSecondDigits: 3, roundingMode: "trunc" });
assert.sameValue(result4, "2001-09-09T01:46:40.123Z",
"roundingMode is trunc (with 3 digits from fractionalSecondDigits)");
const result5 = instant.toString({ smallestUnit: "second", roundingMode: "trunc" });
assert.sameValue(result5, "2001-09-09T01:46:40Z",
"roundingMode is trunc (with 0 digits from smallestUnit)");
const result6 = instant.toString({ fractionalSecondDigits: 0, roundingMode: "trunc" });
assert.sameValue(result6, "2001-09-09T01:46:40Z",
"roundingMode is trunc (with 0 digits from fractionalSecondDigits)");
const result7 = instant.toString({ smallestUnit: "minute", roundingMode: "trunc" });
assert.sameValue(result7, "2001-09-09T01:46Z", "roundingMode is trunc (round to minute)");

View File

@ -0,0 +1,30 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.instant.prototype.tostring
description: fractionalSecondDigits option is not used with smallestUnit present
features: [Temporal]
---*/
const instant = new Temporal.Instant(56_789_999_999n);
const tests = [
["minute", "1970-01-01T00:00Z"],
["second", "1970-01-01T00:00:56Z"],
["millisecond", "1970-01-01T00:00:56.789Z"],
["microsecond", "1970-01-01T00:00:56.789999Z"],
["nanosecond", "1970-01-01T00:00:56.789999999Z"],
];
for (const [smallestUnit, expected] of tests) {
const string = instant.toString({
smallestUnit,
get fractionalSecondDigits() { throw new Test262Error("should not get fractionalSecondDigits") }
});
assert.sameValue(string, expected, `smallestUnit: "${smallestUnit}" overrides fractionalSecondDigits`);
}
assert.throws(RangeError, () => instant.toString({
smallestUnit: "hour",
get fractionalSecondDigits() { throw new Test262Error("should not get fractionalSecondDigits") }
}), "hour is an invalid smallestUnit but still overrides fractionalSecondDigits");

View File

@ -0,0 +1,23 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.plaindatetime.prototype.tostring
description: auto value for fractionalSecondDigits option
features: [Temporal]
---*/
const zeroSeconds = new Temporal.PlainDateTime(1976, 11, 18, 15, 23);
const wholeSeconds = new Temporal.PlainDateTime(1976, 11, 18, 15, 23, 30);
const subSeconds = new Temporal.PlainDateTime(1976, 11, 18, 15, 23, 30, 123, 400);
const tests = [
[zeroSeconds, "1976-11-18T15:23:00"],
[wholeSeconds, "1976-11-18T15:23:30"],
[subSeconds, "1976-11-18T15:23:30.1234"],
];
for (const [datetime, expected] of tests) {
assert.sameValue(datetime.toString(), expected, "default is to emit seconds and drop trailing zeroes");
assert.sameValue(datetime.toString({ fractionalSecondDigits: "auto" }), expected, "auto is the default");
}

View File

@ -0,0 +1,33 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.plaindatetime.prototype.tostring
description: Number for fractionalSecondDigits option
features: [Temporal]
---*/
const zeroSeconds = new Temporal.PlainDateTime(1976, 11, 18, 15, 23);
const wholeSeconds = new Temporal.PlainDateTime(1976, 11, 18, 15, 23, 30);
const subSeconds = new Temporal.PlainDateTime(1976, 11, 18, 15, 23, 30, 123, 400);
assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 0 }), "1976-11-18T15:23:30",
"truncates 4 decimal places to 0");
assert.sameValue(zeroSeconds.toString({ fractionalSecondDigits: 2 }), "1976-11-18T15:23:00.00",
"pads zero seconds to 2 decimal places");
assert.sameValue(wholeSeconds.toString({ fractionalSecondDigits: 2 }), "1976-11-18T15:23:30.00",
"pads whole seconds to 2 decimal places");
assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 2 }), "1976-11-18T15:23:30.12",
"truncates 4 decimal places to 2");
assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 3 }), "1976-11-18T15:23:30.123",
"truncates 4 decimal places to 3");
assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 6 }), "1976-11-18T15:23:30.123400",
"pads 4 decimal places to 6");
assert.sameValue(zeroSeconds.toString({ fractionalSecondDigits: 7 }), "1976-11-18T15:23:00.0000000",
"pads zero seconds to 7 decimal places");
assert.sameValue(wholeSeconds.toString({ fractionalSecondDigits: 7 }), "1976-11-18T15:23:30.0000000",
"pads whole seconds to 7 decimal places");
assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 7 }), "1976-11-18T15:23:30.1234000",
"pads 4 decimal places to 7");
assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 9 }), "1976-11-18T15:23:30.123400000",
"pads 4 decimal places to 9");

View File

@ -0,0 +1,13 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.plaindatetime.prototype.tostring
description: Rounding can cross midnight
features: [Temporal]
---*/
const plainDateTime = new Temporal.PlainDateTime(1999, 12, 31, 23, 59, 59, 999, 999, 999); // one nanosecond before 2000-01-01T00:00:00
for (const roundingMode of ["ceil", "halfExpand"]) {
assert.sameValue(plainDateTime.toString({ fractionalSecondDigits: 8, roundingMode }), "2000-01-01T00:00:00.00000000");
}

View File

@ -0,0 +1,37 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.plaindatetime.prototype.tostring
description: ceil value for roundingMode option
features: [Temporal]
---*/
const datetime = new Temporal.PlainDateTime(2000, 5, 2, 12, 34, 56, 123, 987, 500);
const result1 = datetime.toString({ smallestUnit: "microsecond", roundingMode: "ceil" });
assert.sameValue(result1, "2000-05-02T12:34:56.123988",
"roundingMode is ceil (with 6 digits from smallestUnit)");
const result2 = datetime.toString({ fractionalSecondDigits: 6, roundingMode: "ceil" });
assert.sameValue(result2, "2000-05-02T12:34:56.123988",
"roundingMode is ceil (with 6 digits from fractionalSecondDigits)");
const result3 = datetime.toString({ smallestUnit: "millisecond", roundingMode: "ceil" });
assert.sameValue(result3, "2000-05-02T12:34:56.124",
"roundingMode is ceil (with 3 digits from smallestUnit)");
const result4 = datetime.toString({ fractionalSecondDigits: 3, roundingMode: "ceil" });
assert.sameValue(result4, "2000-05-02T12:34:56.124",
"roundingMode is ceil (with 3 digits from fractionalSecondDigits)");
const result5 = datetime.toString({ smallestUnit: "second", roundingMode: "ceil" });
assert.sameValue(result5, "2000-05-02T12:34:57",
"roundingMode is ceil (with 0 digits from smallestUnit)");
const result6 = datetime.toString({ fractionalSecondDigits: 0, roundingMode: "ceil" });
assert.sameValue(result6, "2000-05-02T12:34:57",
"roundingMode is ceil (with 0 digits from fractionalSecondDigits)");
const result7 = datetime.toString({ smallestUnit: "minute", roundingMode: "ceil" });
assert.sameValue(result7, "2000-05-02T12:35", "roundingMode is ceil (round to minute)");

View File

@ -0,0 +1,37 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.plaindatetime.prototype.tostring
description: floor value for roundingMode option
features: [Temporal]
---*/
const datetime = new Temporal.PlainDateTime(2000, 5, 2, 12, 34, 56, 123, 987, 500);
const result1 = datetime.toString({ smallestUnit: "microsecond", roundingMode: "floor" });
assert.sameValue(result1, "2000-05-02T12:34:56.123987",
"roundingMode is floor (with 6 digits from smallestUnit)");
const result2 = datetime.toString({ fractionalSecondDigits: 6, roundingMode: "floor" });
assert.sameValue(result2, "2000-05-02T12:34:56.123987",
"roundingMode is floor (with 6 digits from fractionalSecondDigits)");
const result3 = datetime.toString({ smallestUnit: "millisecond", roundingMode: "floor" });
assert.sameValue(result3, "2000-05-02T12:34:56.123",
"roundingMode is floor (with 3 digits from smallestUnit)");
const result4 = datetime.toString({ fractionalSecondDigits: 3, roundingMode: "floor" });
assert.sameValue(result4, "2000-05-02T12:34:56.123",
"roundingMode is floor (with 3 digits from fractionalSecondDigits)");
const result5 = datetime.toString({ smallestUnit: "second", roundingMode: "floor" });
assert.sameValue(result5, "2000-05-02T12:34:56",
"roundingMode is floor (with 0 digits from smallestUnit)");
const result6 = datetime.toString({ fractionalSecondDigits: 0, roundingMode: "floor" });
assert.sameValue(result6, "2000-05-02T12:34:56",
"roundingMode is floor (with 0 digits from fractionalSecondDigits)");
const result7 = datetime.toString({ smallestUnit: "minute", roundingMode: "floor" });
assert.sameValue(result7, "2000-05-02T12:34", "roundingMode is floor (round to minute)");

View File

@ -0,0 +1,37 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.plaindatetime.prototype.tostring
description: halfExpand value for roundingMode option
features: [Temporal]
---*/
const datetime = new Temporal.PlainDateTime(2000, 5, 2, 12, 34, 56, 123, 987, 500);
const result1 = datetime.toString({ smallestUnit: "microsecond", roundingMode: "halfExpand" });
assert.sameValue(result1, "2000-05-02T12:34:56.123988",
"roundingMode is halfExpand (with 6 digits from smallestUnit)");
const result2 = datetime.toString({ fractionalSecondDigits: 6, roundingMode: "halfExpand" });
assert.sameValue(result2, "2000-05-02T12:34:56.123988",
"roundingMode is halfExpand (with 6 digits from fractionalSecondDigits)");
const result3 = datetime.toString({ smallestUnit: "millisecond", roundingMode: "halfExpand" });
assert.sameValue(result3, "2000-05-02T12:34:56.124",
"roundingMode is halfExpand (with 3 digits from smallestUnit)");
const result4 = datetime.toString({ fractionalSecondDigits: 3, roundingMode: "halfExpand" });
assert.sameValue(result4, "2000-05-02T12:34:56.124",
"roundingMode is halfExpand (with 3 digits from fractionalSecondDigits)");
const result5 = datetime.toString({ smallestUnit: "second", roundingMode: "halfExpand" });
assert.sameValue(result5, "2000-05-02T12:34:56",
"roundingMode is halfExpand (with 0 digits from smallestUnit)");
const result6 = datetime.toString({ fractionalSecondDigits: 0, roundingMode: "halfExpand" });
assert.sameValue(result6, "2000-05-02T12:34:56",
"roundingMode is halfExpand (with 0 digits from fractionalSecondDigits)");
const result7 = datetime.toString({ smallestUnit: "minute", roundingMode: "halfExpand" });
assert.sameValue(result7, "2000-05-02T12:35", "roundingMode is halfExpand (round to minute)");

View File

@ -0,0 +1,37 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.plaindatetime.prototype.tostring
description: trunc value for roundingMode option
features: [Temporal]
---*/
const datetime = new Temporal.PlainDateTime(2000, 5, 2, 12, 34, 56, 123, 987, 500);
const result1 = datetime.toString({ smallestUnit: "microsecond", roundingMode: "trunc" });
assert.sameValue(result1, "2000-05-02T12:34:56.123987",
"roundingMode is trunc (with 6 digits from smallestUnit)");
const result2 = datetime.toString({ fractionalSecondDigits: 6, roundingMode: "trunc" });
assert.sameValue(result2, "2000-05-02T12:34:56.123987",
"roundingMode is trunc (with 6 digits from fractionalSecondDigits)");
const result3 = datetime.toString({ smallestUnit: "millisecond", roundingMode: "trunc" });
assert.sameValue(result3, "2000-05-02T12:34:56.123",
"roundingMode is trunc (with 3 digits from smallestUnit)");
const result4 = datetime.toString({ fractionalSecondDigits: 3, roundingMode: "trunc" });
assert.sameValue(result4, "2000-05-02T12:34:56.123",
"roundingMode is trunc (with 3 digits from fractionalSecondDigits)");
const result5 = datetime.toString({ smallestUnit: "second", roundingMode: "trunc" });
assert.sameValue(result5, "2000-05-02T12:34:56",
"roundingMode is trunc (with 0 digits from smallestUnit)");
const result6 = datetime.toString({ fractionalSecondDigits: 0, roundingMode: "trunc" });
assert.sameValue(result6, "2000-05-02T12:34:56",
"roundingMode is trunc (with 0 digits from fractionalSecondDigits)");
const result7 = datetime.toString({ smallestUnit: "minute", roundingMode: "trunc" });
assert.sameValue(result7, "2000-05-02T12:34", "roundingMode is trunc (round to minute)");

View File

@ -0,0 +1,30 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.plaindatetime.prototype.tostring
description: fractionalSecondDigits option is not used with smallestUnit present
features: [Temporal]
---*/
const datetime = new Temporal.PlainDateTime(1976, 11, 18, 12, 34, 56, 789, 999, 999);
const tests = [
["minute", "1976-11-18T12:34"],
["second", "1976-11-18T12:34:56"],
["millisecond", "1976-11-18T12:34:56.789"],
["microsecond", "1976-11-18T12:34:56.789999"],
["nanosecond", "1976-11-18T12:34:56.789999999"],
];
for (const [smallestUnit, expected] of tests) {
const string = datetime.toString({
smallestUnit,
get fractionalSecondDigits() { throw new Test262Error("should not get fractionalSecondDigits") }
});
assert.sameValue(string, expected, `smallestUnit: "${smallestUnit}" overrides fractionalSecondDigits`);
}
assert.throws(RangeError, () => datetime.toString({
smallestUnit: "hour",
get fractionalSecondDigits() { throw new Test262Error("should not get fractionalSecondDigits") }
}), "hour is an invalid smallestUnit but still overrides fractionalSecondDigits");

View File

@ -7,13 +7,17 @@ description: auto value for fractionalSecondDigits option
features: [Temporal]
---*/
const zeroSeconds = new Temporal.PlainTime(15, 23);
const wholeSeconds = new Temporal.PlainTime(15, 23, 30);
const subSeconds = new Temporal.PlainTime(15, 23, 30, 123, 400);
const tests = [
["15:23", "15:23:00"],
["15:23:30", "15:23:30"],
["15:23:30.1234", "15:23:30.1234"],
[zeroSeconds, "15:23:00"],
[wholeSeconds, "15:23:30"],
[subSeconds, "15:23:30.1234"],
];
for (const [input, expected] of tests) {
const plainTime = Temporal.PlainTime.from(input);
assert.sameValue(plainTime.toString({ fractionalSecondDigits: "auto" }), expected);
for (const [time, expected] of tests) {
assert.sameValue(time.toString(), expected, "default is to emit seconds and drop trailing zeroes");
assert.sameValue(time.toString({ fractionalSecondDigits: "auto" }), expected, "auto is the default");
}

View File

@ -7,16 +7,27 @@ description: Number for fractionalSecondDigits option
features: [Temporal]
---*/
const t1 = Temporal.PlainTime.from("15:23");
const t2 = Temporal.PlainTime.from("15:23:30");
const t3 = Temporal.PlainTime.from("15:23:30.1234");
assert.sameValue(t3.toString({ fractionalSecondDigits: 0 }), "15:23:30");
assert.sameValue(t1.toString({ fractionalSecondDigits: 2 }), "15:23:00.00");
assert.sameValue(t2.toString({ fractionalSecondDigits: 2 }), "15:23:30.00");
assert.sameValue(t3.toString({ fractionalSecondDigits: 2 }), "15:23:30.12");
assert.sameValue(t3.toString({ fractionalSecondDigits: 3 }), "15:23:30.123");
assert.sameValue(t3.toString({ fractionalSecondDigits: 6 }), "15:23:30.123400");
assert.sameValue(t1.toString({ fractionalSecondDigits: 7 }), "15:23:00.0000000");
assert.sameValue(t2.toString({ fractionalSecondDigits: 7 }), "15:23:30.0000000");
assert.sameValue(t3.toString({ fractionalSecondDigits: 7 }), "15:23:30.1234000");
assert.sameValue(t3.toString({ fractionalSecondDigits: 9 }), "15:23:30.123400000");
const zeroSeconds = new Temporal.PlainTime(15, 23);
const wholeSeconds = new Temporal.PlainTime(15, 23, 30);
const subSeconds = new Temporal.PlainTime(15, 23, 30, 123, 400);
assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 0 }), "15:23:30",
"truncates 4 decimal places to 0");
assert.sameValue(zeroSeconds.toString({ fractionalSecondDigits: 2 }), "15:23:00.00",
"pads zero seconds to 2 decimal places");
assert.sameValue(wholeSeconds.toString({ fractionalSecondDigits: 2 }), "15:23:30.00",
"pads whole seconds to 2 decimal places");
assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 2 }), "15:23:30.12",
"truncates 4 decimal places to 2");
assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 3 }), "15:23:30.123",
"truncates 4 decimal places to 3");
assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 6 }), "15:23:30.123400",
"pads 4 decimal places to 6");
assert.sameValue(zeroSeconds.toString({ fractionalSecondDigits: 7 }), "15:23:00.0000000",
"pads zero seconds to 7 decimal places");
assert.sameValue(wholeSeconds.toString({ fractionalSecondDigits: 7 }), "15:23:30.0000000",
"pads whole seconds to 7 decimal places");
assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 7 }), "15:23:30.1234000",
"pads 4 decimal places to 7");
assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 9 }), "15:23:30.123400000",
"pads 4 decimal places to 9");

View File

@ -7,7 +7,7 @@ description: Rounding can cross midnight
features: [Temporal]
---*/
const plainTime = Temporal.PlainTime.from("23:59:59.999999999");
const plainTime = new Temporal.PlainTime(23, 59, 59, 999, 999, 999); // one nanosecond before 00:00:00
for (const roundingMode of ["ceil", "halfExpand"]) {
assert.sameValue(plainTime.toString({ fractionalSecondDigits: 8, roundingMode }), "00:00:00.00000000");
}

View File

@ -10,10 +10,28 @@ features: [Temporal]
const time = new Temporal.PlainTime(12, 34, 56, 123, 987, 500);
const result1 = time.toString({ smallestUnit: "microsecond", roundingMode: "ceil" });
assert.sameValue(result1, "12:34:56.123988", "roundingMode is ceil");
assert.sameValue(result1, "12:34:56.123988",
"roundingMode is ceil (with 6 digits from smallestUnit)");
const result2 = time.toString({ smallestUnit: "millisecond", roundingMode: "ceil" });
assert.sameValue(result2, "12:34:56.124", "roundingMode is ceil");
const result2 = time.toString({ fractionalSecondDigits: 6, roundingMode: "ceil" });
assert.sameValue(result2, "12:34:56.123988",
"roundingMode is ceil (with 6 digits from fractionalSecondDigits)");
const result3 = time.toString({ smallestUnit: "second", roundingMode: "ceil" });
assert.sameValue(result3, "12:34:57", "roundingMode is ceil");
const result3 = time.toString({ smallestUnit: "millisecond", roundingMode: "ceil" });
assert.sameValue(result3, "12:34:56.124",
"roundingMode is ceil (with 3 digits from smallestUnit)");
const result4 = time.toString({ fractionalSecondDigits: 3, roundingMode: "ceil" });
assert.sameValue(result4, "12:34:56.124",
"roundingMode is ceil (with 3 digits from fractionalSecondDigits)");
const result5 = time.toString({ smallestUnit: "second", roundingMode: "ceil" });
assert.sameValue(result5, "12:34:57",
"roundingMode is ceil (with 0 digits from smallestUnit)");
const result6 = time.toString({ fractionalSecondDigits: 0, roundingMode: "ceil" });
assert.sameValue(result6, "12:34:57",
"roundingMode is ceil (with 0 digits from fractionalSecondDigits)");
const result7 = time.toString({ smallestUnit: "minute", roundingMode: "ceil" });
assert.sameValue(result7, "12:35", "roundingMode is ceil (round to minute)");

View File

@ -10,10 +10,28 @@ features: [Temporal]
const time = new Temporal.PlainTime(12, 34, 56, 123, 987, 500);
const result1 = time.toString({ smallestUnit: "microsecond", roundingMode: "floor" });
assert.sameValue(result1, "12:34:56.123987", "roundingMode is floor");
assert.sameValue(result1, "12:34:56.123987",
"roundingMode is floor (with 6 digits from smallestUnit)");
const result2 = time.toString({ smallestUnit: "millisecond", roundingMode: "floor" });
assert.sameValue(result2, "12:34:56.123", "roundingMode is floor");
const result2 = time.toString({ fractionalSecondDigits: 6, roundingMode: "floor" });
assert.sameValue(result2, "12:34:56.123987",
"roundingMode is floor (with 6 digits from fractionalSecondDigits)");
const result3 = time.toString({ smallestUnit: "second", roundingMode: "floor" });
assert.sameValue(result3, "12:34:56", "roundingMode is floor");
const result3 = time.toString({ smallestUnit: "millisecond", roundingMode: "floor" });
assert.sameValue(result3, "12:34:56.123",
"roundingMode is floor (with 3 digits from smallestUnit)");
const result4 = time.toString({ fractionalSecondDigits: 3, roundingMode: "floor" });
assert.sameValue(result4, "12:34:56.123",
"roundingMode is floor (with 3 digits from fractionalSecondDigits)");
const result5 = time.toString({ smallestUnit: "second", roundingMode: "floor" });
assert.sameValue(result5, "12:34:56",
"roundingMode is floor (with 0 digits from smallestUnit)");
const result6 = time.toString({ fractionalSecondDigits: 0, roundingMode: "floor" });
assert.sameValue(result6, "12:34:56",
"roundingMode is floor (with 0 digits from fractionalSecondDigits)");
const result7 = time.toString({ smallestUnit: "minute", roundingMode: "floor" });
assert.sameValue(result7, "12:34", "roundingMode is floor (round to minute)");

View File

@ -10,10 +10,28 @@ features: [Temporal]
const time = new Temporal.PlainTime(12, 34, 56, 123, 987, 500);
const result1 = time.toString({ smallestUnit: "microsecond", roundingMode: "halfExpand" });
assert.sameValue(result1, "12:34:56.123988", "roundingMode is halfExpand");
assert.sameValue(result1, "12:34:56.123988",
"roundingMode is halfExpand (with 6 digits from smallestUnit)");
const result2 = time.toString({ smallestUnit: "millisecond", roundingMode: "halfExpand" });
assert.sameValue(result2, "12:34:56.124", "roundingMode is halfExpand");
const result2 = time.toString({ fractionalSecondDigits: 6, roundingMode: "halfExpand" });
assert.sameValue(result2, "12:34:56.123988",
"roundingMode is halfExpand (with 6 digits from fractionalSecondDigits)");
const result3 = time.toString({ smallestUnit: "second", roundingMode: "halfExpand" });
assert.sameValue(result3, "12:34:56", "roundingMode is halfExpand");
const result3 = time.toString({ smallestUnit: "millisecond", roundingMode: "halfExpand" });
assert.sameValue(result3, "12:34:56.124",
"roundingMode is halfExpand (with 3 digits from smallestUnit)");
const result4 = time.toString({ fractionalSecondDigits: 3, roundingMode: "halfExpand" });
assert.sameValue(result4, "12:34:56.124",
"roundingMode is halfExpand (with 3 digits from fractionalSecondDigits)");
const result5 = time.toString({ smallestUnit: "second", roundingMode: "halfExpand" });
assert.sameValue(result5, "12:34:56",
"roundingMode is halfExpand (with 0 digits from smallestUnit)");
const result6 = time.toString({ fractionalSecondDigits: 0, roundingMode: "halfExpand" });
assert.sameValue(result6, "12:34:56",
"roundingMode is halfExpand (with 0 digits from fractionalSecondDigits)");
const result7 = time.toString({ smallestUnit: "minute", roundingMode: "halfExpand" });
assert.sameValue(result7, "12:35", "roundingMode is halfExpand (round to minute)");

View File

@ -10,10 +10,28 @@ features: [Temporal]
const time = new Temporal.PlainTime(12, 34, 56, 123, 987, 500);
const result1 = time.toString({ smallestUnit: "microsecond", roundingMode: "trunc" });
assert.sameValue(result1, "12:34:56.123987", "roundingMode is trunc");
assert.sameValue(result1, "12:34:56.123987",
"roundingMode is trunc (with 6 digits from smallestUnit)");
const result2 = time.toString({ smallestUnit: "millisecond", roundingMode: "trunc" });
assert.sameValue(result2, "12:34:56.123", "roundingMode is trunc");
const result2 = time.toString({ fractionalSecondDigits: 6, roundingMode: "trunc" });
assert.sameValue(result2, "12:34:56.123987",
"roundingMode is trunc (with 6 digits from fractionalSecondDigits)");
const result3 = time.toString({ smallestUnit: "second", roundingMode: "trunc" });
assert.sameValue(result3, "12:34:56", "roundingMode is trunc");
const result3 = time.toString({ smallestUnit: "millisecond", roundingMode: "trunc" });
assert.sameValue(result3, "12:34:56.123",
"roundingMode is trunc (with 3 digits from smallestUnit)");
const result4 = time.toString({ fractionalSecondDigits: 3, roundingMode: "trunc" });
assert.sameValue(result4, "12:34:56.123",
"roundingMode is trunc (with 3 digits from fractionalSecondDigits)");
const result5 = time.toString({ smallestUnit: "second", roundingMode: "trunc" });
assert.sameValue(result5, "12:34:56",
"roundingMode is trunc (with 0 digits from smallestUnit)");
const result6 = time.toString({ fractionalSecondDigits: 0, roundingMode: "trunc" });
assert.sameValue(result6, "12:34:56",
"roundingMode is trunc (with 0 digits from fractionalSecondDigits)");
const result7 = time.toString({ smallestUnit: "minute", roundingMode: "trunc" });
assert.sameValue(result7, "12:34", "roundingMode is trunc (round to minute)");

View File

@ -21,11 +21,10 @@ for (const [smallestUnit, expected] of tests) {
smallestUnit,
get fractionalSecondDigits() { throw new Test262Error("should not get fractionalSecondDigits") }
});
assert.sameValue(string, expected, smallestUnit);
assert.sameValue(string, expected, `smallestUnit: "${smallestUnit}" overrides fractionalSecondDigits`);
}
assert.throws(RangeError, () => time.toString({
smallestUnit: "hour",
get fractionalSecondDigits() { throw new Test262Error("should not get fractionalSecondDigits") }
}));
}), "hour is an invalid smallestUnit but still overrides fractionalSecondDigits");

View File

@ -0,0 +1,23 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.zoneddatetime.prototype.tostring
description: auto value for fractionalSecondDigits option
features: [BigInt, Temporal]
---*/
const zeroSeconds = new Temporal.ZonedDateTime(0n, "UTC");
const wholeSeconds = new Temporal.ZonedDateTime(30_000_000_000n, "UTC");
const subSeconds = new Temporal.ZonedDateTime(30_123_400_000n, "UTC");
const tests = [
[zeroSeconds, "1970-01-01T00:00:00+00:00[UTC]"],
[wholeSeconds, "1970-01-01T00:00:30+00:00[UTC]"],
[subSeconds, "1970-01-01T00:00:30.1234+00:00[UTC]"],
];
for (const [datetime, expected] of tests) {
assert.sameValue(datetime.toString(), expected, "default is to emit seconds and drop trailing zeroes");
assert.sameValue(datetime.toString({ fractionalSecondDigits: "auto" }), expected, "auto is the default");
}

View File

@ -0,0 +1,33 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.zoneddatetime.prototype.tostring
description: Number for fractionalSecondDigits option
features: [BigInt, Temporal]
---*/
const zeroSeconds = new Temporal.ZonedDateTime(0n, "UTC");
const wholeSeconds = new Temporal.ZonedDateTime(30_000_000_000n, "UTC");
const subSeconds = new Temporal.ZonedDateTime(30_123_400_000n, "UTC");
assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 0 }), "1970-01-01T00:00:30+00:00[UTC]",
"truncates 4 decimal places to 0");
assert.sameValue(zeroSeconds.toString({ fractionalSecondDigits: 2 }), "1970-01-01T00:00:00.00+00:00[UTC]",
"pads zero seconds to 2 decimal places");
assert.sameValue(wholeSeconds.toString({ fractionalSecondDigits: 2 }), "1970-01-01T00:00:30.00+00:00[UTC]",
"pads whole seconds to 2 decimal places");
assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 2 }), "1970-01-01T00:00:30.12+00:00[UTC]",
"truncates 4 decimal places to 2");
assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 3 }), "1970-01-01T00:00:30.123+00:00[UTC]",
"truncates 4 decimal places to 3");
assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 6 }), "1970-01-01T00:00:30.123400+00:00[UTC]",
"pads 4 decimal places to 6");
assert.sameValue(zeroSeconds.toString({ fractionalSecondDigits: 7 }), "1970-01-01T00:00:00.0000000+00:00[UTC]",
"pads zero seconds to 7 decimal places");
assert.sameValue(wholeSeconds.toString({ fractionalSecondDigits: 7 }), "1970-01-01T00:00:30.0000000+00:00[UTC]",
"pads whole seconds to 7 decimal places");
assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 7 }), "1970-01-01T00:00:30.1234000+00:00[UTC]",
"pads 4 decimal places to 7");
assert.sameValue(subSeconds.toString({ fractionalSecondDigits: 9 }), "1970-01-01T00:00:30.123400000+00:00[UTC]",
"pads 4 decimal places to 9");

View File

@ -0,0 +1,13 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.zoneddatetime.prototype.tostring
description: Rounding can cross midnight
features: [Temporal]
---*/
const zonedDateTime = new Temporal.ZonedDateTime(946_684_799_999_999_999n, "UTC"); // one nanosecond before 2000-01-01T00:00:00
for (const roundingMode of ["ceil", "halfExpand"]) {
assert.sameValue(zonedDateTime.toString({ fractionalSecondDigits: 8, roundingMode }), "2000-01-01T00:00:00.00000000+00:00[UTC]");
}

View File

@ -0,0 +1,37 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.zoneddatetime.prototype.tostring
description: ceil value for roundingMode option
features: [Temporal]
---*/
const datetime = new Temporal.ZonedDateTime(1_000_000_000_123_987_500n, "UTC");
const result1 = datetime.toString({ smallestUnit: "microsecond", roundingMode: "ceil" });
assert.sameValue(result1, "2001-09-09T01:46:40.123988+00:00[UTC]",
"roundingMode is ceil (with 6 digits from smallestUnit)");
const result2 = datetime.toString({ fractionalSecondDigits: 6, roundingMode: "ceil" });
assert.sameValue(result2, "2001-09-09T01:46:40.123988+00:00[UTC]",
"roundingMode is ceil (with 6 digits from fractionalSecondDigits)");
const result3 = datetime.toString({ smallestUnit: "millisecond", roundingMode: "ceil" });
assert.sameValue(result3, "2001-09-09T01:46:40.124+00:00[UTC]",
"roundingMode is ceil (with 3 digits from smallestUnit)");
const result4 = datetime.toString({ fractionalSecondDigits: 3, roundingMode: "ceil" });
assert.sameValue(result4, "2001-09-09T01:46:40.124+00:00[UTC]",
"roundingMode is ceil (with 3 digits from fractionalSecondDigits)");
const result5 = datetime.toString({ smallestUnit: "second", roundingMode: "ceil" });
assert.sameValue(result5, "2001-09-09T01:46:41+00:00[UTC]",
"roundingMode is ceil (with 0 digits from smallestUnit)");
const result6 = datetime.toString({ fractionalSecondDigits: 0, roundingMode: "ceil" });
assert.sameValue(result6, "2001-09-09T01:46:41+00:00[UTC]",
"roundingMode is ceil (with 0 digits from fractionalSecondDigits)");
const result7 = datetime.toString({ smallestUnit: "minute", roundingMode: "ceil" });
assert.sameValue(result7, "2001-09-09T01:47+00:00[UTC]", "roundingMode is ceil (round to minute)");

View File

@ -0,0 +1,37 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.zoneddatetime.prototype.tostring
description: floor value for roundingMode option
features: [Temporal]
---*/
const datetime = new Temporal.ZonedDateTime(1_000_000_000_123_987_500n, "UTC");
const result1 = datetime.toString({ smallestUnit: "microsecond", roundingMode: "floor" });
assert.sameValue(result1, "2001-09-09T01:46:40.123987+00:00[UTC]",
"roundingMode is floor (with 6 digits from smallestUnit)");
const result2 = datetime.toString({ fractionalSecondDigits: 6, roundingMode: "floor" });
assert.sameValue(result2, "2001-09-09T01:46:40.123987+00:00[UTC]",
"roundingMode is floor (with 6 digits from fractionalSecondDigits)");
const result3 = datetime.toString({ smallestUnit: "millisecond", roundingMode: "floor" });
assert.sameValue(result3, "2001-09-09T01:46:40.123+00:00[UTC]",
"roundingMode is floor (with 3 digits from smallestUnit)");
const result4 = datetime.toString({ fractionalSecondDigits: 3, roundingMode: "floor" });
assert.sameValue(result4, "2001-09-09T01:46:40.123+00:00[UTC]",
"roundingMode is floor (with 3 digits from fractionalSecondDigits)");
const result5 = datetime.toString({ smallestUnit: "second", roundingMode: "floor" });
assert.sameValue(result5, "2001-09-09T01:46:40+00:00[UTC]",
"roundingMode is floor (with 0 digits from smallestUnit)");
const result6 = datetime.toString({ fractionalSecondDigits: 0, roundingMode: "floor" });
assert.sameValue(result6, "2001-09-09T01:46:40+00:00[UTC]",
"roundingMode is floor (with 0 digits from fractionalSecondDigits)");
const result7 = datetime.toString({ smallestUnit: "minute", roundingMode: "floor" });
assert.sameValue(result7, "2001-09-09T01:46+00:00[UTC]", "roundingMode is floor (round to minute)");

View File

@ -0,0 +1,37 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.zoneddatetime.prototype.tostring
description: halfExpand value for roundingMode option
features: [Temporal]
---*/
const datetime = new Temporal.ZonedDateTime(1_000_000_000_123_987_500n, "UTC");
const result1 = datetime.toString({ smallestUnit: "microsecond", roundingMode: "halfExpand" });
assert.sameValue(result1, "2001-09-09T01:46:40.123988+00:00[UTC]",
"roundingMode is halfExpand (with 6 digits from smallestUnit)");
const result2 = datetime.toString({ fractionalSecondDigits: 6, roundingMode: "halfExpand" });
assert.sameValue(result2, "2001-09-09T01:46:40.123988+00:00[UTC]",
"roundingMode is halfExpand (with 6 digits from fractionalSecondDigits)");
const result3 = datetime.toString({ smallestUnit: "millisecond", roundingMode: "halfExpand" });
assert.sameValue(result3, "2001-09-09T01:46:40.124+00:00[UTC]",
"roundingMode is halfExpand (with 3 digits from smallestUnit)");
const result4 = datetime.toString({ fractionalSecondDigits: 3, roundingMode: "halfExpand" });
assert.sameValue(result4, "2001-09-09T01:46:40.124+00:00[UTC]",
"roundingMode is halfExpand (with 3 digits from fractionalSecondDigits)");
const result5 = datetime.toString({ smallestUnit: "second", roundingMode: "halfExpand" });
assert.sameValue(result5, "2001-09-09T01:46:40+00:00[UTC]",
"roundingMode is halfExpand (with 0 digits from smallestUnit)");
const result6 = datetime.toString({ fractionalSecondDigits: 0, roundingMode: "halfExpand" });
assert.sameValue(result6, "2001-09-09T01:46:40+00:00[UTC]",
"roundingMode is halfExpand (with 0 digits from fractionalSecondDigits)");
const result7 = datetime.toString({ smallestUnit: "minute", roundingMode: "halfExpand" });
assert.sameValue(result7, "2001-09-09T01:47+00:00[UTC]", "roundingMode is halfExpand (round to minute)");

View File

@ -0,0 +1,37 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.zoneddatetime.prototype.tostring
description: trunc value for roundingMode option
features: [Temporal]
---*/
const datetime = new Temporal.ZonedDateTime(1_000_000_000_123_987_500n, "UTC");
const result1 = datetime.toString({ smallestUnit: "microsecond", roundingMode: "trunc" });
assert.sameValue(result1, "2001-09-09T01:46:40.123987+00:00[UTC]",
"roundingMode is trunc (with 6 digits from smallestUnit)");
const result2 = datetime.toString({ fractionalSecondDigits: 6, roundingMode: "trunc" });
assert.sameValue(result2, "2001-09-09T01:46:40.123987+00:00[UTC]",
"roundingMode is trunc (with 6 digits from fractionalSecondDigits)");
const result3 = datetime.toString({ smallestUnit: "millisecond", roundingMode: "trunc" });
assert.sameValue(result3, "2001-09-09T01:46:40.123+00:00[UTC]",
"roundingMode is trunc (with 3 digits from smallestUnit)");
const result4 = datetime.toString({ fractionalSecondDigits: 3, roundingMode: "trunc" });
assert.sameValue(result4, "2001-09-09T01:46:40.123+00:00[UTC]",
"roundingMode is trunc (with 3 digits from fractionalSecondDigits)");
const result5 = datetime.toString({ smallestUnit: "second", roundingMode: "trunc" });
assert.sameValue(result5, "2001-09-09T01:46:40+00:00[UTC]",
"roundingMode is trunc (with 0 digits from smallestUnit)");
const result6 = datetime.toString({ fractionalSecondDigits: 0, roundingMode: "trunc" });
assert.sameValue(result6, "2001-09-09T01:46:40+00:00[UTC]",
"roundingMode is trunc (with 0 digits from fractionalSecondDigits)");
const result7 = datetime.toString({ smallestUnit: "minute", roundingMode: "trunc" });
assert.sameValue(result7, "2001-09-09T01:46+00:00[UTC]", "roundingMode is trunc (round to minute)");

View File

@ -0,0 +1,30 @@
// Copyright (C) 2022 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.zoneddatetime.prototype.tostring
description: fractionalSecondDigits option is not used with smallestUnit present
features: [Temporal]
---*/
const datetime = new Temporal.ZonedDateTime(56_789_999_999n, "UTC");
const tests = [
["minute", "1970-01-01T00:00+00:00[UTC]"],
["second", "1970-01-01T00:00:56+00:00[UTC]"],
["millisecond", "1970-01-01T00:00:56.789+00:00[UTC]"],
["microsecond", "1970-01-01T00:00:56.789999+00:00[UTC]"],
["nanosecond", "1970-01-01T00:00:56.789999999+00:00[UTC]"],
];
for (const [smallestUnit, expected] of tests) {
const string = datetime.toString({
smallestUnit,
get fractionalSecondDigits() { throw new Test262Error("should not get fractionalSecondDigits") }
});
assert.sameValue(string, expected, `smallestUnit: "${smallestUnit}" overrides fractionalSecondDigits`);
}
assert.throws(RangeError, () => datetime.toString({
smallestUnit: "hour",
get fractionalSecondDigits() { throw new Test262Error("should not get fractionalSecondDigits") }
}), "hour is an invalid smallestUnit but still overrides fractionalSecondDigits");