v8/test/mjsunit/temporal/instant-to-json.js
Frank Tang 1a5cfc20e1 [Temporal] Add toJSON, toString, and non-intl toLocaleString to Instant
Also add AOs: FormatISOTimeZoneOffsetString, TemporalInstantToString,
GetUnsignedRoundingMode, ApplyUnsignedRoundingMode, RoundTemporalInstant

Also update the RoundNumberToIncrement implementation and a BigInt version.

Fix the test setup in test/mjsunit/temporal/instant-to-json.js since fromEpochSeconds and fromEpochMilliseconds do not take BigInt

Spec Text:
https://tc39.es/proposal-temporal/#sec-temporal.instant.prototype.tojson
https://tc39.es/proposal-temporal/#sec-temporal.instant.prototype.tolocalestring
https://tc39.es/proposal-temporal/#sec-temporal.instant.prototype.tostring
https://tc39.es/proposal-temporal/#sec-temporal-temporalinstanttostring
https://tc39.es/proposal-temporal/#sec-temporal-formatisotimezoneoffsetstring
https://tc39.es/proposal-temporal/#sec-temporal-getunsignedroundingmode
https://tc39.es/proposal-temporal/#sec-temporal-applyunsignedroundingmode
https://tc39.es/proposal-temporal/#sec-temporal-roundtemporalinstant
https://tc39.es/proposal-temporal/#sec-temporal-roundnumbertoincrement

This does NOT implement the intl version of toLocaleString
specified in (notice the "sup" not "sec" after #) yet.
https://tc39.es/proposal-temporal/#sup-temporal.instant.prototype.tolocalestring

Bug: v8:11544
Change-Id: I807afd5bf550d2a65a4732a8e536056eea79cf8f
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3696483
Commit-Queue: Frank Tang <ftang@chromium.org>
Reviewed-by: Adam Klein <adamk@chromium.org>
Cr-Commit-Position: refs/heads/main@{#81428}
2022-06-29 08:31:17 +00:00

46 lines
1.7 KiB
JavaScript

// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --harmony-temporal
assertEquals("1970-01-01T00:00:00Z",
(Temporal.Instant.fromEpochSeconds(0)).toJSON());
let days_in_sec = 24 * 60 * 60;
assertEquals("1970-12-31T23:59:59Z",
Temporal.Instant.fromEpochSeconds((365 * days_in_sec) - 1).toJSON());
assertEquals("1971-01-01T00:00:00Z",
Temporal.Instant.fromEpochSeconds((365 * days_in_sec)).toJSON());
assertEquals("1971-12-31T23:59:59Z",
Temporal.Instant.fromEpochSeconds((2 *365 * days_in_sec - 1)).toJSON());
assertEquals("1972-01-01T00:00:00Z",
Temporal.Instant.fromEpochSeconds((2 *365 * days_in_sec)).toJSON());
// 1972 is a leap year
assertEquals("1972-02-28T00:00:00Z",
Temporal.Instant.fromEpochSeconds(((2 *365 + 58) * days_in_sec)).toJSON());
assertEquals("1972-02-29T00:00:00Z",
Temporal.Instant.fromEpochSeconds(((2 *365 + 59) * days_in_sec)).toJSON());
assertEquals("1985-01-01T00:00:00Z",
Temporal.Instant.fromEpochSeconds(((15 *365 + 4) * days_in_sec)).toJSON());
// Test with Date
const year_in_sec = 24*60*60*365;
const number_of_random_test = 500;
for (i = 0; i < number_of_random_test ; i++) {
// bertween -5000 years and +5000 years
let ms = Math.floor(Math.random() * year_in_sec * 1000 * 10000) - year_in_sec * 1000 * 5000;
// Temporal auto precision will remove trailing zeros in milliseconds so we only
// compare the first 19 char- to second.
let d = new Date(ms)
dateout = d.toJSON().substr(0,19);
temporalout = Temporal.Instant.fromEpochMilliseconds(ms).toJSON().substr(0, 19);
if (dateout[0] != '0') {
assertEquals(dateout, temporalout, ms);
}
}