Skip to content

Commit

Permalink
Fix handling of InUse timezone for date_histogram (#44)
Browse files Browse the repository at this point in the history
Co-authored-by: Guillaume Thomas <[email protected]>
  • Loading branch information
gtnx and Guillaume Thomas authored Jun 1, 2021
1 parent 7cb021e commit e07ea03
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
30 changes: 27 additions & 3 deletions src/ui/public/agg_types/buckets/date_histogram.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ import { timefilter } from '../../timefilter';
import dropPartialTemplate from '../controls/drop_partials.html';
import { i18n } from '@kbn/i18n';

function getCookie(cname) {
const name = cname + '=';
const decodedCookie = decodeURIComponent(document.cookie);
const ca = decodedCookie.split(';');
for(let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) === ' ') {
c = c.substring(1);
}
if (c.indexOf(name) === 0) {
return c.substring(name.length, c.length);
}
}
return '';
}

const config = chrome.getUiSettingsClient();
const detectedTimezone = tzDetect.determine().name();
const tzOffset = moment().format('Z');
Expand All @@ -52,6 +68,7 @@ function setBounds(agg, force) {
}



export const dateHistogramBucketAgg = new BucketAggType({
name: 'date_histogram',
title: i18n.translate('common.ui.aggTypes.buckets.dateHistogramTitle', {
Expand Down Expand Up @@ -157,9 +174,16 @@ export const dateHistogramBucketAgg = new BucketAggType({
},
{
name: 'time_zone',
default: () => {
const isDefaultTimezone = config.isDefault('dateFormat:tz');
return isDefaultTimezone ? detectedTimezone || tzOffset : config.get('dateFormat:tz');
write: (agg, output) => {
if (config.isDefault('dateFormat:tz')) {
output.params.time_zone = getCookie('timezone');
}
else if (config.get('dateFormat:tz') === 'Browser') {
output.params.time_zone = detectedTimezone || tzOffset;
}
else {
output.params.time_zone = config.get('dateFormat:tz');
}
},
},
{
Expand Down
19 changes: 18 additions & 1 deletion src/ui/public/vis/lib/timezone.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,27 @@
const tzDetect = require('jstimezonedetect').jstz;
import moment from 'moment';

function getCookie(cname) {
const name = cname + '=';
const decodedCookie = decodeURIComponent(document.cookie);
const ca = decodedCookie.split(';');
for(let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) === ' ') {
c = c.substring(1);
}
if (c.indexOf(name) === 0) {
return c.substring(name.length, c.length);
}
}
return '';
}

export function timezoneProvider(config) {
return function () {

if (config.isDefault('dateFormat:tz')) {
return getCookie('timezone');
} else if (config.get('dateFormat:tz') === 'Browser') {
const detectedTimezone = tzDetect.determine().name();
if (detectedTimezone) return detectedTimezone;
else return moment().format('Z');
Expand Down

0 comments on commit e07ea03

Please sign in to comment.