Skip to content

Commit

Permalink
Fix date_nanos formatting for formats without fractional seconds (ela…
Browse files Browse the repository at this point in the history
…stic#43114)

* Fix formatting for formats without S... part

* Add jest test catching the bug
  • Loading branch information
kertal committed Aug 21, 2019
1 parent 4540c55 commit ca66c6e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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}]`) : '',
};
}

Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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": "",
}
`);
});

0 comments on commit ca66c6e

Please sign in to comment.