From f24cc420f9d37e7cd576601bc242a789e33d9ed1 Mon Sep 17 00:00:00 2001 From: christophermlawson <32881725+christophermlawson@users.noreply.github.com> Date: Mon, 19 Aug 2024 09:35:06 -0400 Subject: [PATCH] Datetime serialization fix (#7089) (cherry picked from commit 0881665149e92e1f4a1b5170db70d0a6faa169ac) --- .../datetime-serialization-fix_2024-08-15-15-12.json | 10 ++++++++++ .../src/Deserialization/XmlSerializationUtils.ts | 2 +- core/ecschema-metadata/src/test/Metadata/Class.test.ts | 9 +++++++++ .../src/test/Metadata/Property.test.ts | 10 ++++++++++ 4 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 common/changes/@itwin/ecschema-metadata/datetime-serialization-fix_2024-08-15-15-12.json diff --git a/common/changes/@itwin/ecschema-metadata/datetime-serialization-fix_2024-08-15-15-12.json b/common/changes/@itwin/ecschema-metadata/datetime-serialization-fix_2024-08-15-15-12.json new file mode 100644 index 000000000000..5dd8c0e532c0 --- /dev/null +++ b/common/changes/@itwin/ecschema-metadata/datetime-serialization-fix_2024-08-15-15-12.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@itwin/ecschema-metadata", + "comment": "", + "type": "none" + } + ], + "packageName": "@itwin/ecschema-metadata" +} \ No newline at end of file diff --git a/core/ecschema-metadata/src/Deserialization/XmlSerializationUtils.ts b/core/ecschema-metadata/src/Deserialization/XmlSerializationUtils.ts index 8ed1dddfdf5b..ae7c4562951c 100644 --- a/core/ecschema-metadata/src/Deserialization/XmlSerializationUtils.ts +++ b/core/ecschema-metadata/src/Deserialization/XmlSerializationUtils.ts @@ -146,7 +146,7 @@ export namespace XmlSerializationUtils { propertyElement.textContent = propertyValue.toString(); return; case PrimitiveType.DateTime: - propertyElement.textContent = (propertyValue as Date).getTime().toString(); + propertyElement.textContent = new Date(propertyValue).getTime().toString(); return; case PrimitiveType.Point2d: propertyElement.textContent = `${propertyValue.x},${propertyValue.y}`; diff --git a/core/ecschema-metadata/src/test/Metadata/Class.test.ts b/core/ecschema-metadata/src/test/Metadata/Class.test.ts index 75937b4e5e39..2f74cc889132 100644 --- a/core/ecschema-metadata/src/test/Metadata/Class.test.ts +++ b/core/ecschema-metadata/src/test/Metadata/Class.test.ts @@ -1318,6 +1318,11 @@ describe("ECClass", () => { typeName: "dateTime", name: "DateTime", }, + { + type: "PrimitiveProperty", + typeName: "dateTime", + name: "DateTimeString", + }, { type: "PrimitiveProperty", typeName: "point2d", @@ -1354,6 +1359,7 @@ describe("ECClass", () => { Long: 100, Double: 200, DateTime: new Date(nowTicks), + DateTimeString: "2021-08-19T16:37:42.278", Point2D: { x: 100, y: 200 }, Point3D: { x: 100, y: 200, z: 300 }, IGeometry: "geometry", @@ -1362,6 +1368,7 @@ describe("ECClass", () => { testClass.addCustomAttribute(ca); const serialized = await testClass.toXml(newDom); + const expectedTimeFromString = new Date("2021-08-19T16:37:42.278").getTime(); let element = getCAPropertyValueElement(serialized, "TestCustomAttribute", "TrueBoolean"); expect(element.textContent).to.equal("True"); @@ -1375,6 +1382,8 @@ describe("ECClass", () => { expect(element.textContent).to.equal("200"); element = getCAPropertyValueElement(serialized, "TestCustomAttribute", "DateTime"); expect(element.textContent).to.equal(nowTicks.toString()); + element = getCAPropertyValueElement(serialized, "TestCustomAttribute", "DateTimeString"); + expect(element.textContent).to.equal(expectedTimeFromString.toString()); element = getCAPropertyValueElement(serialized, "TestCustomAttribute", "Point2D"); expect(element.textContent).to.equal("100,200"); element = getCAPropertyValueElement(serialized, "TestCustomAttribute", "Point3D"); diff --git a/core/ecschema-metadata/src/test/Metadata/Property.test.ts b/core/ecschema-metadata/src/test/Metadata/Property.test.ts index 1c279a7b9f50..f278620c0818 100644 --- a/core/ecschema-metadata/src/test/Metadata/Property.test.ts +++ b/core/ecschema-metadata/src/test/Metadata/Property.test.ts @@ -480,6 +480,11 @@ describe("Property", () => { typeName: "dateTime", name: "DateTime", }, + { + type: "PrimitiveProperty", + typeName: "dateTime", + name: "DateTimeString", + }, { type: "PrimitiveProperty", typeName: "point2d", @@ -517,6 +522,7 @@ describe("Property", () => { Long: 100, Double: 200, DateTime: new Date(nowTicks), + DateTimeString: "2021-08-19T16:37:42.278", Point2D: { x: 100, y: 200 }, Point3D: { x: 100, y: 200, z: 300 }, IGeometry: "geometry", @@ -525,6 +531,7 @@ describe("Property", () => { property.addCustomAttribute(ca); const serialized = await property.toXml(newDom); + const expectedTimeFromString = new Date("2021-08-19T16:37:42.278").getTime(); let element = getCAPropertyValueElement(serialized, "TestCustomAttribute", "TrueBoolean"); expect(element.textContent).to.equal("True"); @@ -538,6 +545,8 @@ describe("Property", () => { expect(element.textContent).to.equal("200"); element = getCAPropertyValueElement(serialized, "TestCustomAttribute", "DateTime"); expect(element.textContent).to.equal(nowTicks.toString()); + element = getCAPropertyValueElement(serialized, "TestCustomAttribute", "DateTimeString"); + expect(element.textContent).to.equal(expectedTimeFromString.toString()); element = getCAPropertyValueElement(serialized, "TestCustomAttribute", "Point2D"); expect(element.textContent).to.equal("100,200"); element = getCAPropertyValueElement(serialized, "TestCustomAttribute", "Point3D"); @@ -1399,3 +1408,4 @@ describe("NavigationProperty (Deserialization not fully implemented)", () => { }); }); }); +