Skip to content

Commit

Permalink
Remove fixed-offset formatting workaround #866 (#867)
Browse files Browse the repository at this point in the history
* Remove fixed-offset formatting workaround #866

* Fix range of accepted fixed offset tz in formatter
  • Loading branch information
durasj committed Feb 12, 2021
1 parent 6934632 commit ff17eea
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
32 changes: 20 additions & 12 deletions src/impl/locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,20 +183,28 @@ class PolyDateFormatter {

let z;
if (dt.zone.universal && this.hasIntl) {
// Chromium doesn't support fixed-offset zones like Etc/GMT+8 in its formatter,
// See https://bugs.chromium.org/p/chromium/issues/detail?id=364374.
// So we have to make do. Two cases:
// 1. The format options tell us to show the zone. We can't do that, so the best
// we can do is format the date in UTC.
// 2. The format options don't tell us to show the zone. Then we can adjust them
// the time and tell the formatter to show it to us in UTC, so that the time is right
// and the bad zone doesn't show up.
// We can clean all this up when Chrome fixes this.
z = "UTC";
if (opts.timeZoneName) {
// UTC-8 or Etc/UTC-8 are not part of tzdata, only Etc/GMT+8 and the like.
// That is why fixed-offset TZ is set to that unless it is:
// 1. Outside of the supported range Etc/GMT-14 to Etc/GMT+12.
// 2. Not a whole hour, e.g. UTC+4:30.
const gmtOffset = -1 * (dt.offset / 60);
if (gmtOffset >= -14 && gmtOffset <= 12 && gmtOffset % 1 === 0) {
z = gmtOffset >= 0 ? `Etc/GMT+${gmtOffset}` : `Etc/GMT${gmtOffset}`;
this.dt = dt;
} else {
this.dt = dt.offset === 0 ? dt : DateTime.fromMillis(dt.ts + dt.offset * 60 * 1000);
// Not all fixed-offset zones like Etc/+4:30 are present in tzdata.
// So we have to make do. Two cases:
// 1. The format options tell us to show the zone. We can't do that, so the best
// we can do is format the date in UTC.
// 2. The format options don't tell us to show the zone. Then we can adjust them
// the time and tell the formatter to show it to us in UTC, so that the time is right
// and the bad zone doesn't show up.
z = "UTC";
if (opts.timeZoneName) {
this.dt = dt;
} else {
this.dt = dt.offset === 0 ? dt : DateTime.fromMillis(dt.ts + dt.offset * 60 * 1000);
}
}
} else if (dt.zone.type === "local") {
this.dt = dt;
Expand Down
8 changes: 7 additions & 1 deletion test/datetime/format.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,14 @@ test("DateTime#toLocaleString() shows things in the right fixed-offset zone", ()
expect(dt.setZone("UTC-8").toLocaleString(DateTime.DATETIME_SHORT)).toBe("5/25/1982, 1:23 AM");
});

test("DateTime#toLocaleString() does the best it can with a fixed-offset zone when showing the zone", () => {
test("DateTime#toLocaleString() shows things in the right fixed-offset zone when showing the zone", () => {
expect(dt.setZone("UTC-8").toLocaleString(DateTime.DATETIME_FULL)).toBe(
"May 25, 1982, 1:23 AM GMT-8"
);
});

test("DateTime#toLocaleString() does the best it can with unsupported fixed-offset zone when showing the zone", () => {
expect(dt.setZone("UTC+4:30").toLocaleString(DateTime.DATETIME_FULL)).toBe(
"May 25, 1982, 9:23 AM UTC"
);
});
Expand Down

0 comments on commit ff17eea

Please sign in to comment.