diff --git a/.changeset/itchy-areas-help.md b/.changeset/itchy-areas-help.md new file mode 100644 index 000000000..b9e92d8a3 --- /dev/null +++ b/.changeset/itchy-areas-help.md @@ -0,0 +1,5 @@ +--- +"@adobe/alloy": patch +--- + +Include IANA timezone on the place context diff --git a/packages/core/src/components/Context/injectPlaceContext.js b/packages/core/src/components/Context/injectPlaceContext.js index 6c0bae743..af0ea1980 100644 --- a/packages/core/src/components/Context/injectPlaceContext.js +++ b/packages/core/src/components/Context/injectPlaceContext.js @@ -28,6 +28,10 @@ export default (dateProvider) => { ) { placeContext.localTime = toISOStringLocal(date); } + const ianaTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone; + if (ianaTimezone) { + placeContext.ianaTimezone = ianaTimezone; + } event.mergeXdm({ placeContext }); }; diff --git a/packages/core/test/unit/specs/components/Context/injectPlaceContext.spec.js b/packages/core/test/unit/specs/components/Context/injectPlaceContext.spec.js index e6251f3ce..e2cbb4be5 100644 --- a/packages/core/test/unit/specs/components/Context/injectPlaceContext.spec.js +++ b/packages/core/test/unit/specs/components/Context/injectPlaceContext.spec.js @@ -10,10 +10,18 @@ OF ANY KIND, either express or implied. See the License for the specific languag governing permissions and limitations under the License. */ -import { vi, describe, it, expect } from "vitest"; +import { vi, describe, it, expect, beforeEach } from "vitest"; import injectPlaceContext from "../../../../../src/components/Context/injectPlaceContext.js"; describe("Context::injectPlaceContext", () => { + beforeEach(() => { + vi.spyOn(Intl, "DateTimeFormat").mockImplementation(() => { + return { + resolvedOptions: () => ({ timeZone: "" }), + }; + }); + }); + it("adds placeContext", () => { const date = new Date("March 25, 2019 21:56:18"); vi.spyOn(date, "getTimezoneOffset").mockReturnValue(7 * 60); @@ -72,4 +80,28 @@ describe("Context::injectPlaceContext", () => { }, }); }); + it("includes ianaTimezone when Intl reports a timezone", () => { + vi.mocked(Intl.DateTimeFormat).mockImplementation(() => { + return { + resolvedOptions: () => ({ timeZone: "America/New_York" }), + }; + }); + const date = new Date("March 25, 2019 21:56:18"); + vi.spyOn(date, "getTimezoneOffset").mockReturnValue(4 * 60); + const event = { mergeXdm: vi.fn() }; + injectPlaceContext(() => date)(event); + expect(event.mergeXdm).toHaveBeenCalledWith({ + placeContext: expect.objectContaining({ + ianaTimezone: "America/New_York", + }), + }); + }); + it("omits ianaTimezone when Intl reports an empty timezone", () => { + const date = new Date("March 25, 2019 21:56:18"); + vi.spyOn(date, "getTimezoneOffset").mockReturnValue(4 * 60); + const event = { mergeXdm: vi.fn() }; + injectPlaceContext(() => date)(event); + const placeContext = event.mergeXdm.mock.calls[0][0].placeContext; + expect(placeContext).not.toHaveProperty("ianaTimezone"); + }); });