Skip to content

Commit 0cfa9c9

Browse files
committed
respect timezone setting
1 parent e5153c0 commit 0cfa9c9

File tree

1 file changed

+23
-4
lines changed
  • x-pack/legacy/plugins/canvas/public/functions

1 file changed

+23
-4
lines changed

x-pack/legacy/plugins/canvas/public/functions/timelion.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
*/
66

77
import { flatten } from 'lodash';
8+
import moment from 'moment-timezone';
89
import chrome from 'ui/chrome';
910
import { npStart } from 'ui/new_platform';
11+
import { TimeRange } from 'src/plugins/data/common';
1012
import { ExpressionFunction, DatatableRow } from 'src/plugins/expressions/public';
1113
import { fetch } from '../../common/lib/fetch';
1214
// @ts-ignore untyped local
@@ -22,6 +24,26 @@ interface Arguments {
2224
timezone: string;
2325
}
2426

27+
/**
28+
* This function parses a given time range containing date math
29+
* and returns ISO dates. Parsing is done respecting the given time zone.
30+
* @param timeRange time range to parse
31+
* @param timeZone time zone to do the parsing in
32+
*/
33+
function parseDateMath(timeRange: TimeRange, timeZone: string) {
34+
// the datemath plugin always parses dates by using the current default moment time zone.
35+
// to use the configured time zone, we are switching just for the bounds calculation.
36+
const defaultTimezone = moment().zoneName();
37+
moment.tz.setDefault(timeZone);
38+
39+
const parsedRange = npStart.plugins.data.query.timefilter.timefilter.calculateBounds(timeRange);
40+
41+
// reset default moment timezone
42+
moment.tz.setDefault(defaultTimezone);
43+
44+
return parsedRange;
45+
}
46+
2547
export function timelion(): ExpressionFunction<'timelion', Filter, Arguments, Promise<Datatable>> {
2648
const { help, args: argHelp } = getFunctionHelp().timelion;
2749

@@ -66,10 +88,7 @@ export function timelion(): ExpressionFunction<'timelion', Filter, Arguments, Pr
6688
const timeFilter = context.and.find(and => and.type === 'time');
6789
const range = timeFilter
6890
? { min: timeFilter.from, max: timeFilter.to }
69-
: npStart.plugins.data.query.timefilter.timefilter.calculateBounds({
70-
from: args.from,
71-
to: args.to,
72-
});
91+
: parseDateMath({ from: args.from, to: args.to }, args.timezone);
7392

7493
const body = {
7594
extended: {

0 commit comments

Comments
 (0)