From 01a3078d6403c602817c9eb2f64bd7126bb81555 Mon Sep 17 00:00:00 2001 From: Ashley Harrison Date: Tue, 29 Aug 2023 11:46:16 +0100 Subject: [PATCH] account for timezone when using relative times Co-authored-by: eledobleefe Co-authored-by: Joao Silva Co-authored-by: L-M-K-B <48948963+L-M-K-B@users.noreply.github.com> Co-authored-by: joshhunt Co-authored-by: tskarhed <1438972+tskarhed@users.noreply.github.com> --- packages/grafana-data/src/datetime/moment_wrapper.ts | 6 ++++++ .../components/TimePicker/TimePickerWithHistory.test.tsx | 2 +- public/app/features/dashboard/utils/panel.ts | 8 +++++--- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/packages/grafana-data/src/datetime/moment_wrapper.ts b/packages/grafana-data/src/datetime/moment_wrapper.ts index 73f5850dde5..a89d56279c3 100644 --- a/packages/grafana-data/src/datetime/moment_wrapper.ts +++ b/packages/grafana-data/src/datetime/moment_wrapper.ts @@ -130,6 +130,12 @@ export const dateTimeForTimeZone = ( return toUtc(input, formatInput); } + if (timezone && timezone !== 'browser') { + // TODO fix types here + //@ts-ignore + return moment.tz(dateTimeAsMoment(input), formatInput, timezone); + } + return dateTime(input, formatInput); }; diff --git a/public/app/core/components/TimePicker/TimePickerWithHistory.test.tsx b/public/app/core/components/TimePicker/TimePickerWithHistory.test.tsx index c946efe0547..87ab11d13c8 100644 --- a/public/app/core/components/TimePicker/TimePickerWithHistory.test.tsx +++ b/public/app/core/components/TimePicker/TimePickerWithHistory.test.tsx @@ -129,7 +129,7 @@ describe('TimePickerWithHistory', () => { it('Should display handle timezones correctly', async () => { const timeRange = getDefaultTimeRange(); - render(); + render(); await userEvent.click(screen.getByLabelText(/Time range selected/)); await clearAndType(getFromField(), '2022-12-10 00:00:00'); diff --git a/public/app/features/dashboard/utils/panel.ts b/public/app/features/dashboard/utils/panel.ts index 38156c1c115..cd178de9a4d 100644 --- a/public/app/features/dashboard/utils/panel.ts +++ b/public/app/features/dashboard/utils/panel.ts @@ -1,6 +1,6 @@ import { isString as _isString } from 'lodash'; -import { TimeRange, AppEvents, rangeUtil, dateMath, PanelModel as IPanelModel } from '@grafana/data'; +import { TimeRange, AppEvents, rangeUtil, dateMath, PanelModel as IPanelModel, dateTimeAsMoment } from '@grafana/data'; import { getTemplateSrv } from '@grafana/runtime'; import appEvents from 'app/core/app_events'; import config from 'app/core/config'; @@ -126,11 +126,13 @@ export function applyPanelTimeOverrides(panel: PanelModel, timeRange: TimeRange) } if (_isString(timeRange.raw.from)) { - const timeFromDate = dateMath.parse(timeFromInfo.from)!; + const fromTimezone = dateTimeAsMoment(timeRange.from).tz(); + const toTimezone = dateTimeAsMoment(timeRange.to).tz(); + const timeFromDate = dateMath.parse(timeFromInfo.from, undefined, fromTimezone)!; newTimeData.timeInfo = timeFromInfo.display; newTimeData.timeRange = { from: timeFromDate, - to: dateMath.parse(timeFromInfo.to)!, + to: dateMath.parse(timeFromInfo.to, undefined, toTimezone)!, raw: { from: timeFromInfo.from, to: timeFromInfo.to,