Commit c89ad9b0 by Jesse Tan Committed by GitHub

TimeSrv: Try to parse 8 and 15 digit numbers as timestamps if parsing as date fails (#21694)

* Try to parse 8 and 15 digit numbers as timestamps if parsing as date fails

Fixes #19738

* Add tests
parent 2106f0af
...@@ -135,6 +135,38 @@ describe('timeSrv', () => { ...@@ -135,6 +135,38 @@ describe('timeSrv', () => {
expect(time.to.valueOf()).toEqual(1410337665699); expect(time.to.valueOf()).toEqual(1410337665699);
}); });
it('should handle epochs that look like formatted date without time', () => {
location = {
search: jest.fn(() => ({
from: '20149999',
to: '20159999',
})),
};
timeSrv = new TimeSrv(rootScope as any, jest.fn() as any, location as any, timer, new ContextSrvStub() as any);
timeSrv.init(_dashboard);
const time = timeSrv.timeRange();
expect(time.from.valueOf()).toEqual(20149999);
expect(time.to.valueOf()).toEqual(20159999);
});
it('should handle epochs that look like formatted date', () => {
location = {
search: jest.fn(() => ({
from: '201499991234567',
to: '201599991234567',
})),
};
timeSrv = new TimeSrv(rootScope as any, jest.fn() as any, location as any, timer, new ContextSrvStub() as any);
timeSrv.init(_dashboard);
const time = timeSrv.timeRange();
expect(time.from.valueOf()).toEqual(201499991234567);
expect(time.to.valueOf()).toEqual(201599991234567);
});
it('should handle bad dates', () => { it('should handle bad dates', () => {
location = { location = {
search: jest.fn(() => ({ search: jest.fn(() => ({
......
...@@ -102,10 +102,15 @@ export class TimeSrv { ...@@ -102,10 +102,15 @@ export class TimeSrv {
return value; return value;
} }
if (value.length === 8) { if (value.length === 8) {
return toUtc(value, 'YYYYMMDD'); const utcValue = toUtc(value, 'YYYYMMDD');
} if (utcValue.isValid()) {
if (value.length === 15) { return utcValue;
return toUtc(value, 'YYYYMMDDTHHmmss'); }
} else if (value.length === 15) {
const utcValue = toUtc(value, 'YYYYMMDDTHHmmss');
if (utcValue.isValid()) {
return utcValue;
}
} }
if (!isNaN(value)) { if (!isNaN(value)) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment