Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): using correct timezone when parsing dates #1355

Merged
merged 6 commits into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/app/core/entity/schema-datatypes/datatype-date-only.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,23 @@ import { dateOnlyEntitySchemaDatatype } from "./datatype-date-only";

describe("Schema data type:Date", () => {
it("should not fail on null values", () => {
const nullDateRes = dateOnlyEntitySchemaDatatype.transformToDatabaseFormat(
null
);
const nullDateRes =
dateOnlyEntitySchemaDatatype.transformToDatabaseFormat(null);
expect(nullDateRes).toBeUndefined();
});

it("should correctly transform values", () => {
const date = new Date(2022, 0, 1);

const dbFormat =
dateOnlyEntitySchemaDatatype.transformToDatabaseFormat(date);
expect(dbFormat).toBe("2022-01-01");

const objFormat: Date =
sleidig marked this conversation as resolved.
Show resolved Hide resolved
dateOnlyEntitySchemaDatatype.transformToObjectFormat(dbFormat);
expect(objFormat).toBeInstanceOf(Date);
expect(objFormat.getFullYear()).toBe(2022);
expect(objFormat.getMonth()).toBe(0);
expect(objFormat.getDate()).toBe(1);
});
});
7 changes: 5 additions & 2 deletions src/app/core/entity/schema-datatypes/datatype-date-only.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,11 @@ export const dateOnlyEntitySchemaDatatype: EntitySchemaDatatype = {
);
},

transformToObjectFormat: (value) => {
const date = new Date(value);
transformToObjectFormat: (value: string) => {
// new Date("2022-01-01") is interpreted as UTC time whereas new Date(2022, 0, 1) is local time
// -> we want local time to represent the same day wherever used.
const values = value.split("-").map((v) => Number(v));
const date = new Date(values[0], values[1] - 1, values[2]);
if (Number.isNaN(date.getTime())) {
throw new Error("failed to convert data to Date object: " + value);
}
Expand Down
10 changes: 6 additions & 4 deletions src/app/core/entity/schema-datatypes/datatype-month.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ export const monthEntitySchemaDatatype: EntitySchemaDatatype = {
);
},

transformToObjectFormat: (value) => {
value = value
transformToObjectFormat: (value: string) => {
const values = value
.toString()
.replace(/-(\d)-/g, "-0$1-")
.replace(/-(\d)$/g, "-0$1");
const date = new Date(value);
.replace(/-(\d)$/g, "-0$1")
TheSlimvReal marked this conversation as resolved.
Show resolved Hide resolved
.split("-")
.map((v) => Number(v));
const date = new Date(values[0], values[1] - 1);
if (Number.isNaN(date.getTime())) {
throw new Error("failed to convert data to Date object: " + value);
}
Expand Down