Skip to content

Commit 88eae19

Browse files
authored
Add check for etc zone support before use (#918)
1 parent da04179 commit 88eae19

2 files changed

Lines changed: 15 additions & 4 deletions

File tree

‎src/impl/locale.js‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as English from "./english.js";
33
import Settings from "../settings.js";
44
import DateTime from "../datetime.js";
55
import Formatter from "./formatter.js";
6+
import IANAZone from "../zones/IANAZone.js";
67

78
let intlDTCache = {};
89
function getCachedDTF(locString, opts = {}) {
@@ -185,11 +186,15 @@ class PolyDateFormatter {
185186
if (dt.zone.universal && this.hasIntl) {
186187
// UTC-8 or Etc/UTC-8 are not part of tzdata, only Etc/GMT+8 and the like.
187188
// That is why fixed-offset TZ is set to that unless it is:
188-
// 1. Outside of the supported range Etc/GMT-14 to Etc/GMT+12.
189-
// 2. Not a whole hour, e.g. UTC+4:30.
189+
// 1. Representing offset 0 when UTC is used to maintain previous behavior and does not become GMT.
190+
// 2. Unsupported by the browser:
191+
// - some do not support Etc/
192+
// - < Etc/GMT-14, > Etc/GMT+12, and 30-minute or 45-minute offsets are not part of tzdata
190193
const gmtOffset = -1 * (dt.offset / 60);
191-
if (gmtOffset >= -14 && gmtOffset <= 12 && gmtOffset % 1 === 0) {
192-
z = gmtOffset >= 0 ? `Etc/GMT+${gmtOffset}` : `Etc/GMT${gmtOffset}`;
194+
const offsetZ = gmtOffset >= 0 ? `Etc/GMT+${gmtOffset}` : `Etc/GMT${gmtOffset}`;
195+
const isOffsetZoneSupported = IANAZone.isValidZone(offsetZ);
196+
if (dt.offset !== 0 && isOffsetZoneSupported) {
197+
z = offsetZ;
193198
this.dt = dt;
194199
} else {
195200
// Not all fixed-offset zones like Etc/+4:30 are present in tzdata.

‎test/datetime/format.test.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,12 @@ test("DateTime#toLocaleString() shows things in the right fixed-offset zone when
333333
);
334334
});
335335

336+
test("DateTime#toLocaleString() shows things with UTC if fixed-offset zone with 0 offset is used", () => {
337+
expect(dt.setZone("UTC").toLocaleString(DateTime.DATETIME_FULL)).toBe(
338+
"May 25, 1982, 9:23 AM UTC"
339+
);
340+
});
341+
336342
test("DateTime#toLocaleString() does the best it can with unsupported fixed-offset zone when showing the zone", () => {
337343
expect(dt.setZone("UTC+4:30").toLocaleString(DateTime.DATETIME_FULL)).toBe(
338344
"May 25, 1982, 9:23 AM UTC"

0 commit comments

Comments
 (0)