realm-time-zone.js (2260B)
1 // |reftest| skip-if(!this.hasOwnProperty('Intl')||!xulRuntime.shell) 2 3 // Don't run in browser because `SpecialPowers.Cu.getJSTestingFunctions()` doesn't 4 // appear to be able to change time zone in other Realms. 5 6 const nsPerMinute = 60 * 1000 * 1000 * 1000; 7 8 // Default time zone for jstests is PST8PDT. 9 const defaultTimeZone = "PST8PDT"; 10 11 assertEq(["PST", "PDT"].includes(getTimeZone()), true); 12 13 const canonicalDefaultTimeZone = new Intl.DateTimeFormat("en", { 14 timeZone: defaultTimeZone 15 }).resolvedOptions().timeZone; 16 17 function test(timeZone) { 18 // The time zone of the initial global is correct. 19 assertEq(getRealmTimeZone(), canonicalDefaultTimeZone); 20 21 // Create a new global with a different time zone. 22 var g = newGlobal({timeZone}); 23 24 var initialTimeZone = timeZone ?? canonicalDefaultTimeZone; 25 26 // Ensure the new global has the expected time zone. 27 assertEq(g.getRealmTimeZone(), initialTimeZone); 28 29 // Create a Date object in the new global. 30 var d = new g.Date(); 31 32 // Call getTimeZoneOffset to fill the local date-time slots in |d|. 33 assertEq( 34 d.getTimezoneOffset() * nsPerMinute, 35 -d.toTemporalInstant().toZonedDateTimeISO(initialTimeZone).offsetNanoseconds + 0 36 ); 37 38 // Change the time zone of the new global. 39 g.setRealmTimeZone("Asia/Tokyo"); 40 41 // Ensure the new global has the expected time zone. 42 assertEq(g.getRealmTimeZone(), "Asia/Tokyo"); 43 44 // The time zone of the initial global hasn't changed. 45 assertEq(getRealmTimeZone(), canonicalDefaultTimeZone); 46 47 // Ensure the local date-time slots in |d| don't return stale values. 48 assertEq( 49 d.getTimezoneOffset() * nsPerMinute, 50 -d.toTemporalInstant().toZonedDateTimeISO("Asia/Tokyo").offsetNanoseconds + 0 51 ); 52 53 // Change the time zone of the new global to use the default time zone. 54 g.setRealmTimeZone(undefined); 55 56 // Ensure the new global has the expected time zone. 57 assertEq(g.getRealmTimeZone(), canonicalDefaultTimeZone); 58 59 // Ensure the local date-time slots in |d| don't return stale values. 60 assertEq( 61 d.getTimezoneOffset() * nsPerMinute, 62 -d.toTemporalInstant().toZonedDateTimeISO(defaultTimeZone).offsetNanoseconds + 0 63 ); 64 } 65 66 test(); 67 test("Europe/London"); 68 69 if (typeof reportCompare === "function") 70 reportCompare(true, true);