summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/Date/to-temporal-instant.js
blob: 341a456725265c48740fa5a9867efc940b7dec79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// |reftest| skip-if(!this.hasOwnProperty("Temporal"))

const min = new Date(-8640000000000000).toTemporalInstant();
const max = new Date(8640000000000000).toTemporalInstant();
const epoch = new Date(0).toTemporalInstant();

const minTemporalInstant = new Temporal.Instant(-8640000000000000000000n)
const maxTemporalInstant = new Temporal.Instant(8640000000000000000000n)
const zeroInstant = new Temporal.Instant(0n)

let zero = Temporal.Duration.from({nanoseconds: 0});
let one = Temporal.Duration.from({nanoseconds: 1});
let minusOne = Temporal.Duration.from({nanoseconds: -1});

//Test invalid date
{
    const invalidDate = new Date(NaN);
    assertThrowsInstanceOf(() => invalidDate.toTemporalInstant(), RangeError);
}

//Test Temporal.Instant properties
{
    // epochNanoseconds
    assertEq(min.epochNanoseconds, minTemporalInstant.epochNanoseconds);
    assertEq(max.epochNanoseconds, maxTemporalInstant.epochNanoseconds);
    assertEq(epoch.epochNanoseconds, zeroInstant.epochNanoseconds);

    // toZonedDateTime
    assertEq(min.toZonedDateTimeISO('UTC').toString(), minTemporalInstant.toZonedDateTimeISO('UTC').toString());
    assertEq(max.toZonedDateTimeISO('UTC').toString(), maxTemporalInstant.toZonedDateTimeISO('UTC').toString());
    assertEq(epoch.toZonedDateTimeISO('UTC').toString(), zeroInstant.toZonedDateTimeISO('UTC').toString());
}

// Test values around the minimum/maximum instant.
{
    // Adding zero to the minimum instant.
    assertEq(min.add(zero).epochNanoseconds, min.epochNanoseconds);
    assertEq(min.subtract(zero).epochNanoseconds, min.epochNanoseconds);

    // Adding zero to the maximum instant.
    assertEq(max.add(zero).epochNanoseconds, max.epochNanoseconds);
    assertEq(max.subtract(zero).epochNanoseconds, max.epochNanoseconds);

    // Adding one to the minimum instant.
    assertEq(min.add(one).epochNanoseconds, min.epochNanoseconds + 1n);
    assertEq(min.subtract(minusOne).epochNanoseconds, min.epochNanoseconds + 1n);

    // Subtracting one from the maximum instant.
    assertEq(max.add(minusOne).epochNanoseconds, max.epochNanoseconds - 1n);
    assertEq(max.subtract(one).epochNanoseconds, max.epochNanoseconds - 1n);

    // Subtracting one from the minimum instant.
    assertThrowsInstanceOf(() => min.add(minusOne), RangeError);
    assertThrowsInstanceOf(() => min.subtract(one), RangeError);

    // Adding one to the maximum instant.
    assertThrowsInstanceOf(() => max.add(one), RangeError);
    assertThrowsInstanceOf(() => max.subtract(minusOne), RangeError);
}

if (typeof reportCompare === "function")
    reportCompare(true, true);