Skip to content

Commit d070b74

Browse files
fix(ui): Use configured timezone in TimeSince tooltip (#101181)
The TimeSince component now uses the timezone from useTimezone() hook to format the tooltip, ensuring it displays the correct timezone abbreviation based on the `TimezoneProvider`.
1 parent c6388d0 commit d070b74

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

static/app/components/timeSince.spec.tsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import {render, screen} from 'sentry-test/reactTestingLibrary';
1+
import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
22

33
import TimeSince from 'sentry/components/timeSince';
4+
import {TimezoneProvider} from 'sentry/components/timezoneProvider';
45

56
describe('TimeSince', () => {
67
const now = new Date();
@@ -56,4 +57,18 @@ describe('TimeSince', () => {
5657
render(<TimeSince unitStyle="extraShort" date={pastTenMin} suffix="atrás" />);
5758
expect(screen.getByText('10m atrás')).toBeInTheDocument();
5859
});
60+
61+
it('respects timezone in tooltip', async () => {
62+
const date = new Date('2024-01-15T12:00:00Z');
63+
render(
64+
<TimezoneProvider timezone="America/New_York">
65+
<TimeSince date={date} />
66+
</TimezoneProvider>
67+
);
68+
const timeElement = screen.getByRole('time');
69+
await userEvent.hover(timeElement);
70+
await waitFor(() => {
71+
expect(screen.getByText(/EST/)).toBeInTheDocument();
72+
});
73+
});
5974
});

static/app/components/timeSince.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import getDuration from 'sentry/utils/duration/getDuration';
99
import type {ColorOrAlias} from 'sentry/utils/theme';
1010
import {useUser} from 'sentry/utils/useUser';
1111

12+
import {useTimezone} from './timezoneProvider';
13+
1214
function getDateObj(date: RelaxedDateType): Date {
1315
return typeof date === 'string' || isNumber(date) ? new Date(date) : date;
1416
}
@@ -120,6 +122,7 @@ function TimeSince({
120122
...props
121123
}: Props) {
122124
const user = useUser();
125+
const tz = useTimezone();
123126

124127
// Counter to trigger periodic re-computation of relative time
125128
const [tick, setTick] = useState(0);
@@ -153,7 +156,7 @@ function TimeSince({
153156
: 'MMMM D, YYYY h:mm A z';
154157
const format = options?.clock24Hours ? 'MMMM D, YYYY HH:mm z' : tooltipFormat;
155158

156-
const tooltip = moment(dateObj).format(format);
159+
const tooltip = moment.tz(dateObj, tz).format(format);
157160

158161
return (
159162
<Tooltip

0 commit comments

Comments
 (0)