zoneddatetime-string.js (2447B)
1 // |reftest| skip-if(!this.hasOwnProperty('Temporal')) -- Temporal is not enabled unconditionally 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: Conversion of ISO date-time strings to Temporal.ZonedDateTime instances 8 features: [Temporal] 9 ---*/ 10 11 let str = "1970-01-01T00:00"; 12 assert.throws(RangeError, () => Temporal.ZonedDateTime.from(str), "bare date-time string is not a ZonedDateTime"); 13 str = "1970-01-01T00:00Z"; 14 assert.throws(RangeError, () => Temporal.ZonedDateTime.from(str), "date-time + Z is not a ZonedDateTime"); 15 str = "1970-01-01T00:00+01:00"; 16 assert.throws(RangeError, () => Temporal.ZonedDateTime.from(str), "date-time + offset is not a ZonedDateTime"); 17 18 str = "1970-01-01T00:00[+01:00]"; 19 const result1 = Temporal.ZonedDateTime.from(str); 20 assert.sameValue(result1.epochNanoseconds, -3600_000_000_000n, "date-time + IANA annotation preserves wall time in the time zone"); 21 assert.sameValue(result1.timeZoneId, "+01:00", "IANA annotation is not ignored"); 22 23 str = "1970-01-01T00:00Z[+01:00]"; 24 const result2 = Temporal.ZonedDateTime.from(str); 25 assert.sameValue(result2.epochNanoseconds, 0n, "date-time + Z + IANA annotation preserves exact time in the time zone"); 26 assert.sameValue(result2.timeZoneId, "+01:00", "IANA annotation is not ignored"); 27 28 str = "1970-01-01T00:00+01:00[+01:00]"; 29 const result3 = Temporal.ZonedDateTime.from(str); 30 assert.sameValue(result3.epochNanoseconds, -3600_000_000_000n, "date-time + offset + IANA annotation ensures both exact and wall time match"); 31 assert.sameValue(result3.timeZoneId, "+01:00", "IANA annotation is not ignored"); 32 33 str = "1970-01-01T00:00-04:15[+01:00]"; 34 assert.throws(RangeError, () => Temporal.ZonedDateTime.from(str), "date-time + offset + IANA annotation throws if wall time and exact time mismatch"); 35 assert.throws(RangeError, () => Temporal.ZonedDateTime.from(str, { offset: "reject" }), "date-time + offset + IANA annotation throws if wall time and exact time mismatch (explicit reject option)"); 36 const result4 = Temporal.ZonedDateTime.from(str, { offset: "ignore" }); 37 assert.sameValue(result4.epochNanoseconds, -3600_000_000_000n, "date-time + wrong offset + IANA annotation preserves wall time in the time zone (offset: ignore option)"); 38 assert.sameValue(result4.timeZoneId, "+01:00", "IANA annotation is not ignored"); 39 40 reportCompare(0, 0);