Skip to content

Commit

Permalink
Datetime serialization fix (backport #7089) [release/4.8.x] (#7102)
Browse files Browse the repository at this point in the history
Co-authored-by: christophermlawson <32881725+christophermlawson@users.noreply.github.com>
  • Loading branch information
mergify[bot] and christophermlawson authored Aug 22, 2024
1 parent 49fae69 commit 1347577
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@itwin/ecschema-metadata",
"comment": "",
"type": "none"
}
],
"packageName": "@itwin/ecschema-metadata"
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
Expand Down
9 changes: 9 additions & 0 deletions core/ecschema-metadata/src/test/Metadata/Class.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1318,6 +1318,11 @@ describe("ECClass", () => {
typeName: "dateTime",
name: "DateTime",
},
{
type: "PrimitiveProperty",
typeName: "dateTime",
name: "DateTimeString",
},
{
type: "PrimitiveProperty",
typeName: "point2d",
Expand Down Expand Up @@ -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",
Expand All @@ -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");
Expand All @@ -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");
Expand Down
10 changes: 10 additions & 0 deletions core/ecschema-metadata/src/test/Metadata/Property.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,11 @@ describe("Property", () => {
typeName: "dateTime",
name: "DateTime",
},
{
type: "PrimitiveProperty",
typeName: "dateTime",
name: "DateTimeString",
},
{
type: "PrimitiveProperty",
typeName: "point2d",
Expand Down Expand Up @@ -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",
Expand All @@ -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");
Expand All @@ -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");
Expand Down Expand Up @@ -1399,3 +1408,4 @@ describe("NavigationProperty (Deserialization not fully implemented)", () => {
});
});
});

0 comments on commit 1347577

Please sign in to comment.