Skip to content

Commit

Permalink
Support custom zone formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
thatsmydoing committed Feb 9, 2023
1 parent 03f2391 commit 5877f93
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 7 deletions.
8 changes: 4 additions & 4 deletions src/impl/locale.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { padStart, roundTo, hasRelative } from "./util.js";
import { padStart, roundTo, hasRelative, formatOffset } from "./util.js";
import * as English from "./english.js";
import Settings from "../settings.js";
import DateTime from "../datetime.js";
Expand Down Expand Up @@ -203,7 +203,7 @@ class PolyDateFormatter {
if (this.opts.timeZone) {
// Don't apply any workarounds if a timeZone is explicitly provided in opts
this.dt = dt;
} else if (dt.zone.isUniversal) {
} else if (dt.zone.type === "fixed") {
// 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. Representing offset 0 when UTC is used to maintain previous behavior and does not become GMT.
Expand All @@ -229,9 +229,9 @@ class PolyDateFormatter {
z = dt.zone.name;
} else {
// Custom zones can have any offset / offsetName so we just manually
// apply the offset and substitute the offsetName as needed.
// apply the offset and substitute the zone as needed.
z = "UTC";
this.dt = dt.offset === 0 ? dt : dt.setZone("UTC").plus({ minutes: dt.offset });
this.dt = dt.setZone("UTC").plus({ minutes: dt.offset });
this.originalZone = dt.zone;
}

Expand Down
66 changes: 63 additions & 3 deletions test/datetime/format.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* global test expect */

import { DateTime } from "../../src/luxon";
import { DateTime, Zone } from "../../src/luxon";

const dtMaker = () =>
DateTime.fromObject(
Expand All @@ -20,6 +20,50 @@ const dtMaker = () =>
dt = dtMaker(),
invalid = DateTime.invalid("because");

class CustomZone extends Zone {
constructor(name, offset) {
super();
this._name = name;
this._offset = offset;
}

get isUniversal() {
return true;
}

get isValid() {
return true;
}

get name() {
return this._name;
}

get type() {
return "custom";
}

equals(zone) {
return zone instanceof CustomZone && zone._name === this._name && zone._offset === this._offset;
}

offset(_ms) {
return this._offset;
}

offsetName(_ms, { format }) {
if (format === "short") {
return this._name.substring(0, 4);
} else {
return this._name;
}
}

formatOffset(...args) {
return FixedOffsetZone.prototype.formatOffset(...args);
}
}

//------
// #toJSON()
//------
Expand Down Expand Up @@ -403,8 +447,24 @@ test("DateTime#toLocaleString() does the best it can with unsupported fixed-offs
});

test("DateTime#toLocaleString() does the best it can with unsupported fixed-offset zone with timeStyle full", () => {
expect(dt.setZone("UTC+4:30").toLocaleString({ timeStyle: "full" })).toBe(
"1:53:54 PM UTC+4:30"
expect(dt.setZone("UTC+4:30").toLocaleString({ timeStyle: "full" })).toBe("1:53:54 PM UTC+4:30");
});

test("DateTime#toLocaleString() shows things in the right custom zone", () => {
expect(dt.setZone(new CustomZone("CUSTOM", 30)).toLocaleString(DateTime.DATETIME_SHORT)).toBe(
"5/25/1982, 9:53 AM"
);
});

test("DateTime#toLocaleString() shows things in the right custom zone when showing the zone", () => {
expect(dt.setZone(new CustomZone("CUSTOM", 30)).toLocaleString(DateTime.DATETIME_FULL)).toBe(
"May 25, 1982 at 9:53 AM CUST"
);
});

test("DateTime#toLocaleString() shows things in the right custom zone with timeStyle full", () => {
expect(dt.setZone(new CustomZone("CUSTOM", 30)).toLocaleString({ timeStyle: "full" })).toBe(
"9:53:54 AM CUSTOM"
);
});

Expand Down

0 comments on commit 5877f93

Please sign in to comment.