Debian Codegraph @

js/src/tests/test262/intl402/Temporal/ZonedDateTime/from/zoneddatetime-sub-minute-offset.js

raw
1 // |reftest| shell-option(--enable-temporal) skip-if(!this.hasOwnProperty('Temporal')||!xulRuntime.shell) -- Temporal is not enabled unconditionally, requires shell-options
2 // Copyright (C) 2021 Igalia, S.L. All rights reserved.
3 // This code is governed by the BSD license found in the LICENSE file.
4
5 /*---
6 esid: sec-temporal.zoneddatetime.from
7 description: Fuzzy matching behaviour with UTC offsets in ISO 8601 strings with named time zones and offset option
8 includes: [temporalHelpers.js]
9 features: [Temporal]
10 ---*/
11
12 ["use", "ignore", "prefer", "reject"].forEach((offset) => {
13 const result = Temporal.ZonedDateTime.from("1970-01-01T12:00-00:44:30[Africa/Monrovia]", { offset });
14 assert.sameValue(result.epochNanoseconds, 45870_000_000_000n, `accepts the exact offset string (offset: ${offset})`);
15 assert.sameValue(result.offset, "-00:44:30", "offset property is correct");
16 });
17
18 ["use", "ignore", "prefer", "reject"].forEach((offset) => {
19 const result = Temporal.ZonedDateTime.from("1970-01-01T12:00-00:44:30.000000000[Africa/Monrovia]", { offset });
20 assert.sameValue(
21 result.epochNanoseconds,
22 45870_000_000_000n,
23 `accepts trailing zeroes after ISO string offset (offset: ${offset})`
24 );
25 assert.sameValue(result.offset, "-00:44:30", "offset property removes trailing zeroes from input");
26 });
27
28 assert.throws(
29 RangeError,
30 () => Temporal.ZonedDateTime.from("1970-01-01T00:00-00:44:30[-00:45]", { offset: "reject" }),
31 "minute rounding not supported for offset time zones"
32 );
33
34 const str = "1970-01-01T12:00-00:45[Africa/Monrovia]";
35
36 ["ignore", "prefer", "reject"].forEach((offset) => {
37 const result = Temporal.ZonedDateTime.from(str, { offset });
38 assert.sameValue(
39 result.epochNanoseconds,
40 45870_000_000_000n,
41 `accepts the offset string rounded to minutes (offset=${offset})`
42 );
43 assert.sameValue(result.offset, "-00:44:30", "offset property is still the full precision");
44 TemporalHelpers.assertPlainDateTime(
45 result.toPlainDateTime(),
46 1970,
47 1,
48 "M01",
49 1,
50 12,
51 0,
52 0,
53 0,
54 0,
55 0,
56 "wall time is preserved"
57 );
58 });
59
60 const result = Temporal.ZonedDateTime.from(str, { offset: "use" });
61 assert.sameValue(
62 result.epochNanoseconds,
63 45900_000_000_000n,
64 "prioritizes the offset string with HH:MM precision when offset=use"
65 );
66 assert.sameValue(result.offset, "-00:44:30", "offset property is still the full precision");
67 TemporalHelpers.assertPlainDateTime(
68 result.toPlainDateTime(),
69 1970,
70 1,
71 "M01",
72 1,
73 12,
74 0,
75 30,
76 0,
77 0,
78 0,
79 "wall time is shifted by the difference between exact and rounded offset"
80 );
81
82 const properties = { year: 1970, month: 1, day: 1, hour: 12, offset: "-00:45", timeZone: "Africa/Monrovia" };
83
84 ["ignore", "prefer"].forEach((offset) => {
85 const result = Temporal.ZonedDateTime.from(properties, { offset });
86 assert.sameValue(
87 result.epochNanoseconds,
88 45870_000_000_000n,
89 `no fuzzy matching is done on offset in property bag (offset=${offset})`
90 );
91 });
92
93 const result2 = Temporal.ZonedDateTime.from(properties, { offset: "use" });
94 assert.sameValue(
95 result2.epochNanoseconds,
96 45900_000_000_000n,
97 "no fuzzy matching is done on offset in property bag (offset=use)"
98 );
99
100 assert.throws(
101 RangeError,
102 () => Temporal.ZonedDateTime.from(properties, { offset: "reject" }),
103 "no fuzzy matching is done on offset in property bag (offset=reject)"
104 );
105
106 reportCompare(0, 0);