From ca66c6e437d337761864d54db750f6bee6946cc4 Mon Sep 17 00:00:00 2001 From: Matthias Wilhelm Date: Wed, 21 Aug 2019 22:42:16 +0200 Subject: [PATCH] Fix date_nanos formatting for formats without fractional seconds (#43114) * Fix formatting for formats without S... part * Add jest test catching the bug --- .../common/field_formats/types/date_nanos.js | 13 +++--- .../field_formats/types/date_nanos.test.js | 42 +++++++++++++++++++ 2 files changed, 48 insertions(+), 7 deletions(-) create mode 100644 src/legacy/core_plugins/kibana/common/field_formats/types/date_nanos.test.js diff --git a/src/legacy/core_plugins/kibana/common/field_formats/types/date_nanos.js b/src/legacy/core_plugins/kibana/common/field_formats/types/date_nanos.js index b440f51451b7d..48ed1c05eb7b3 100644 --- a/src/legacy/core_plugins/kibana/common/field_formats/types/date_nanos.js +++ b/src/legacy/core_plugins/kibana/common/field_formats/types/date_nanos.js @@ -26,13 +26,14 @@ import _ from 'lodash'; * part when formatting with moment.js -> e.g. [SSS] */ export function analysePatternForFract(pattern) { - const fracSecMatch = pattern.match('S+'); //extract fractional seconds sub-pattern + const fracSecMatchStr = fracSecMatch ? fracSecMatch[0] : ''; + return { - length: fracSecMatch[0] ? fracSecMatch[0].length : 0, - patternNanos: fracSecMatch[0], + length: fracSecMatchStr.length, + patternNanos: fracSecMatchStr, pattern, - patternEscaped: fracSecMatch[0] ? pattern.replace(fracSecMatch[0], `[${fracSecMatch[0]}]`) : '', + patternEscaped: fracSecMatchStr ? pattern.replace(fracSecMatch, `[${fracSecMatch}]`) : '', }; } @@ -42,11 +43,9 @@ export function analysePatternForFract(pattern) { * milliseconds, the fractional pattern is replaced by the fractional value of the raw timestamp */ export function formatWithNanos(dateMomentObj, valRaw, fracPatternObj) { - if (fracPatternObj.length <= 3) { //S,SS,SSS is formatted correctly by moment.js return dateMomentObj.format(fracPatternObj.pattern); - } else { //Beyond SSS the precise value of the raw datetime string is used const valFormatted = dateMomentObj.format(fracPatternObj.patternEscaped); @@ -99,7 +98,7 @@ export function createDateNanosFormat(FieldFormat) { const date = moment(val); - if(typeof val !== 'string' && date.isValid()) { + if (typeof val !== 'string' && date.isValid()) { //fallback for max/min aggregation, where unixtime in ms is returned as a number //aggregations in Elasticsearch generally just return ms return date.format(fallbackPattern); diff --git a/src/legacy/core_plugins/kibana/common/field_formats/types/date_nanos.test.js b/src/legacy/core_plugins/kibana/common/field_formats/types/date_nanos.test.js new file mode 100644 index 0000000000000..04e89f97316ad --- /dev/null +++ b/src/legacy/core_plugins/kibana/common/field_formats/types/date_nanos.test.js @@ -0,0 +1,42 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { analysePatternForFract } from './date_nanos'; + +test('analysePatternForFract using timestamp format containing fractional seconds', () => { + expect(analysePatternForFract('MMM, YYYY @ HH:mm:ss.SSS')).toMatchInlineSnapshot(` + Object { + "length": 3, + "pattern": "MMM, YYYY @ HH:mm:ss.SSS", + "patternEscaped": "MMM, YYYY @ HH:mm:ss.[SSS]", + "patternNanos": "SSS", + } + `); +}); + +test('analysePatternForFract using timestamp format without fractional seconds', () => { + expect(analysePatternForFract('MMM, YYYY @ HH:mm:ss')).toMatchInlineSnapshot(` + Object { + "length": 0, + "pattern": "MMM, YYYY @ HH:mm:ss", + "patternEscaped": "", + "patternNanos": "", + } + `); +});