Skip to content

Commit e07ea03

Browse files
gtnxGuillaume Thomas
andauthored
Fix handling of InUse timezone for date_histogram (#44)
Co-authored-by: Guillaume Thomas <guillaume.thomas@inuse.eu>
1 parent 7cb021e commit e07ea03

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

src/ui/public/agg_types/buckets/date_histogram.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,22 @@ import { timefilter } from '../../timefilter';
3232
import dropPartialTemplate from '../controls/drop_partials.html';
3333
import { i18n } from '@kbn/i18n';
3434

35+
function getCookie(cname) {
36+
const name = cname + '=';
37+
const decodedCookie = decodeURIComponent(document.cookie);
38+
const ca = decodedCookie.split(';');
39+
for(let i = 0; i < ca.length; i++) {
40+
let c = ca[i];
41+
while (c.charAt(0) === ' ') {
42+
c = c.substring(1);
43+
}
44+
if (c.indexOf(name) === 0) {
45+
return c.substring(name.length, c.length);
46+
}
47+
}
48+
return '';
49+
}
50+
3551
const config = chrome.getUiSettingsClient();
3652
const detectedTimezone = tzDetect.determine().name();
3753
const tzOffset = moment().format('Z');
@@ -52,6 +68,7 @@ function setBounds(agg, force) {
5268
}
5369

5470

71+
5572
export const dateHistogramBucketAgg = new BucketAggType({
5673
name: 'date_histogram',
5774
title: i18n.translate('common.ui.aggTypes.buckets.dateHistogramTitle', {
@@ -157,9 +174,16 @@ export const dateHistogramBucketAgg = new BucketAggType({
157174
},
158175
{
159176
name: 'time_zone',
160-
default: () => {
161-
const isDefaultTimezone = config.isDefault('dateFormat:tz');
162-
return isDefaultTimezone ? detectedTimezone || tzOffset : config.get('dateFormat:tz');
177+
write: (agg, output) => {
178+
if (config.isDefault('dateFormat:tz')) {
179+
output.params.time_zone = getCookie('timezone');
180+
}
181+
else if (config.get('dateFormat:tz') === 'Browser') {
182+
output.params.time_zone = detectedTimezone || tzOffset;
183+
}
184+
else {
185+
output.params.time_zone = config.get('dateFormat:tz');
186+
}
163187
},
164188
},
165189
{

src/ui/public/vis/lib/timezone.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,27 @@
2020
const tzDetect = require('jstimezonedetect').jstz;
2121
import moment from 'moment';
2222

23+
function getCookie(cname) {
24+
const name = cname + '=';
25+
const decodedCookie = decodeURIComponent(document.cookie);
26+
const ca = decodedCookie.split(';');
27+
for(let i = 0; i < ca.length; i++) {
28+
let c = ca[i];
29+
while (c.charAt(0) === ' ') {
30+
c = c.substring(1);
31+
}
32+
if (c.indexOf(name) === 0) {
33+
return c.substring(name.length, c.length);
34+
}
35+
}
36+
return '';
37+
}
38+
2339
export function timezoneProvider(config) {
2440
return function () {
25-
2641
if (config.isDefault('dateFormat:tz')) {
42+
return getCookie('timezone');
43+
} else if (config.get('dateFormat:tz') === 'Browser') {
2744
const detectedTimezone = tzDetect.determine().name();
2845
if (detectedTimezone) return detectedTimezone;
2946
else return moment().format('Z');

0 commit comments

Comments
 (0)