From 30c3d4c0fac3272fef35691604bea3f83b5bd53e Mon Sep 17 00:00:00 2001 From: Simon Date: Sun, 10 Jul 2022 14:12:24 -0600 Subject: [PATCH 1/4] fix(core): using correct timezone when parsing dates --- src/app/core/entity/schema-datatypes/datatype-date-only.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/app/core/entity/schema-datatypes/datatype-date-only.ts b/src/app/core/entity/schema-datatypes/datatype-date-only.ts index 1288e49617..0e97dd4bca 100644 --- a/src/app/core/entity/schema-datatypes/datatype-date-only.ts +++ b/src/app/core/entity/schema-datatypes/datatype-date-only.ts @@ -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); } From 50556067bee120ee62740043f59fd31f4ef1cbc9 Mon Sep 17 00:00:00 2001 From: Simon Date: Sun, 10 Jul 2022 14:19:43 -0600 Subject: [PATCH 2/4] added fix for datatype month --- src/app/core/entity/schema-datatypes/datatype-month.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/app/core/entity/schema-datatypes/datatype-month.ts b/src/app/core/entity/schema-datatypes/datatype-month.ts index 26c7a554e5..5ca7449274 100644 --- a/src/app/core/entity/schema-datatypes/datatype-month.ts +++ b/src/app/core/entity/schema-datatypes/datatype-month.ts @@ -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") + .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); } From a2bdfc48bda4b09a4400efcfe061ba9eb6b3e0d9 Mon Sep 17 00:00:00 2001 From: Simon Date: Sun, 10 Jul 2022 14:29:48 -0600 Subject: [PATCH 3/4] added test --- .../datatype-date-only.spec.ts | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/app/core/entity/schema-datatypes/datatype-date-only.spec.ts b/src/app/core/entity/schema-datatypes/datatype-date-only.spec.ts index e4c9b0aa44..0f14344d5d 100644 --- a/src/app/core/entity/schema-datatypes/datatype-date-only.spec.ts +++ b/src/app/core/entity/schema-datatypes/datatype-date-only.spec.ts @@ -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 = + dateOnlyEntitySchemaDatatype.transformToObjectFormat(dbFormat); + expect(objFormat).toBeInstanceOf(Date); + expect(objFormat.getFullYear()).toBe(2022); + expect(objFormat.getMonth()).toBe(0); + expect(objFormat.getDate()).toBe(1); + }); }); From 13a462593b82a63dc738fa060152ac46c3e47b8a Mon Sep 17 00:00:00 2001 From: Simon Date: Tue, 12 Jul 2022 08:51:59 +0200 Subject: [PATCH 4/4] removed unneeded 0 fills --- src/app/core/entity/schema-datatypes/datatype-month.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/app/core/entity/schema-datatypes/datatype-month.ts b/src/app/core/entity/schema-datatypes/datatype-month.ts index 5ca7449274..7e57dca6b8 100644 --- a/src/app/core/entity/schema-datatypes/datatype-month.ts +++ b/src/app/core/entity/schema-datatypes/datatype-month.ts @@ -41,12 +41,7 @@ export const monthEntitySchemaDatatype: EntitySchemaDatatype = { }, transformToObjectFormat: (value: string) => { - const values = value - .toString() - .replace(/-(\d)-/g, "-0$1-") - .replace(/-(\d)$/g, "-0$1") - .split("-") - .map((v) => Number(v)); + const values = value.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);