From 0cfa9c98607c851a1d77e9132d822aad9d2979da Mon Sep 17 00:00:00 2001 From: Joe Reuter Date: Wed, 4 Dec 2019 12:35:15 +0100 Subject: [PATCH] respect timezone setting --- .../canvas/public/functions/timelion.ts | 27 ++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/x-pack/legacy/plugins/canvas/public/functions/timelion.ts b/x-pack/legacy/plugins/canvas/public/functions/timelion.ts index e948cf586e6e1..4377f2cb4d53b 100644 --- a/x-pack/legacy/plugins/canvas/public/functions/timelion.ts +++ b/x-pack/legacy/plugins/canvas/public/functions/timelion.ts @@ -5,8 +5,10 @@ */ import { flatten } from 'lodash'; +import moment from 'moment-timezone'; import chrome from 'ui/chrome'; import { npStart } from 'ui/new_platform'; +import { TimeRange } from 'src/plugins/data/common'; import { ExpressionFunction, DatatableRow } from 'src/plugins/expressions/public'; import { fetch } from '../../common/lib/fetch'; // @ts-ignore untyped local @@ -22,6 +24,26 @@ interface Arguments { timezone: string; } +/** + * This function parses a given time range containing date math + * and returns ISO dates. Parsing is done respecting the given time zone. + * @param timeRange time range to parse + * @param timeZone time zone to do the parsing in + */ +function parseDateMath(timeRange: TimeRange, timeZone: string) { + // the datemath plugin always parses dates by using the current default moment time zone. + // to use the configured time zone, we are switching just for the bounds calculation. + const defaultTimezone = moment().zoneName(); + moment.tz.setDefault(timeZone); + + const parsedRange = npStart.plugins.data.query.timefilter.timefilter.calculateBounds(timeRange); + + // reset default moment timezone + moment.tz.setDefault(defaultTimezone); + + return parsedRange; +} + export function timelion(): ExpressionFunction<'timelion', Filter, Arguments, Promise> { const { help, args: argHelp } = getFunctionHelp().timelion; @@ -66,10 +88,7 @@ export function timelion(): ExpressionFunction<'timelion', Filter, Arguments, Pr const timeFilter = context.and.find(and => and.type === 'time'); const range = timeFilter ? { min: timeFilter.from, max: timeFilter.to } - : npStart.plugins.data.query.timefilter.timefilter.calculateBounds({ - from: args.from, - to: args.to, - }); + : parseDateMath({ from: args.from, to: args.to }, args.timezone); const body = { extended: {