mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Fix relative time range parsing (#95603)
This commit is contained in:
parent
777979965d
commit
048ba14c34
@ -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);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -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) {
|
||||||
|
Loading…
Reference in New Issue
Block a user