Fix date_nanos formatting for formats without fractional seconds (#43114) (#43718)

* Fix formatting for formats without S... part

* Add jest test catching the bug
This commit is contained in:
Matthias Wilhelm 2019-08-23 13:07:50 +02:00 committed by GitHub
parent c9da599ab6
commit 04f79dc0c1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 7 deletions

View file

@ -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);

View file

@ -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": "",
}
`);
});