Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/itchy-areas-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@adobe/alloy": patch
---

Include IANA timezone on the place context
4 changes: 4 additions & 0 deletions packages/core/src/components/Context/injectPlaceContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export default (dateProvider) => {
) {
placeContext.localTime = toISOStringLocal(date);
}
const localTimezoneName = Intl.DateTimeFormat().resolvedOptions().timeZone;
Comment thread
carterworks marked this conversation as resolved.
Outdated
if (localTimezoneName) {
placeContext.localTimezoneName = localTimezoneName;
Comment thread
Spencer-Smith marked this conversation as resolved.
Outdated
}

event.mergeXdm({ placeContext });
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Comment thread
Spencer-Smith marked this conversation as resolved.
return {
resolvedOptions: () => ({ timeZone: "" }),
};
});
});

it("adds placeContext", () => {
const date = new Date("March 25, 2019 21:56:18");
vi.spyOn(date, "getTimezoneOffset").mockReturnValue(7 * 60);
Expand Down Expand Up @@ -72,4 +80,28 @@ describe("Context::injectPlaceContext", () => {
},
});
});
it("includes localTimezoneName 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({
localTimezoneName: "America/New_York",
}),
});
});
it("omits localTimezoneName 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("localTimezoneName");
});
});