Skip to content

Commit

Permalink
Add tests for direction of rounding functionality
Browse files Browse the repository at this point in the history
The round() and toString() methods of Temporal.Instant, PlainDateTime, and
ZonedDateTime can round up or down. However, the instance must not be
treated as "negative" even when the time is before 1 BCE (years are
negative) or before the Unix epoch (epoch nanoseconds are negative). That
is, rounding down is always towards the Big Bang, and rounding up is
always away from it. Add tests that verify this.
  • Loading branch information
ptomato authored and catamorphism committed May 30, 2022
1 parent 69f987e commit 61cfb53
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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.round
description: Rounding down is towards the Big Bang, not the epoch or 1 BCE
features: [Temporal]
---*/

const instance = new Temporal.Instant(-65_261_246_399_500_000_000n); // -000099-12-15T12:00:00.5Z
assert.sameValue(
instance.round({ smallestUnit: "second", roundingMode: "floor" }).epochNanoseconds,
-65_261_246_400_000_000_000n, // -000099-12-15T12:00:00Z
"Rounding down is towards the Big Bang, not the epoch or 1 BCE (roundingMode floor)"
);
assert.sameValue(
instance.round({ smallestUnit: "second", roundingMode: "trunc" }).epochNanoseconds,
-65_261_246_400_000_000_000n, // -000099-12-15T12:00:00Z
"Rounding down is towards the Big Bang, not the epoch or 1 BCE (roundingMode trunc)"
);
assert.sameValue(
instance.round({ smallestUnit: "second", roundingMode: "ceil" }).epochNanoseconds,
-65_261_246_399_000_000_000n, // -000099-12-15T12:00:01Z
"Rounding up is away from the Big Bang, not the epoch or 1 BCE (roundingMode ceil)"
);
assert.sameValue(
instance.round({ smallestUnit: "second", roundingMode: "halfExpand" }).epochNanoseconds,
-65_261_246_399_000_000_000n, // -000099-12-15T12:00:01Z
"Rounding up is away from the Big Bang, not the epoch or 1 BCE (roundingMode halfExpand)"
);
Original file line number Diff line number Diff line change
@@ -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: Rounding down is towards the Big Bang, not the epoch or 1 BCE
features: [Temporal]
---*/

