Skip to content

Commit ff56b96

Browse files
DateDetails: handle not passing in system timezones (#580)
1 parent 79c1525 commit ff56b96

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

src/components/DateDetails/DateDetails.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export const Playground = {
3838
args: {
3939
date: new Date(),
4040
side: "top",
41-
systemTimeZone: "America/Los_Angeles",
41+
systemTimeZone: undefined,
4242
title: "DateDetails",
4343
},
4444
render: (args: Args) => {

src/components/DateDetails/DateDetails.test.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,30 @@ describe("DateDetails", () => {
5555
expect(getByText(fiveMinutesAgo.getTime() / 1000)).toBeInTheDocument();
5656
});
5757

58+
it("allows for not passing in a system timezone", () => {
59+
const baseDate = new Date("2024-12-24 11:45:00 AM");
60+
vi.setSystemTime(baseDate);
61+
62+
const fiveMinutesAgo = new Date("2024-12-24 11:40:00 AM");
63+
64+
const { getByText, queryByText } = renderCUI(<DateDetails date={fiveMinutesAgo} />);
65+
66+
const trigger = getByText("5 minutes ago");
67+
expect(trigger).toBeInTheDocument();
68+
69+
fireEvent.click(trigger);
70+
expect(
71+
getByText(content => {
72+
return content.includes("EST");
73+
})
74+
).toBeInTheDocument();
75+
76+
expect(getByText("Dec 24, 4:40 p.m.")).toBeInTheDocument();
77+
expect(getByText("Dec 24, 11:40 a.m. (EST)")).toBeInTheDocument();
78+
expect(queryByText("System")).not.toBeInTheDocument();
79+
expect(getByText(fiveMinutesAgo.getTime() / 1000)).toBeInTheDocument();
80+
});
81+
5882
it("only shows the date if the previous date isn't in this year", () => {
5983
const baseDate = new Date("2025-02-07 11:45:00 AM");
6084
const systemTimeZone = "America/Los_Angeles";

src/components/DateDetails/DateDetails.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { Container } from "@/components/Container/Container";
1717
dayjs.extend(advancedFormat);
1818
dayjs.extend(duration);
1919
dayjs.extend(localizedFormat);
20+
dayjs.extend(timezone);
2021
dayjs.extend(updateLocale);
2122
dayjs.extend(utc);
2223

@@ -89,7 +90,7 @@ const formatDateDetails = (date: Dayjs, timezone?: string): string => {
8990
return date.format(formatForPastYear).replace("am", "a.m.").replace("pm", "p.m.");
9091
};
9192

92-
const formatTimezone = (date: Dayjs, timezone: string): string => {
93+
const formatTimezone = (date: Dayjs, timezone?: string): string => {
9394
return (
9495
new Intl.DateTimeFormat(undefined, {
9596
timeZone: timezone,
@@ -108,16 +109,11 @@ export interface DateDetailsProps {
108109
systemTimeZone?: string;
109110
}
110111

111-
export const DateDetails = ({
112-
date,
113-
side = "top",
114-
systemTimeZone = "America/New_York",
115-
}: DateDetailsProps) => {
112+
export const DateDetails = ({ date, side = "top", systemTimeZone }: DateDetailsProps) => {
116113
const dayjsDate = dayjs(date);
117114

118115
let systemTime;
119116
if (systemTimeZone) {
120-
dayjs.extend(timezone);
121117
try {
122118
systemTime = dayjsDate.tz(systemTimeZone);
123119
} catch {

0 commit comments

Comments
 (0)