Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix date_nanos formatting for formats without fractional seconds #43114

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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": "",
}
`);
});