Skip to content
Snippets Groups Projects
Unverified Commit 4e30153c authored by Ryan Laurie's avatar Ryan Laurie Committed by GitHub
Browse files

Improved datetime handling in Action Forms :woozy_face: (#29002)

* improved datetime handling

* :muscle: more tests :muscle:
parent 5e54ea23
No related branches found
No related tags found
No related merge requests found
......@@ -25,15 +25,15 @@ export const formatValue = (
value: string | number | null,
inputType?: InputSettingType,
) => {
if (!isEmpty(value)) {
if (!isEmpty(value) && typeof value === "string") {
if (inputType === "date" && moment(value).isValid()) {
return moment(value).utc(true).format("YYYY-MM-DD");
return moment(stripTZInfo(value)).format("YYYY-MM-DD");
}
if (inputType === "datetime" && moment(value).isValid()) {
return moment(value).utc(true).format("YYYY-MM-DDTHH:mm:ss");
return moment(stripTZInfo(value)).format("YYYY-MM-DDTHH:mm:ss");
}
if (inputType === "time") {
return moment(`2020-01-10 ${value}`).utc(true).format("HH:mm:ss");
return moment(stripTZInfo(`2020-01-10T${value}`)).format("HH:mm:ss");
}
}
return value;
......@@ -51,3 +51,8 @@ export const getInitialValues = (
]),
);
};
export function stripTZInfo(dateOrTimeString: string) {
// strip everything after a trailing tz (e.g. +08:00)
return moment(dateOrTimeString.replace(/(\+|-)\d{2}:\d{2}$/, "")).utc(true);
}
import { getChangedValues, formatValue } from "./utils";
import { getChangedValues, formatValue, stripTZInfo } from "./utils";
describe("actions > containers > ActionParametersInputForm > utils", () => {
describe("stripTZInfo", () => {
it("should strip timezone info from dateTimes", () => {
const result = stripTZInfo("2020-05-01T03:30:00-02:00");
expect(result.format()).toEqual("2020-05-01T03:30:00Z");
});
it("should strip timezone info from times", () => {
const result = stripTZInfo("2020-05-01T03:30:00+08:00");
expect(result.format()).toEqual("2020-05-01T03:30:00Z");
});
it("should not do anything to strings without time info", () => {
const result = stripTZInfo("2020-05-01");
expect(result.format()).toEqual("2020-05-01T00:00:00Z");
});
it("should not do anything to strings without timezone info", () => {
const result = stripTZInfo("2020-05-01T03:30:00");
expect(result.format()).toEqual("2020-05-01T03:30:00Z");
});
});
describe("formatValue", () => {
it("ignores null values", () => {
const result = formatValue(null);
......@@ -17,14 +39,28 @@ describe("actions > containers > ActionParametersInputForm > utils", () => {
expect(result).toEqual("123");
});
it("formats dates", () => {
const result = formatValue("2020-05-01T00:00:00Z", "date");
expect(result).toEqual("2020-05-01");
});
it("formats datetimes", () => {
const result = formatValue("2020-05-01T05:00:00Z", "datetime");
expect(result).toEqual("2020-05-01T05:00:00");
const timezones = ["-02:00", "-07:00", "+01:00", "+09:00"];
timezones.forEach(offset => {
describe(`with timezone ${offset}`, () => {
it("formats dates", () => {
const result = formatValue(`2020-05-01T00:00:00${offset}`, "date");
expect(result).toEqual("2020-05-01");
});
it("formats datetimes", () => {
const result = formatValue(
`2020-05-01T05:00:00${offset}`,
"datetime",
);
expect(result).toEqual("2020-05-01T05:00:00");
});
it("formats times", () => {
const result = formatValue(`05:25:30${offset}`, "time");
expect(result).toEqual("05:25:30");
});
});
});
});
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment