Skip to content

Commit

Permalink
Allow timestamp with both fractions and offset (#1061)
Browse files Browse the repository at this point in the history
  • Loading branch information
liskaant authored Jan 29, 2025
1 parent 02916f6 commit 5749c55
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
20 changes: 20 additions & 0 deletions packages/protobuf-test/src/json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,26 @@ describe("JSON serialization", () => {
});
});
testJson(TimestampSchema, {}, "1970-01-01T00:00:00Z");
test(`fromJson decodes ${TimestampSchema.typeName}`, () => {
const decode = (str: string, seconds: number, nanos: number) =>
expect(fromJson(TimestampSchema, str)).toStrictEqual({
$typeName: "google.protobuf.Timestamp",
seconds: protoInt64.parse(seconds),
nanos,
});

decode("2025-01-27T11:42:15.689823456+01:00", 1737974535, 689823456);
decode("2025-01-27T11:42:15.6898+01:00", 1737974535, 689800000);
decode("2025-01-27T11:42:15.6+01:00", 1737974535, 600000000);
decode("2025-01-27T11:42:15.0+01:00", 1737974535, 0);
decode("2025-01-27T11:42:15+01:00", 1737974535, 0);
decode("2025-01-27T11:42:15.689823456Z", 1737978135, 689823456);
decode("2025-01-27T11:42:15.689800Z", 1737978135, 689800000);
decode("2025-01-27T11:42:15.6898Z", 1737978135, 689800000);
decode("2025-01-27T11:42:15.6Z", 1737978135, 600000000);
decode("2025-01-27T11:42:15.0Z", 1737978135, 0);
decode("2025-01-27T11:42:15Z", 1737978135, 0);
});
describe("FieldMask", () => {
testJson(
FieldMaskSchema,
Expand Down
2 changes: 1 addition & 1 deletion packages/protobuf/src/from-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ function timestampFromJson(timestamp: Timestamp, json: JsonValue) {
);
}
const matches = json.match(
/^([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(?:Z|\.([0-9]{3,9})Z|([+-][0-9][0-9]:[0-9][0-9]))$/,
/^([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})(?:\.([0-9]{1,9}))?(?:Z|([+-][0-9][0-9]:[0-9][0-9]))$/,
);
if (!matches) {
throw new Error(
Expand Down

0 comments on commit 5749c55

Please sign in to comment.