Skip to content

Commit

Permalink
Bugfix for exception at discover context view using date_nanos (#41353)
Browse files Browse the repository at this point in the history
* Fix extracting nanos of timestamps with only dates

* Add jest test catching it
  • Loading branch information
kertal authored Jul 18, 2019
1 parent 0c9a4a1 commit 8cfe265
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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 { extractNanos } from '../date_conversion';

describe('function extractNanos', function() {
test('extract nanos of 2014-01-01', function() {
expect(extractNanos('2014-01-01')).toBe('000000000');
});
test('extract nanos of 2014-01-01T12:12:12.234Z', function() {
expect(extractNanos('2014-01-01T12:12:12.234Z')).toBe('234000000');
});
test('extract nanos of 2014-01-01T12:12:12.234123321Z', function() {
expect(extractNanos('2014-01-01T12:12:12.234123321Z')).toBe('234123321');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ import moment from 'moment';
* returns the nanos as string like this:
* 9ns -> 000000009
* 10000ns -> 0000010000
*/
* returns 000000000 for invalid timestamps or timestamps with just date
**/
export function extractNanos(timeFieldValue: string = ''): string {
const fractionSeconds = timeFieldValue.split('.')[1].replace('Z', '');
const fieldParts = timeFieldValue.split('.');
const fractionSeconds = fieldParts.length === 2 ? fieldParts[1].replace('Z', '') : '';
return fractionSeconds.length !== 9 ? fractionSeconds.padEnd(9, '0') : fractionSeconds;
}

Expand Down

0 comments on commit 8cfe265

Please sign in to comment.