Fix relative time range parsing (#95603)

This commit is contained in:
Bogdan Matei 2024-10-30 15:52:19 +02:00 committed by GitHub
parent 777979965d
commit 048ba14c34
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 45 additions and 8 deletions

View File

@ -41,15 +41,49 @@ describe('Range Utils', () => {
expect(deserializedTimeRange.from.format()).toBe('1996-07-30T16:00:00Z'); expect(deserializedTimeRange.from.format()).toBe('1996-07-30T16:00:00Z');
}); });
it('should leave the raw part intact if it has calulactions', () => { it('should leave the raw part intact if it has calculations', () => {
const timeRange = {
from: 'now-6h',
to: 'now',
};
const deserialized = convertRawToRange(timeRange);
expect(deserialized.from).not.toBe(timeRange.from);
expect(deserialized.raw.from).not.toBe(deserialized.from);
expect(deserialized.raw.from).toBe(timeRange.from);
expect(deserialized.to).not.toBe(timeRange.to);
expect(deserialized.raw.to).not.toBe(deserialized.from);
expect(deserialized.raw.to).toBe(timeRange.to);
});
it('should leave the raw part intact if it has calculations for "from"', () => {
const timeRange = {
from: 'now',
to: DEFAULT_DATE_VALUE,
};
const deserialized = convertRawToRange(timeRange);
expect(deserialized.from).not.toBe(timeRange.from);
expect(deserialized.raw.from).not.toBe(deserialized.from);
expect(deserialized.raw.from).toBe(timeRange.from);
expect(deserialized.to).not.toBe(timeRange.to);
expect(deserialized.raw.to).toBe(deserialized.to);
expect(deserialized.raw.to).not.toBe(timeRange.to);
});
it('should leave the raw part intact if it has calculations for "to"', () => {
const timeRange = { const timeRange = {
from: DEFAULT_DATE_VALUE, from: DEFAULT_DATE_VALUE,
to: 'now', to: 'now',
}; };
const deserialized = convertRawToRange(timeRange); const deserialized = convertRawToRange(timeRange);
expect(deserialized.raw).toStrictEqual(timeRange); expect(deserialized.from).not.toBe(timeRange.from);
expect(deserialized.to.toString()).not.toBe(deserialized.raw.to); expect(deserialized.raw.from).toBe(deserialized.from);
expect(deserialized.raw.from).not.toBe(timeRange.from);
expect(deserialized.to).not.toBe(timeRange.to);
expect(deserialized.raw.to).not.toBe(deserialized.to);
expect(deserialized.raw.to).toBe(timeRange.to);
}); });
}); });

View File

@ -207,11 +207,14 @@ export const convertRawToRange = (
const from = dateTimeParse(raw.from, { roundUp: false, timeZone, fiscalYearStartMonth, format }); const from = dateTimeParse(raw.from, { roundUp: false, timeZone, fiscalYearStartMonth, format });
const to = dateTimeParse(raw.to, { roundUp: true, timeZone, fiscalYearStartMonth, format }); const to = dateTimeParse(raw.to, { roundUp: true, timeZone, fiscalYearStartMonth, format });
if (dateMath.isMathString(raw.from) || dateMath.isMathString(raw.to)) { return {
return { from, to, raw }; from,
} to,
raw: {
return { from, to, raw: { from, to } }; from: dateMath.isMathString(raw.from) ? raw.from : from,
to: dateMath.isMathString(raw.to) ? raw.to : to,
},
};
}; };
export function isRelativeTime(v: DateTime | string) { export function isRelativeTime(v: DateTime | string) {