const instance = new Temporal.Instant(-65_261_246_399_500_000_000n); // -000099-12-15T12:00:00.5Z
assert.sameValue(
instance.toString({ smallestUnit: "second", roundingMode: "floor" }),
"-000099-12-15T12:00:00Z",
"Rounding down is towards the Big Bang, not the epoch or 1 BCE"
);
assert.sameValue(
instance.toString({ smallestUnit: "second", roundingMode: "trunc" }),
"-000099-12-15T12:00:00Z",
"Rounding down is towards the Big Bang, not the epoch or 1 BCE (roundingMode trunc)"
);
assert.sameValue(
instance.toString({ smallestUnit: "second", roundingMode: "ceil" }),
"-000099-12-15T12:00:01Z",
"Rounding up is away from the Big Bang, not the epoch or 1 BCE (roundingMode ceil)"
);
assert.sameValue(
instance.toString({ smallestUnit: "second", roundingMode: "halfExpand" }),
"-000099-12-15T12:00:01Z",
"Rounding up is away from the Big Bang, not the epoch or 1 BCE (roundingMode halfExpand)"
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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.round
description: Rounding down is towards the Big Bang, not the epoch or 1 BCE
includes: [temporalHelpers.js]
features: [Temporal]
---*/

const instance = new Temporal.PlainDateTime(-99, 12, 15, 12, 0, 0, 500);
TemporalHelpers.assertPlainDateTime(
instance.round({ smallestUnit: "second", roundingMode: "floor" }),
-99, 12, "M12", 15, 12, 0, 0, 0, 0, 0,
"Rounding down is towards the Big Bang, not the epoch or 1 BCE (roundingMode floor)"
);
TemporalHelpers.assertPlainDateTime(
instance.round({ smallestUnit: "second", roundingMode: "trunc" }),
-99, 12, "M12", 15, 12, 0, 0, 0, 0, 0,
"Rounding down is towards the Big Bang, not the epoch or 1 BCE (roundingMode trunc)"
);
TemporalHelpers.assertPlainDateTime(
instance.round({ smallestUnit: "second", roundingMode: "ceil" }),
-99, 12, "M12", 15, 12, 0, 1, 0, 0, 0,
"Rounding up is away from the Big Bang, not the epoch or 1 BCE (roundingMode ceil)"
);
TemporalHelpers.assertPlainDateTime(
instance.round({ smallestUnit: "second", roundingMode: "halfExpand" }),
-99, 12, "M12", 15, 12, 0, 1, 0, 0, 0,
"Rounding up is away from the Big Bang, not the epoch or 1 BCE (roundingMode halfExpand)"
);
Original file line number Diff line number Diff line change
@@ -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: Rounding down is towards the Big Bang, not the epoch or 1 BCE
features: [Temporal]
---*/

const instance = new Temporal.PlainDateTime(-99, 12, 15, 12, 0, 0, 500);
assert.sameValue(
instance.toString({ smallestUnit: "second", roundingMode: "floor" }),
"-000099-12-15T12:00:00",
"Rounding down is towards the Big Bang, not the epoch or 1 BCE"
);
assert.sameValue(
instance.toString({ smallestUnit: "second", roundingMode: "trunc" }),
"-000099-12-15T12:00:00",
"Rounding down is towards the Big Bang, not the epoch or 1 BCE (roundingMode trunc)"
);
assert.sameValue(
instance.toString({ smallestUnit: "second", roundingMode: "ceil" }),
"-000099-12-15T12:00:01",
"Rounding up is away from the Big Bang, not the epoch or 1 BCE (roundingMode ceil)"
);
assert.sameValue(
instance.toString({ smallestUnit: "second", roundingMode: "halfExpand" }),
"-000099-12-15T12:00:01",
"Rounding up is away from the Big Bang, not the epoch or 1 BCE (roundingMode halfExpand)"
);
Original file line number Diff line number Diff line change
@@ -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.round
description: Rounding down is towards the Big Bang, not the epoch or 1 BCE
features: [Temporal]
---*/

const instance = new Temporal.ZonedDateTime(-65_261_246_399_500_000_000n, "UTC"); // -000099-12-15T12:00:00.5Z
assert.sameValue(
instance.round({ smallestUnit: "second", roundingMode: "floor" }).epochNanoseconds,
-65_261_246_400_000_000_000n, // -000099-12-15T12:00:00Z
"Rounding down is towards the Big Bang, not the epoch or 1 BCE (roundingMode floor)"
);
assert.sameValue(
instance.round({ smallestUnit: "second", roundingMode: "trunc" }).epochNanoseconds,
-65_261_246_400_000_000_000n, // -000099-12-15T12:00:00Z
"Rounding down is towards the Big Bang, not the epoch or 1 BCE (roundingMode trunc)"
);
assert.sameValue(
instance.round({ smallestUnit: "second", roundingMode: "ceil" }).epochNanoseconds,
-65_261_246_399_000_000_000n, // -000099-12-15T12:00:01Z
"Rounding up is away from the Big Bang, not the epoch or 1 BCE (roundingMode ceil)"
);
assert.sameValue(
instance.round({ smallestUnit: "second", roundingMode: "halfExpand" }).epochNanoseconds,
-65_261_246_399_000_000_000n, // -000099-12-15T12:00:01Z
"Rounding up is away from the Big Bang, not the epoch or 1 BCE (roundingMode halfExpand)"
);
Original file line number Diff line number Diff line change
@@ -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: Rounding down is towards the Big Bang, not the epoch or 1 BCE
features: [Temporal]
---*/

const instance = new Temporal.ZonedDateTime(-65_261_246_399_500_000_000n, "UTC"); // -000099-12-15T12:00:00.5Z
assert.sameValue(
instance.toString({ smallestUnit: "second", roundingMode: "floor" }),
"-000099-12-15T12:00:00+00:00[UTC]",
"Rounding down is towards the Big Bang, not the epoch or 1 BCE"
);
assert.sameValue(
instance.toString({ smallestUnit: "second", roundingMode: "trunc" }),
"-000099-12-15T12:00:00+00:00[UTC]",
"Rounding down is towards the Big Bang, not the epoch or 1 BCE (roundingMode trunc)"
);
assert.sameValue(
instance.toString({ smallestUnit: "second", roundingMode: "ceil" }),
"-000099-12-15T12:00:01+00:00[UTC]",
"Rounding up is away from the Big Bang, not the epoch or 1 BCE (roundingMode ceil)"
);
assert.sameValue(
instance.toString({ smallestUnit: "second", roundingMode: "halfExpand" }),
"-000099-12-15T12:00:01+00:00[UTC]",
"Rounding up is away from the Big Bang, not the epoch or 1 BCE (roundingMode halfExpand)"
);

0 comments on commit 61cfb53

Please sign in to comment.